forked from Juniorchen2012/scriptable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatchDay.js
219 lines (193 loc) · 6.2 KB
/
matchDay.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
var id="9"
var s="39301"
var local="en"
// var matchdaysUrl ="https://feedmonster.onefootball.com/feeds/il/en/competitions/112/39198/matchdaysOverview.json"
// https://api.onefootball.com/scores-mixer/v1/en/cn/matchdays/6074754
//https://feedmonster.onefootball.com/feeds/il/en/competitions/112/39198/standings.json
// zh en
if(Device.locale() == "zh_CN"){
local="zh"
}
if (args.widgetParameter == "laliga") {
//laliga
id = 10
s=39319
}else if (args.widgetParameter == "bundesliga") {
//bundesliga
id=1
s=39285
} else if (args.widgetParameter == "sa") {
//serie A
id=13
s=39325
}else if (args.widgetParameter == "lue") {
//ligue1 uber eats
id=23
s=39245
}
//You can find more ids and s value here:https://github.com/Juniorchen2012/scriptable/blob/master/footballLeagueData
formatDate()
var matchdaysurl=`https://feedmonster.onefootball.com/feeds/il/en/competitions/${id}/${s}/matchdaysOverview.json`
const matchjson = await get({ 'url': matchdaysurl })
var matchdayId=""
for(var matchday of matchjson.matchdays){
if(matchday.isCurrentMatchday){
matchdayId = matchday.id
}
}
if(
matchdayId.length==0
){return}
var url=`https://api.onefootball.com/scores-mixer/v1/${local}/cn/matchdays/${matchdayId}`
// const requestM = new importModule('Env')()
const json = await get({ 'url': url })
const w = new ListWidget()
const bgColor = new LinearGradient()
bgColor.colors = [new Color("#1c1c1c"), new Color("#29323c")]
bgColor.locations = [0.0, 1.0]
// w.backgroundGradient = bgColor
w.backgroundColor = new Color("#222222")
var matchDate=""
var count=0
for (var item of json.kickoffs) {
if(count>4){
break
}
var date = new Date(item.kickoff)
let nowa = new Date()
let formatter = new RelativeDateTimeFormatter()
let str = formatter.string(date, nowa)
console.log(str)
if(str.includes( "昨天")||str.includes("yesterday")){
continue
}
if(date.getDate()!=matchDate){
matchDate=date.getDate()
const dLine = w.addText(`${str}`)
dLine.textSize = 8
dLine.textColor = new Color("#e587ce")
dLine.font = Font.systemFont(13)
w.addSpacer(2)
}
for (match of item.groups[0].matches) {
count++
var now = new Date(item.kickoff)
// now.setTime(item.kickoff * 1000)
// console.log(match.begin_time + now)
// console.log(date)
console.log(date.getHours())
var homeScore = ''
var awayScore = ''
if (match.period != 'PreMatch') {
homeScore = match.score_home
awayScore = match.score_away
}
var i = 0
var total = match.team_home.name.length
for (i = 0; i < 5 - total; i++) {
console.log(match.team_home.name)
match.team_home.name += " "
}
var homeWin = ""
var awayWin = ""
// if (homeScore > awayScore) {
// homeWin = "🏆"
// }
//
// if (homeScore < awayScore) {
// awayWin = "🏆"
// }
const stack = w.addStack()
w.addSpacer(2)
stack.layoutHorizontally()
stack.centerAlignContent()
const homeimg = await loadImage(`https://images.onefootball.com/icons/teams/56/${match.team_home.id}.png`)
// const timeText = createTextStack(stack, `${now.getHours()}:${now.getMinutes() == 0 ? '00' : now.getMinutes()}`, 40)
const timeText = createTextStack(stack, `${now.format("hh:mm")}`, 40)
const imagew = stack.addImage(homeimg)
imagew.imageSize=new Size(20, 20)
const homeText = createTextStack(stack, ` ${match.team_home.name} `, 100)
homeText.leftAlignText()
const scroeText = createTextStack(stack, `${homeWin} ${homeScore} VS ${awayScore} ${awayWin}`, 50)
const awayText = createTextStack(stack, ` ${match.team_away.name}`, 100)
awayText.rightAlignText()
// if (today) {
homeText.textOpacity = 1
awayText.textOpacity = 1
// }
scroeText.textOpacity=1
if (match.period != 'PreMatch' && match.period!= 'FullTime') {
homeText.textColor = Color.red()
awayText.textColor = Color.red()
scroeText.textColor = Color.red()
}
if (match.period== 'FullTime') {
homeText.textOpacity = 0.6
awayText.textOpacity = 0.6
scroeText.textOpacity=0.6
}
const img = await loadImage(`https://images.onefootball.com/icons/teams/56/${match.team_away.id}.png`)
const imagew1 = stack.addImage(img)
imagew1.imageSize=new Size(20, 20)
}
// }
}
w.presentMedium()
if (config.runsInWidget) {
let widget = w
Script.setWidget(widget)
Script.complete()
}
function createTextStack(stack, text, width) {
const tmpStack = stack.addStack()
tmpStack.size = new Size(width, 20)
const widgetText = tmpStack.addText(text)
widgetText.font = Font.systemFont(13)
// homeText.textColor = new Color("#e587ce")
widgetText.textColor = Color.white()
widgetText.textOpacity = 0.6
return widgetText
}
// QuickLook.present(table)
async function loadImage(imgUrl) {
let req = new Request(imgUrl)
let image = await req.loadImage()
return image
}
async function get(opts) {
const request = new Request(opts.url)
request.headers = {
...opts.headers,
...this.defaultHeaders
}
var result=await request.loadJSON()
console.log(result)
return result
}
async function loadImage(imgUrl) {
let req = new Request(imgUrl)
let image = await req.loadImage()
return image
}
function formatDate() {
Date.prototype.format = function(fmt) {
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt)) {
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("("+ k +")").test(fmt)){
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
}
}
return fmt;
}
}