-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathTelegram.js
240 lines (216 loc) · 6.9 KB
/
Telegram.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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: pink; icon-glyph: paper-plane;
// 添加require,是为了vscode中可以正确引入包,以获得自动补全等功能
if (typeof require === 'undefined') require = importModule;
const {DmYY, Runing} = require('./DmYY');
// @组件代码开始
class Widget extends DmYY {
constructor(arg) {
super(arg);
this.name = 'Telegram';
this.en = 'Telegram';
this.inputValue = arg || 'Durov';
let _md5 = this.md5(module.filename + this.en);
this.CACHE_KEY = `cache_${_md5}_` + this.inputValue;
this.Run();
}
CACHE_KEY;
useBoxJS = false;
dataSource = {
footer: {},
};
init = async () => {
try {
await this.getData();
} catch (e) {
console.log(e);
}
};
getData = async () => {
try {
let data = await this.$request.get(
'https://t.me/s/' + this.inputValue,
'STRING',
);
data = data.match(
/tgme_channel_info_header">(.|\n)+tgme_channel_download_telegram"/,
)[0];
this.dataSource.logo = data.match(/https.+jpg/)[0];
this.dataSource.title = data.match(
/header_title"><span dir="auto">(.+)<\/span>/,
)[1];
let entities = this.dataSource.title.match(/&#\d{2,3};/g);
if (entities) {
for (let k in entities) {
let rExp = new RegExp(entities[k], 'g');
this.dataSource.title = this.dataSource.title.replace(
rExp,
this.entityToString(entities[k]),
);
}
}
let counters = data.match(/counter_value">.+?<\/span>/g);
let type = data.match(/counter_type">.+?<\/span>/g);
counters.forEach((item, index) => {
const value = item.match(/counter_value">(.+?)<\/span>/)[1];
const key = type[index].match(/counter_type">(.+?)<\/span>/)[1];
this.dataSource.footer[key] = value;
});
Keychain.set(this.CACHE_KEY, JSON.stringify(this.dataSource));
} catch (e) {
if (Keychain.contains(this.CACHE_KEY)) {
this.dataSource = Keychain.get(this.CACHE_KEY);
}
}
};
entityToString(entity) {
let entities = entity.split(';');
entities.pop();
return entities.map((item) =>
String.fromCharCode(
item[2] === 'x'
? parseInt(item.slice(3), 16)
: parseInt(item.slice(2)),
),
).join('');
}
setAvatar = async (stack) => {
stack.size = new Size(60, 60);
stack.backgroundColor = this.widgetColor;
stack.cornerRadius = 10;
try {
const {logo} = this.dataSource;
const imgLogo = await this.$request.get(logo, 'IMG');
const imgLogoItem = stack.addImage(imgLogo);
imgLogoItem.imageSize = new Size(60, 60);
} catch (e) {
console.log(e);
}
return stack;
};
setNumberStack = (stack, data) => {
stack.layoutVertically();
stack.addSpacer();
const textFormatNumber = this.textFormat.title;
textFormatNumber.color = this.widgetColor;
textFormatNumber.size = 18;
const stackTitle = stack.addStack();
stackTitle.addSpacer();
const valueItem = this.provideText(
data.value,
stackTitle,
textFormatNumber,
);
valueItem.lineLimit = 1;
stackTitle.addSpacer();
stack.addSpacer(5);
const textFormatDesc = this.textFormat.defaultText;
textFormatDesc.color = new Color('#aaaaaa');
textFormatDesc.size = 10;
const stackDesc = stack.addStack();
stackDesc.addSpacer();
const descItem = this.provideText(data.key, stackDesc, textFormatDesc);
descItem.lineLimit = 1;
stackDesc.addSpacer();
stack.addSpacer();
return stack;
};
setTitleStack = (stack) => {
const textFormatNumber = this.textFormat.title;
textFormatNumber.color = this.widgetColor;
const title = this.dataSource.title;
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 = new Color('#2481cc');
textFormatNumber.size = 12;
const titleItem = this.provideText(
`@${this.inputValue}`,
stack,
textFormatNumber,
);
titleItem.lineLimit = 1;
};
renderSmall = async (w) => {
const stackBody = w.addStack();
stackBody.url = `tg://resolve?domain=${this.inputValue}`;
stackBody.layoutVertically();
stackBody.addSpacer();
const stackHeader = stackBody.addStack();
const stackLeft = stackHeader.addStack();
await this.setAvatar(stackLeft);
stackHeader.addSpacer(5);
const stackRight = stackHeader.addStack();
Object.keys(this.dataSource.footer).forEach((key, index) => {
if (index === 0) {
const value = this.dataSource.footer[key];
this.setNumberStack(stackRight, {key, value});
}
});
stackBody.addSpacer();
const stackFooter = stackBody.addStack();
stackFooter.layoutVertically();
this.setTitleStack(stackFooter);
stackFooter.addSpacer(5);
this.setPathStack(stackFooter);
stackBody.addSpacer();
return w;
};
renderMedium = async (w) => {
const stackBody = w.addStack();
stackBody.url = `https://t.me/${this.inputValue}`;
stackBody.layoutVertically();
stackBody.addSpacer();
const stackHeader = stackBody.addStack();
const stackLeft = stackHeader.addStack();
await this.setAvatar(stackLeft);
stackHeader.addSpacer(20);
const stackRight = stackHeader.addStack();
stackRight.layoutVertically();
stackRight.addSpacer(5);
this.setTitleStack(stackRight);
stackRight.addSpacer(5);
this.setPathStack(stackRight);
stackBody.addSpacer();
const stackFooter = stackBody.addStack();
Object.keys(this.dataSource.footer).forEach((key) => {
const value = this.dataSource.footer[key];
const stack = stackFooter.addStack();
this.setNumberStack(stack, {key, value});
});
stackBody.addSpacer();
return w;
};
renderLarge = async (w) => {
return await this.renderMedium(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); //远程开发环境