forked from dompling/Scriptable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BiliBili.js
273 lines (250 loc) · 8.34 KB
/
BiliBili.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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-gray; icon-glyph: tv;
// 添加require,是为了vscode中可以正确引入包,以获得自动补全等功能
if (typeof require === 'undefined') require = importModule;
const { DmYY, Runing } = require('./DmYY');
// @组件代码开始
class Widget extends DmYY {
constructor(arg) {
super(arg);
this.name = '哔哩哔哩今日番剧';
this.en = 'BiliBiliMonitor';
this.logo =
'https://raw.githubusercontent.com/Orz-3/mini/master/Color/bilibili.png';
config.runsInApp && this.registerAction('基础设置', this.setWidgetConfig);
this.cacheName = this.md5(`dataSouce_${this.en}`);
}
useBoxJS = false;
today = '';
dataSource = [];
init = async () => {
try {
const today = new Date();
const month = today.getMonth() + 1;
const day = today.getDate();
this.today = `${month}-${day}`;
if (Keychain.contains(this.cacheName)) {
const dataSource = JSON.parse(Keychain.get(this.cacheName));
if (dataSource[this.today]) {
this.dataSource = dataSource[this.today].seasons;
} else {
this.dataSource = await this.getDramaList();
}
} else {
this.dataSource = await this.getDramaList();
}
} catch (e) {
console.log(e);
}
};
getDramaList = async () => {
const url = `https://bangumi.bilibili.com/web_api/timeline_global`;
const response = await this.$request.get(url);
try {
if (response.code === 0 && response.result.length > 0) {
const dataSource = response.result;
const result = dataSource.find((item) => item.date === this.today);
if (result) {
Keychain.set(
this.cacheName,
JSON.stringify({ [this.today]: result }),
);
return result.seasons;
}
}
return false;
} catch (e) {
return false;
}
};
setListCell = async (body, data) => {
let {
cover,
url,
title,
pub_time,
pub_index,
delay,
delay_reason,
delay_index,
} = data;
body.url = url;
const imageView = body.addStack();
imageView.size = new Size(89, 105);
imageView.cornerRadius = 5;
const image = await this.$request.get(cover, 'IMG');
imageView.backgroundImage = image;
imageView.borderWidth = 1;
imageView.borderColor = new Color(this.widgetColor.hex, 0.7);
const stackDesc = imageView.addStack();
stackDesc.layoutVertically();
const stackDescTop = stackDesc.addStack();
stackDescTop.setPadding(5, 0, 0, 0);
const textColor = new Color('#fff');
if (delay) pub_index = `${delay_index}「${delay_reason}」`;
stackDescTop.addSpacer();
const stackTopText = stackDescTop.addStack();
stackTopText.setPadding(0, 2, 0, 2);
stackTopText.backgroundColor = new Color('#000', 0.3);
stackTopText.cornerRadius = 4;
const subContent = stackTopText.addText(pub_index);
subContent.font = Font.boldSystemFont(10);
subContent.textColor = textColor;
subContent.lineLimit = 1;
stackDesc.addSpacer();
const stackDescBottom = stackDesc.addStack();
stackDescBottom.addSpacer();
stackDescBottom.backgroundColor = new Color('#000', 0.3);
const textView = stackDescBottom.addStack();
textView.setPadding(0, 10, 0, 10);
textView.size = new Size(89, 30);
textView.layoutVertically();
const descText = textView.addText(title);
descText.font = Font.boldSystemFont(10);
descText.textColor = textColor;
descText.lineLimit = 1;
const timerText = textView.addText(`更新:${pub_time}`);
timerText.font = Font.lightSystemFont(10);
timerText.textColor = textColor;
timerText.lineLimit = 1;
stackDescBottom.addSpacer();
return body;
};
fillRect(drawing, rect, color) {
const path = new Path();
path.addRoundedRect(rect, 0, 0);
drawing.addPath(path);
drawing.setFillColor(new Color(color, 1));
drawing.fillPath();
}
drawLine(drawing, rect, color, scale) {
const x1 = Math.round(rect.x + scale * 1.5);
const y1 = rect.y - scale;
const x2 = Math.round(rect.width + scale * 1.5);
const point1 = new Point(x1, y1);
const point2 = new Point(x2, y1);
const path = new Path();
path.move(point1);
path.addLine(point2);
drawing.addPath(path);
drawing.setStrokeColor(new Color(color, 1));
drawing.setLineWidth(60 / (40 + 15 * scale));
drawing.strokePath();
}
setLine = (stack, color) => {
try {
const topDrawing = new DrawContext();
topDrawing.size = new Size(642, 100);
topDrawing.opaque = false;
topDrawing.respectScreenScale = true;
const rectLine = new Rect(0, 70, 610, 26);
this.fillRect(topDrawing, rectLine, color);
for (let i = 0; i < 40; i++) {
this.drawLine(topDrawing, rectLine, color, i);
}
const stackLine = stack.addStack();
stack.backgroundImage = topDrawing.getImage();
stackLine.addSpacer();
const line = stackLine.addStack();
line.size = new Size(1, 10);
stackLine.addSpacer();
} catch (e) {
console.log(e);
}
};
setWidget = async (body, data) => {
const d = body.addStack();
d.addSpacer();
const container = d.addStack();
container.layoutVertically();
const dataSource = data.length > 3 ? [data.splice(0, 3), data] : [data];
let itemIndex = 0;
for (const item of dataSource) {
let listItem = container.addStack();
let index = 0;
for (const video of item) {
const stackItem = listItem.addStack();
await this.setListCell(stackItem, video);
index++;
if (item.length !== index) listItem.addSpacer(13);
}
itemIndex++;
if (dataSource.length !== itemIndex) container.addSpacer(13);
}
if (this.widgetFamily === 'large') {
container.addSpacer();
this.setLine(container, '#e8e8e8');
const timerColor = new Color(this.widgetColor.hex, 0.7);
const fontSize = 10;
container.addSpacer();
const stackFooter = container.addStack();
stackFooter.addSpacer();
const now = new Date();
const stackDate = stackFooter.addDate(
new Date(`${now.getFullYear()}/${now.getMonth() + 1}/${now.getDate()}`),
);
stackDate.textColor = timerColor;
stackDate.fontSize = fontSize;
stackDate.rightAlignText();
stackDate.applyTimerStyle();
}
d.addSpacer();
return body;
};
renderSmall = async (w) => {
const stack = w.addStack();
stack.addText('暂不支持');
return w;
};
renderLarge = async (w) => {
const dataSource = this.getRandomArrayElements(this.dataSource, 6);
return await this.setWidget(w, dataSource);
};
renderMedium = async (w) => {
const dataSource = this.getRandomArrayElements(this.dataSource, 3);
return await this.setWidget(w, dataSource);
};
/**
* 渲染函数,函数名固定
* 可以根据 this.widgetFamily 来判断小组件尺寸,以返回不同大小的内容
*/
async render() {
await this.init();
const widget = new ListWidget();
await this.getWidgetBackgroundImage(widget);
const header = widget.addStack();
if (this.widgetFamily !== 'small') {
await this.renderJDHeader(header);
} else {
await this.renderHeader(header, this.logo, this.name, this.widgetColor);
}
widget.addSpacer(10);
if (this.widgetFamily === 'medium') {
return await this.renderMedium(widget);
} else if (this.widgetFamily === 'large') {
return await this.renderLarge(widget);
} else {
return await this.renderSmall(widget);
}
}
renderJDHeader = async (header) => {
header.centerAlignContent();
await this.renderHeader(header, this.logo, this.name, this.widgetColor);
header.addSpacer();
const headerMore = header.addStack();
headerMore.url = '';
headerMore.setPadding(1, 10, 1, 10);
headerMore.cornerRadius = 10;
headerMore.backgroundColor = new Color('#fff', 0.5);
const textItem = headerMore.addText(this.today);
textItem.font = Font.boldSystemFont(12);
textItem.textColor = this.widgetColor;
textItem.lineLimit = 1;
textItem.rightAlignText();
return header;
};
}
// @组件代码结束
// await Runing(Widget, "", false); // 正式环境
await Runing(Widget, '', false); //远程开发环境