forked from dompling/Scriptable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BiliBiliUp.js
215 lines (194 loc) · 6.41 KB
/
BiliBiliUp.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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: gray; icon-glyph: chalkboard;
// 添加require,是为了vscode中可以正确引入包,以获得自动补全等功能
if (typeof require === 'undefined') require = importModule;
const {DmYY, Runing} = require('./DmYY');
// @组件代码开始
class Widget extends DmYY {
constructor(arg) {
super(arg);
this.name = 'B站 UP 主';
this.en = 'BiliBiliUp';
this.inputValue = arg || '50952087';
this.Run();
}
useBoxJS = false;
ytInitialData = {};
videos = [];
baseUrl = 'https://api.bilibili.com/x';
url;
numberFormat(value) {
try {
const param = {};
let k = 10000;
const size = ['', '万', '亿', '万亿'];
let i;
if (value < k) {
param.value = value;
param.unit = '';
} else {
i = Math.floor(Math.log(value) / Math.log(k));
param.value = ((value / Math.pow(k, i))).toFixed(2);
param.unit = size[i];
}
return `${param.value}${param.unit}`;
} catch (e) {
console.log(e);
}
}
init = async () => {
try {
await this.getData();
await this.getRelationStat();
await this.getVideoList();
} catch (e) {
console.log('❌错误信息:' + e);
}
};
getData = async () => {
const url = `${this.baseUrl}/space/acc/info?mid=${this.inputValue}&jsonp=jsonp`;
const response = await this.$request.get(url);
if (response.code === 0) {
this.ytInitialData = response.data;
}
};
getRelationStat = async () => {
const url = `${this.baseUrl}/relation/stat?vmid=${this.inputValue}&jsonp=jsonp`;
const response = await this.$request.get(url);
if (response.code === 0) {
this.ytInitialData = {...this.ytInitialData, relation: response.data};
}
};
getVideoList = async () => {
const url = `${this.baseUrl}/space/arc/search?mid=${this.inputValue}&pn=1&ps=25&index=1&jsonp=jsonp`;
const response = await this.$request.get(url);
if (response.code === 0) {
this.ytInitialData = {
...this.ytInitialData,
videos: response.data.list.vlist.map(item => ({
thumb: item.pic,
title: item.title,
view: `${this.numberFormat(item.play)}次`,
url: item.bvid,
})),
};
}
};
setAvatar = async (stack) => {
stack.size = new Size(50, 50);
stack.cornerRadius = 5;
const {face} = this.ytInitialData;
const imgLogo = await this.$request.get(face, 'IMG');
const imgLogoItem = stack.addImage(imgLogo);
imgLogoItem.imageSize = new Size(50, 50);
return stack;
};
setTitleStack = (stack) => {
const textFormatNumber = this.textFormat.title;
textFormatNumber.color = this.backGroundColor;
const {name} = this.ytInitialData;
const title = name;
textFormatNumber.size =
title.length > 20 || this.widgetFamily === 'small' ? 16 : 20;
const titleItem = this.provideText(title, stack, textFormatNumber);
titleItem.lineLimit = 1;
};
setPathStack = (stack) => {
const textFormatNumber = this.textFormat.defaultText;
textFormatNumber.color = this.backGroundColor;
textFormatNumber.size = 12;
const {relation} = this.ytInitialData;
let simpleText = `关注数:${this.numberFormat(relation.follower)}`;
const titleItem = this.provideText(
simpleText,
stack,
textFormatNumber,
);
titleItem.lineLimit = 1;
titleItem.textOpacity = 0.9;
};
setFooterCell = async (stack) => {
const datas = this.getRandomArrayElements(this.ytInitialData.videos, 3);
for (let i = 0; i < datas.length; i++) {
if (i === 1) stack.addSpacer();
const video = datas[i];
const stackVideo = stack.addStack();
stackVideo.setPadding(10, 10, 10, 10);
stackVideo.url = 'https://www.bilibili.com/video/' + video.url;
stackVideo.backgroundColor = this.widgetColor;
stackVideo.centerAlignContent();
stackVideo.layoutVertically();
const img = await this.$request.get(video.thumb, 'IMG');
stackVideo.backgroundImage = await this.shadowImage(img, '#000', 0.3);
const title = {...this.textFormat.defaultText};
title.size = 8;
title.color = new Color('#fff');
this.provideText(video.title, stackVideo, title);
stackVideo.addSpacer();
title.color = new Color('#fff', 0.7);
this.provideText(video.view, stackVideo, title);
stackVideo.size = new Size(87, 56);
stackVideo.cornerRadius = 4;
if (i === 1) stack.addSpacer();
}
};
renderSmall = async (w) => {
return w;
};
renderMedium = async (w) => {
const stackBody = w.addStack();
stackBody.url = `https://space.bilibili.com/${this.inputValue}/`;
stackBody.layoutVertically();
const stackHeader = stackBody.addStack();
stackHeader.setPadding(5, 10, 5, 10);
stackHeader.cornerRadius = 10;
stackHeader.backgroundColor = this.widgetColor;
stackHeader.centerAlignContent();
const stackLeft = stackHeader.addStack();
await this.setAvatar(stackLeft);
stackHeader.addSpacer(20);
const stackRight = stackHeader.addStack();
stackRight.layoutVertically();
this.setTitleStack(stackRight);
stackRight.addSpacer(5);
this.setPathStack(stackRight);
stackHeader.addSpacer();
stackBody.addSpacer();
const stackFooter = stackBody.addStack();
stackFooter.setPadding(10, 0, 10, 0);
stackFooter.cornerRadius = 10;
stackFooter.backgroundColor = this.widgetColor;
stackFooter.addSpacer();
await this.setFooterCell(stackFooter);
stackFooter.addSpacer();
return w;
};
renderLarge = async (w) => {
return w;
};
Run() {
if (config.runsInApp) {
this.registerAction('基础设置', this.setWidgetConfig);
}
}
/**
* 渲染函数,函数名固定
* 可以根据 this.widgetFamily 来判断小组件尺寸,以返回不同大小的内容
*/
async render() {
await this.init();
const widget = new ListWidget();
await this.getWidgetBackgroundImage(widget);
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);
}
}
}
// @组件代码结束
// await Runing(Widget, "", true); // 正式环境
await Runing(Widget, args.widgetParameter, false); //远程开发环境