forked from dompling/Scriptable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBiliBili.js
181 lines (164 loc) · 5.29 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
// 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/task/master/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 (cell, data) => {
let {
cover,
url,
title,
pub_time,
pub_index,
delay,
delay_reason,
delay_index,
} = data;
let body = cell.addStack();
body.url = url;
if (this.widgetFamily !== "small") {
const imageView = body.addStack();
imageView.size = new Size(43, 43);
imageView.cornerRadius = 5;
const image = await this.$request.get(cover, "IMG");
imageView.backgroundImage = image;
body.addSpacer(10);
}
const textView = body.addStack();
textView.layoutVertically();
const descText = textView.addText(title);
descText.font = Font.boldSystemFont(14);
descText.textColor = this.widgetColor;
descText.lineLimit = 1;
textView.addSpacer(3);
if (delay) pub_index = `${delay_index}「${delay_reason}」`;
const subContent = textView.addText(pub_index);
subContent.font = Font.boldSystemFont(10);
subContent.textColor = this.widgetColor;
subContent.lineLimit = 1;
const timerText = textView.addText(`更新时间:${pub_time}`);
timerText.font = Font.lightSystemFont(10);
timerText.textColor = this.widgetColor;
timerText.lineLimit = 1;
return cell;
};
setWidget = async (body, size) => {
const container = body.addStack();
container.layoutVertically();
const dataSource = this.getRandomArrayElements(this.dataSource, size);
for (let index = 0; index < dataSource.length; index++) {
const data = dataSource[index];
let listItem = container.addStack();
await this.setListCell(listItem, data);
container.addSpacer(10);
}
body.addSpacer();
return body;
};
renderSmall = async (w) => {
return await this.setWidget(w, 2);
};
renderLarge = async (w) => {
return await this.setWidget(w, 5);
};
renderMedium = async (w) => {
return await this.setWidget(w, 2);
};
/**
* 渲染函数,函数名固定
* 可以根据 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); //远程开发环境