-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy path世界杯.js
496 lines (473 loc) · 17.9 KB
/
世界杯.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: red; icon-glyph: user-astronaut;
/**
* Author:LSP
* Date:2023-05-12
*/
// -------------------------------------------------------
// 是否是开发环境,配合手机端调试使用,正式发布设置为false
const isDev = false;
const dependencyLSP = '20230511';
console.log(`当前环境 👉👉👉👉👉 ${isDev ? 'DEV' : 'RELEASE'}`);
console.log(`----------------------------------------`);
// 分支
const branch = 'v2';
// 仓库根目录
const remoteGithubRoot = `https://raw.githubusercontent.com/Enjoyee/Scriptable/${branch}`;
const remoteHomeLandRoot = `https://glimmerk.coding.net/p/Scriptable/shared-depot/source/git/raw/${branch}`;
// 依赖包目录
const fm = FileManager.local();
const rootDir = fm.documentsDirectory();
const cacheDir = fm.joinPath(rootDir, 'LSP');
const dependencyFileName = isDev ? "_LSP.js" : `${cacheDir}/_LSP.js`;
// 下载依赖包
await downloadLSPDependency();
// -------------------------------------------------------
if (typeof require === 'undefined') require = importModule
// 引入相关方法
const { BaseWidget } = require(dependencyFileName);
// @定义小组件
class Widget extends BaseWidget {
defaultPreference = {
bgUrl: 'https://s3.uuu.ovh/imgs/2022/12/07/6af2cea7cdbfdce4.png',
shadowColor: '#FFF',
shadowColorAlpha: '0.5',
titleColor: '#222',
teamColor: '#222',
notStartedColor: '#211',
startingColor: '#d30742',
finishedColor: '#666'
};
getValueByKey = (key) => this.readWidgetSetting()[key] ?? this.defaultPreference[key];
titleColorFun = () => this.getValueByKey('titleColor');
teamColorFun = () => this.getValueByKey('teamColor');
notStartedColorFun = () => this.getValueByKey('notStartedColor');
startingColorFun = () => this.getValueByKey('startingColor');
finishedColorFun = () => this.getValueByKey('finishedColor');
constructor(scriptName) {
super(scriptName);
this.changeBgMode2OnLineBg(this.defaultPreference.bgUrl, { ...this.defaultPreference });
}
async getAppViewOptions() {
return {
widgetProvider: {
defaultBgType: '1',
small: false, // 是否提供小号组件
medium: true, // 是否提供中号组件
large: false, // 是否提供大号组件
},
// 预览界面的组件设置item
settingItems: [
{
name: 'filterFinished',
label: '过滤已完成赛事',
type: 'switch',
icon: { name: 'backpack.fill', color: '#e85d04', },
needLoading: false,
default: true,
},
{
name: 'otherSetting',
label: '其他设置',
type: 'cell',
icon: 'https://cdnjson.com/images/2024/02/05/settings.png',
needLoading: true,
childItems: [
{
name: 'titleColor',
label: '标题颜色',
type: 'color',
icon: { name: 'scribble.variable', color: '#264653', },
needLoading: false,
default: this.titleColorFun(),
},
{
name: 'teamColor',
label: '队名颜色',
type: 'color',
icon: { name: 'highlighter', color: '#2a9d8f', },
needLoading: false,
default: this.teamColorFun(),
},
{
name: 'notStartedColor',
label: '未开始比分颜色',
type: 'color',
icon: { name: 'pencil.and.outline', color: '#e9c46a', },
needLoading: false,
default: this.notStartedColorFun(),
},
{
name: 'startingColor',
label: '进行中比分颜色',
type: 'color',
icon: { name: 'lasso', color: '#f4a261', },
needLoading: false,
default: this.startingColorFun(),
},
{
name: 'finishedColor',
label: '结束比分颜色',
type: 'color',
icon: { name: 'lasso.and.sparkles', color: '#e76f51', },
needLoading: false,
default: this.finishedColorFun(),
},
]
},
],
};
}
provideMediumWidget = async (widgetSetting) => {
// 赛程
const RES = await this.loadWorldCupData(widgetSetting.filterFinished ?? true);
//
const ballImg = await this.getImageByUrl('https://gitee.com/enjoyee/img/raw/master/other/ball.png');
//
const titleColor = this.titleColorFun();
const teamColor = this.teamColorFun();
const notStartedColor = this.notStartedColorFun();
const startingColor = this.startingColorFun();
const finishedColor = this.finishedColorFun();
//=================================
const widget = new ListWidget();
widget.setPadding(0, 0, 0, 0);
const widgetSize = this.getWidgetSize('中号');
let { width, height } = widgetSize;
//=================================
const contentStack = widget.addStack();
contentStack.layoutVertically();
contentStack.centerAlignContent();
if (RES.length == 0) {
let mxStack = contentStack.addStack();
mxStack.layoutHorizontally();
mxStack.addSpacer();
let mxImg = await this.getImageByUrl('https://s3.uuu.ovh/imgs/2022/12/08/e84b3697053ddaab.webp');
let mxImgSpan = mxStack.addImage(mxImg);
mxStack.addSpacer();
mxImgSpan.imageSize = new Size(100, 100);
mxImgSpan.centerAlignImage();
let infoStack = contentStack.addStack();
infoStack.layoutHorizontally();
infoStack.addSpacer();
let tipsTextSpan = infoStack.addText('ops~o_O||今天球场上没人');
infoStack.addSpacer();
tipsTextSpan.textColor = new Color(titleColor);
tipsTextSpan.font = Font.systemFont(20);
tipsTextSpan.shadowColor = new Color('#666', 0.4);
tipsTextSpan.shadowRadius = 0.4;
tipsTextSpan.shadowOffset = new Point(1, 1);
} else {
height -= 12;
contentStack.size = new Size(width, height);
const perHeight = height / 3;
const descStackHeight = perHeight / 5 * 2.1;
const detailStackHeight = perHeight / 5 * 3;
const areaStackSize = new Size(width / 7 * 2.5, detailStackHeight);
if (RES.length == 1) {
let topStack = contentStack.addStack();
topStack.layoutHorizontally();
topStack.centerAlignContent();
topStack.addSpacer();
let topImg = await this.getImageByUrl('https://s3.uuu.ovh/imgs/2022/12/06/3f48556409802788.png');
let topImgSpan = topStack.addImage(topImg);
topImgSpan.imageSize = new Size(28, 28);
//
topStack.addSpacer(2);
let topTextSpan = topStack.addText(`${RES[0].round_name}`);
topTextSpan.textColor = new Color(titleColor);
topTextSpan.font = Font.systemFont(20);
topTextSpan.shadowColor = new Color('#666', 0.4);
topTextSpan.shadowRadius = 0.4;
topTextSpan.shadowOffset = new Point(1, 1);
//
topStack.addSpacer(2);
topImgSpan = topStack.addImage(topImg);
topImgSpan.imageSize = new Size(28, 28);
//
topStack.addSpacer();
contentStack.addSpacer(4);
}
for (let index = 0; index < RES.length; index++) {
const item = RES[index];
let { room_id, host_team = {}, guest_team = {}, match_status = 1, bottom_note } = item;
// ================================
let stack = contentStack.addStack();
stack.layoutVertically();
// ================================
let descContainerStack = stack.addStack();
descContainerStack.layoutHorizontally();
descContainerStack.addSpacer();
let descStack = descContainerStack.addStack();
descContainerStack.addSpacer();
descStack.size = new Size(width / 10 * 9, descStackHeight);
descStack.backgroundColor = new Color('#FFF', 0.5);
descStack.cornerRadius = descStackHeight / 2;
descStack.borderWidth = 1;
descStack.borderColor = new Color('#000', 0.2);
descStack.layoutHorizontally();
descStack.centerAlignContent();
///
let imgSpan = descStack.addImage(ballImg);
imgSpan.imageSize = new Size(12, 12);
imgSpan.imageOpacity = 0.6;
descStack.addSpacer(8);
///
let textSpan = descStack.addText(`${item.match_round_desc}`);
textSpan.textColor = new Color(titleColor, 0.9);
textSpan.font = Font.systemFont(12); // 小标题
///
descStack.addSpacer(8);
imgSpan = descStack.addImage(ballImg);
imgSpan.imageSize = new Size(12, 12);
imgSpan.imageOpacity = 0.6;
///
// ================================
stack.addSpacer(4);
let detailStack = stack.addStack();
detailStack.size = new Size(width, detailStackHeight);
detailStack.layoutHorizontally();
let jumpUrl = `https://webcast-open.douyin.com/open/webcast/reflow/?webcast_app_id=6822&room_id=${room_id}`;
if (room_id) {
detailStack.url = jumpUrl;
}
// --------------------------------
let hostStack = detailStack.addStack();
hostStack.size = areaStackSize;
hostStack.centerAlignContent();
hostStack.addSpacer();
textSpan = hostStack.addText(host_team.cn_name);
textSpan.textColor = new Color(teamColor, 1);
textSpan.font = Font.mediumSystemFont(14); // 队名
hostStack.addSpacer(10);
//
let image = await this.getImageByUrl(host_team.flag);
imgSpan = hostStack.addImage(image);
imgSpan.imageSize = new Size(40, 40); // 国旗
// --------------------------------
let scoreStack = detailStack.addStack();
scoreStack.addSpacer();
scoreStack.size = new Size(width / 7 * 2, detailStackHeight);
scoreStack.centerAlignContent();
textSpan = scoreStack.addText(`${item.host_score} : ${item.guest_score}`);
let scoreColor;
switch (match_status) {
case 1: // 未开始
scoreColor = notStartedColor;
break;
case 2: // 直播中
scoreColor = startingColor;
break;
case 3: // 已结束
scoreColor = finishedColor;
break;
}
textSpan.textColor = new Color(scoreColor, 0.8);
textSpan.font = Font.heavySystemFont(28); // 比分
scoreStack.addSpacer();
// --------------------------------
let guestStack = detailStack.addStack();
guestStack.size = areaStackSize;
guestStack.centerAlignContent();
image = await this.getImageByUrl(guest_team.flag);
imgSpan = guestStack.addImage(image);
imgSpan.imageSize = new Size(40, 40); // 国旗
guestStack.addSpacer(10);
//
textSpan = guestStack.addText(guest_team.cn_name);
textSpan.textColor = new Color(teamColor, 1);
textSpan.font = Font.mediumSystemFont(14); // 队名
guestStack.addSpacer();
//
if (index != RES.length - 1) {
stack.addSpacer(10);
}
if (RES.length === 1) {
stack.addSpacer(6);
const bottomStack = stack.addStack();
bottomStack.layoutHorizontally();
bottomStack.addSpacer();
textSpan = bottomStack.addText(`『${bottom_note}』`);
textSpan.textColor = new Color(titleColor, 0.9);
textSpan.font = Font.systemFont(13);
bottomStack.addSpacer();
if (room_id) {
bottomStack.url = jumpUrl;
}
}
}
}
//=================================
return widget;
}
async render({ widgetSetting, family }) {
return await this.provideMediumWidget(widgetSetting);
}
// --------------------------NET START--------------------------
/**
* 世界杯数据
*/
loadWorldCupData = async (filterFinished) => {
const URL = 'https://api5-normal-lq.toutiaoapi.com/vertical/sport/go/world_cup/match_info';
const RES = await this.httpGet(URL);
const match_infos = JSON.parse(RES.data).match_infos;
const matchInfoKeys = Object.keys(match_infos);
// -------------------------------------------------
const currDateKey = this.getDateStr(new Date(), 'yyyyMMdd');
let currDateIndex = matchInfoKeys.indexOf(currDateKey);
// -------------------------------------------------
let preDateIndex = currDateIndex - 1;
let preDateKey = matchInfoKeys[preDateIndex];
// -------------------------------------------------
let afterDateIndex = currDateIndex + 1;
let afterDateKey = '';
if (currDateIndex == matchInfoKeys.length - 1) {
afterDateIndex = -1;
} else {
afterDateKey = matchInfoKeys[afterDateIndex];
}
console.log(`preDateKey->${preDateKey}, afterDateKey:${afterDateKey}, currDateKey=${currDateKey}`);
// -------------------------------------------------
// match_status:1未开始,2:进行中,3:已结束,其他非比赛
let currMatch = match_infos[currDateKey];
let preMatch = match_infos[preDateKey];
let afterMatch;
if (afterDateKey.length > 0) {
afterMatch = match_infos[afterDateKey];
}
//
let showInfoArr = [];
if (preMatch.match_status == 2) {
showInfoArr = showInfoArr.concat(preMatch);
}
if ([1, 2, 3].indexOf(currMatch.match_status) != -1) {
showInfoArr = showInfoArr.concat(currMatch);
}
if (filterFinished) {
showInfoArr = showInfoArr.filter(item => item.match_status != 3);
if (showInfoArr.length == 0 && afterDateKey.length > 0) {
showInfoArr = showInfoArr.concat(afterMatch);
}
}
const infoArr = [];
for (const info of showInfoArr) {
const {
round_name = '',
match_status = 1, // 1:未开始,2:直播中,3:已结束
match_round_desc = '',
host_team = {},
guest_team = {},
host_score = '',
guest_score = '',
bottom_note = '',
room_id = ''
} = info;
infoArr.push({
match_status,
round_name,
match_round_desc: match_round_desc.replaceAll(round_name, ''),
host_team,
guest_team,
host_score,
guest_score,
bottom_note,
room_id
});
}
return infoArr.length > 1 ? infoArr.slice(0, 2) : infoArr;
}
// --------------------------NET END--------------------------
}
await new Widget(Script.name()).run();
// =================================================================================
// =================================================================================
async function downloadLSPDependency() {
let fm = FileManager.local();
const fileName = fm.joinPath(fm.documentsDirectory(), `LSP/${Script.name()}/settings.json`);
const fileExists = fm.fileExists(fileName);
let cacheString = '{}';
if (fileExists) {
cacheString = fm.readString(fileName);
}
const use_github = JSON.parse(cacheString)['use_github'];
const dependencyURL = `${use_github ? remoteGithubRoot : remoteHomeLandRoot}/_LSP.js`;
const update = needUpdateDependency();
if (isDev) {
const iCloudPath = FileManager.iCloud().documentsDirectory();
const localIcloudDependencyExit = fm.isFileStoredIniCloud(`${iCloudPath}/_LSP.js`);
const localDependencyExit = fm.fileExists(`${rootDir}/_LSP.js`);
const fileExist = localIcloudDependencyExit || localDependencyExit;
console.log(`🚀 DEV开发依赖文件${fileExist ? '已存在 ✅' : '不存在 🚫'}`);
if (!fileExist || update) {
console.log(`🤖 DEV 开始${update ? '更新' + dependencyLSP : '下载'}依赖~`);
keySave('VERSION', dependencyLSP);
await downloadFile2Scriptable('_LSP', dependencyURL);
}
return;
}
//////////////////////////////////////////////////////////
console.log(`----------------------------------------`);
const remoteDependencyExit = fm.fileExists(`${cacheDir}/_LSP.js`);
console.log(`🚀 RELEASE依赖文件${remoteDependencyExit ? '已存在 ✅' : '不存在 🚫'}`);
console.log(`----------------------------------------`);
// ------------------------------
if (!remoteDependencyExit) { // 下载依赖
// 创建根目录
if (!fm.fileExists(cacheDir)) {
fm.createDirectory(cacheDir, true);
}
// 下载
console.log('🤖 RELEASE开始下载依赖~');
console.log(`----------------------------------------`);
const req = new Request(dependencyURL);
const moduleJs = await req.load();
if (moduleJs) {
fm.write(fm.joinPath(cacheDir, '/_LSP.js'), moduleJs);
console.log('✅ LSP远程依赖环境下载成功!');
console.log(`----------------------------------------`);
} else {
console.error('🚫 获取依赖环境脚本失败,请重试!');
console.log(`----------------------------------------`);
}
}
}
/**
* 获取保存的文件名
* @param {*} fileName
* @returns
*/
function getSaveFileName(fileName) {
const hasSuffix = fileName.lastIndexOf(".") + 1;
return !hasSuffix ? `${fileName}.js` : fileName;
};
/**
* 保存文件到Scriptable软件目录,app可看到
* @param {*} fileName
* @param {*} content
* @returns
*/
function saveFile2Scriptable(fileName, content) {
try {
const fm = FileManager.iCloud();
let fileSimpleName = getSaveFileName(fileName);
const filePath = fm.joinPath(fm.documentsDirectory(), fileSimpleName);
fm.writeString(filePath, content);
return true;
} catch (error) {
return false;
}
};
/**
* 下载js文件到Scriptable软件目录
* @param {*} moduleName 名称
* @param {*} url 在线地址
* @returns
*/
async function downloadFile2Scriptable(moduleName, url) {
const req = new Request(url);
const content = await req.loadString();
return saveFile2Scriptable(`${moduleName}`, content);
};
// =================================================================================
// =================================================================================