-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathtgs.js
277 lines (262 loc) · 9.58 KB
/
tgs.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
const cheerio = createCheerio()
/*
{
"channels": [
"QuarkFree",
"ucpanpan",
]
}
*/
let $config = argsify($config_str)
const UA = 'MOBILE_UA'
let appConfig = {
ver: 1,
title: 'tg搜索',
site: 'https://t.me/s/',
// tabs: [
// {
// name: '只能搜索',
// ui: 1,
// ext: {
// id: '',
// },
// },
// ],
}
async function getConfig() {
let config = appConfig
config.tabs = []
let channels = $config.channels
channels.forEach((e) => {
config.tabs.push({
name: e,
// ui: 1,
ext: {
channel: e,
},
})
})
return jsonify(config)
}
async function getCards(ext) {
ext = argsify(ext)
let cards = []
let page = ext.page || 1
let url = `${appConfig.site}${ext.channel}`
let headers = {
'User-Agent': UA,
}
try {
if (page > 1) {
let config = argsify($cache.get('tgs-card-config'))
if (config && config.hasMore) {
url = config.nextPage
headers['Referer'] = `${appConfig.site}${ext.channel}`
headers['X-Requested-With'] = 'XMLHttpRequest'
} else
return jsonify({
list: cards,
})
}
let { data } = await $fetch.get(url, {
headers,
})
if (page > 1) {
data = data.slice(1, -1).replaceAll('\\"', '"').replaceAll('\\n', '').replaceAll('\\/', '/')
}
const $ = cheerio.load(data)
if ($('div.tgme_widget_message_bubble').length === 0) return
$('div.tgme_widget_message_bubble').each((_, element) => {
let title = ''
let hrefs = []
let cover = ''
let remarks = ''
try {
const html = $(element).find('.tgme_widget_message_text').html().replace(/<b[^>]*>|<\/b>|<a[^>]*>|<\/a>|<mark[^>]*>|<\/mark>|<i[^>]*>|<\/i>|<div[^>]*>|<\/div>/g, '').replace(/【[^】]*】/g, '')
html.split('<br>').forEach((e) => {
const titletext = e.trim()
if (/(名称|名字|短剧|资源标题)(:|:)/.test(titletext)) {
title = titletext
.split(/:|:/)[1]
.trim()
//如果第一字符是[则匹配第二个[
if (title.startsWith('[')) {
title = title.split('][')[0].replace('[', '')
} else { title = title.split(/(|\(|\[|(更新?至|全)/)[0] }
} else if (/(|\(|\[|(更新?至|全)/.test(titletext)) {
title = titletext.split(/(|\(|\[|(更新?至|全)/)[0]
} else if (/(.+)\s(更新?至|全)/.test(titletext)) {
title = titletext.split(/更新?至|全/)[0]
} else if (/S\d+/.test(titletext)) {
title = titletext.split('S')[0]
}
})
title = title.replace(/<b>/, '').replace(/4K.*$/g, '').replace('发行时间', '').replace('描述', '').trim()
$(element)
.find('.tgme_widget_message_text > a')
.each((_, element) => {
const href = $(element).attr('href').replace('anxia.com', '115.com')
if (href.match(/https:\/\/(.+)\/(s|t)\/(.+)/)) {
hrefs.push(href)
}
})
cover = $(element)
.find('.tgme_widget_message_photo_wrap')
.attr('style')
.match(/image\:url\('(.+)'\)/)[1]
remarks = hrefs[0].match(/https:\/\/(.+)\/(s|t)\//)[1].replace(/(115\.com)|(anxia\.com)/, '115')
.replace(/(pan\.quark\.cn)/, '夸克')
.replace(/(drive\.uc\.cn)/, 'UC')
.replace(/(www\.aliyundrive\.com)|(www\.alipan\.com)/, '阿里')
.replace(/(cloud\.189\.cn)/, '天翼')
.trim();
} catch (e) {
// $utils.toastError(`${ext.channel}搜索失败`)
}
if (remarks === '') return
cards.push({
vod_id: hrefs[0],
vod_name: title,
vod_pic: cover,
vod_remarks: remarks,
ext: {
url: hrefs,
name: title,
},
})
})
let nextPage = $('.js-messages_more_wrap a').attr('href')
nextPage = nextPage ? `https://t.me${nextPage}` : ''
if (nextPage) {
let config = {
hasMore: true,
nextPage,
}
$cache.set('tgs-card-config', jsonify(config))
} else {
$cache.set(
'tgs-card-config',
jsonify({
hasMore: false,
})
)
}
return jsonify({
list: cards.reverse(),
})
} catch (error) {
$print(error)
}
}
async function getTracks(ext) {
ext = argsify(ext)
let tracks = []
let urls = ext.url
const name = ext.name
urls.forEach((url) => {
tracks.push({
name: name,
pan: url,
})
})
return jsonify({
list: [
{
title: '默认分组',
tracks,
},
],
})
}
async function getPlayinfo(ext) {
return jsonify({ urls: [] })
}
async function search(ext) {
ext = argsify(ext)
let cards = []
let page = ext.page || 1
if (page > 1) {
return jsonify({
list: [],
})
}
let text = encodeURIComponent(ext.text)
const requests = $config.channels.map(async (channel) => {
const url = `${appConfig.site}${channel}?q=${text}`;
try {
const { data } = await $fetch.get(url, {
headers: {
'User-Agent': UA,
},
});
const $ = cheerio.load(data)
if ($('div.tgme_widget_message_bubble').length === 0) return
$('div.tgme_widget_message_bubble').each((_, element) => {
let title = ''
let hrefs = []
let cover = ''
let remarks = ''
try {
const html = $(element).find('.tgme_widget_message_text').html().replace(/<b[^>]*>|<\/b>|<a[^>]*>|<\/a>|<mark[^>]*>|<\/mark>|<i[^>]*>|<\/i>|<div[^>]*>|<\/div>/g, '').replace(/【[^】]*】/g, '')
html.split('<br>').forEach((e) => {
const titletext = e.trim()
if (/(名称|名字|短剧|资源标题)(:|:)/.test(titletext)) {
title = titletext
.split(/:|:/)[1]
.trim()
//如果第一字符是[则匹配第二个[
if (title.startsWith('[')) {
title = title.split('][')[0].replace('[', '')
} else { title = title.split(/(|\(|\[|(更新?至|全)/)[0] }
} else if (/(|\(|\[|(更新?至|全)/.test(titletext)) {
title = titletext.split(/(|\(|\[|(更新?至|全)/)[0]
} else if (/(.+)\s(更新?至|全)/.test(titletext)) {
title = titletext.split(/更新?至|全/)[0]
} else if (/S\d+/.test(titletext)) {
title = titletext.split('S')[0]
}
})
title = title.replace(/<b>/, '').replace(/4K.*$/g, '').replace('发行时间', '').replace('描述', '').trim()
$(element)
.find('.tgme_widget_message_text > a')
.each((_, element) => {
const href = $(element).attr('href')
if (href.match(/https:\/\/(.+)\/(s|t)\/(.+)/)) {
hrefs.push(href)
}
})
cover = $(element)
.find('.tgme_widget_message_photo_wrap')
.attr('style')
.match(/image\:url\('(.+)'\)/)[1]
remarks = hrefs[0].match(/https:\/\/(.+)\/(s|t)\//)[1].replace(/(115\.com)|(anxia\.com)/, '115')
.replace(/(pan\.quark\.cn)/, '夸克')
.replace(/(drive\.uc\.cn)/, 'UC')
.replace(/(www\.aliyundrive\.com)|(www\.alipan\.com)/, '阿里')
.replace(/(cloud\.189\.cn)/, '天翼')
.trim();
} catch (e) {
//$utils.toastError(`${channel}搜索失败`)
}
if (remarks === '') return
cards.push({
vod_id: hrefs[0],
vod_name: title,
vod_pic: cover,
vod_remarks: remarks,
vod_duration: channel,
ext: {
url: hrefs,
name: title,
},
})
})
} catch (error) {
//
}
});
await Promise.all(requests);
return jsonify({
list: cards,
})
}