-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy pathTG频道.js
More file actions
executable file
·266 lines (226 loc) · 9.5 KB
/
TG频道.js
File metadata and controls
executable file
·266 lines (226 loc) · 9.5 KB
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
/*
@header({
searchable: 1,
filterable: 0,
quickSearch: 1,
title: 'TG频道',
author: 'EylinSir',
'类型': '影视',
logo: 'https://api.xinac.net/icon/?url=https://t.me',
lang: 'ds'
})
*/
const { req_proxy } = $.require('./_lib.request.js');
const MAX_CONCUR = 5;
let pageCache = { video: {}, search: {} };
let searchState = { key: '', idx: 0 };
const diskRules = {
'百度': { reg: /pan\.baidu\.com/, icon: '百度.png' },
'夸克': { reg: /pan\.quark\.cn/, icon: '夸克.png' },
'阿里': { reg: /www\.(aliyundrive|alipan)\.com/, icon: '阿里.png' },
'移动': { reg: /(yun|caiyun)\.139\.com/, icon: '移动.png' },
'天翼': { reg: /cloud\.189\.cn/, icon: '天翼.png' },
'115': { reg: /(www\.115|115cdn)\.com/, icon: '115.png' },
'UC': { reg: /pan\.uc\.cn|drive\.uc\.cn/, icon: 'UC.png' },
'123': { reg: /123pan\.(?:com|cn)|123(?:684|865|912|592)\.com/i, icon: '123.png' },
'磁力': { reg: /magnet:\?xt=urn:btih:[\da-fA-F]{32,40}/gi, icon: '磁力.png' }
};
const EPISODE_COMBINED_REGEX = /((?:更新至|全|第)\s*\d+\s*集)|((?:更新至|全|第)\s*[一二三四五六七八九十百千万亿]+\s*集)|((?:更至|更)\s*(?:EP)?\s*\d+)/;
const QUALITY_REGEX = /(SDR|HDR|HQ|4K|1080[Pp])/g;
const TITLE_FILTER_REG = /^[^\u4e00-\u9fa5A-Za-z0-9\(\[\{(【《「『〔〖〈﹝[]+/;
const TITLE_CLEAN_REG = /(名称:|资源标题:|名称[::])/g;
const EPISODE_CLEAN_REG = /(全|共)\s*\d+\s*集|\d+\s*集\s*全|更(新|至)?\s*\d+\s*集|第\s*\d+\s*-\s*\d+\s*集|更.*|剧情.*|(又名:.*)|(SDR|HDR|HQ|4K|1080[Pp])/g;
function getDiskInfo(url) {
if (diskRules.磁力.reg.test(url)) return { name: '磁力', icon: diskRules.磁力.icon };
const host = url.match(/^(?:https?:\/\/)?([^\/]+)/)?.[1] || '';
for (const [name, { reg, icon }] of Object.entries(diskRules)) {
if (name !== '磁力' && reg.test(host)) return { name, icon };
}
return null;
}
function cleanTitle(text) {
const titleLine = (text.split('\n').find(line => {
const trimLine = line.trim();
return !/^[A-Za-z]+\s+\d+$|https?:\/\/\S+/i.test(trimLine);
}) || text.split('\n')[0] || '未知资源').trim();
return titleLine
.replace(TITLE_CLEAN_REG, '')
.replace(TITLE_FILTER_REG, '')
.replace(EPISODE_CLEAN_REG, '')
.trim();
}
function getRemarks(text) {
const episodeMatch = text.match(EPISODE_COMBINED_REGEX);
const qualityInfo = (text.match(QUALITY_REGEX) || []).filter((v, i, a) => a.indexOf(v) === i);
const remarksParts = [];
if (episodeMatch) remarksParts.push(episodeMatch[0]);
if (qualityInfo.length) remarksParts.push(...qualityInfo);
return remarksParts.join(' ');
}
function addLinkToResults(url, ctx) {
const { diskCount, playUrls, playFroms, diskTypes, linkSet } = ctx;
const diskInfo = getDiskInfo(url);
if (!diskInfo) return;
linkSet.add(url);
diskCount[diskInfo.name] = (diskCount[diskInfo.name] || 0) + 1;
const fromName = diskCount[diskInfo.name] > 1 ? `${diskInfo.name}${diskCount[diskInfo.name]}` : diskInfo.name;
const playUrl = diskInfo.name === '磁力' ? url : `点击播放$push://${url.replace(/\#/g, '%23')}`;
playUrls.push(playUrl);
playFroms.push(fromName);
diskTypes.add(diskInfo.name);
}
function getPageUrl(baseUrl, cacheKey, cache) {
return cacheKey ? `${baseUrl}${cache[cacheKey] || ''}` : baseUrl;
}
const rule = {
类型: '影视',
title: 'TG频道',
author: 'EylinSir',
host: 'https://t.me',
url: '/s/fyclass',
searchUrl: '?q=**',
Pan_API: 'http://127.0.0.1:6080', // 网盘链接有效性检测过滤api,需自行替换
logo: 'https://api.xinac.net/icon/?url=https://t.me',
searchable: 1,
quickSearch: 1,
filterable: 0,
play_parse: true,
timeout: 18000,
hikerListCol: 'icon_4',
headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' },
class_parse: async function() {
return { class: JSON.parse(await request(this.params)) };
},
lazy: async function(flag, id) {
const realUrl = id.includes('$') ? id.split('$').slice(1).join('$').replace(/%23/g, '\#') : id;
return { url: realUrl };
},
parseMessages: async function(html) {
const { pdfa, pdfh } = this;
const $ = require('cheerio').load(html);
const messages = pdfa(html, '.tgme_widget_message') || [];
let results = [];
let allLinks = [];
for (const item of messages) {
const textHtml = pdfh(item, '.tgme_widget_message_text&&Html') || '';
const text = textHtml.replace(/<br\s*\/?>/gi, '\n').replace(/<[^>]+>/g, '').trim();
if (!text) continue;
const title = cleanTitle(text);
const linkCtx = {
diskCount: {},
playUrls: [],
playFroms: [],
diskTypes: new Set(),
linkSet: new Set()
};
[...pdfa(item, '.tgme_widget_message_text a'), ...pdfa(item, '.tgme_widget_message_inline_keyboard a')].forEach(a => {
const url = (pdfh(a, 'a&&href') || '').replace(/&\#$/, '').trim();
if (/^https?:\/\//.test(url)) addLinkToResults(url, linkCtx);
});
(text.match(diskRules.磁力.reg) || []).forEach(magnet => {
addLinkToResults(magnet.trim(), linkCtx);
});
if (linkCtx.playUrls.length === 0) continue;
let pic = '';
const photoStyle = $(item).find('.tgme_widget_message_photo_wrap').attr('style') || '';
const picMatch = photoStyle.match(/background-image\s*:\s*url\(['"]?([^'"]+)['"]?\)/i);
if (picMatch?.[1]) {
pic = picMatch[1];
} else if (linkCtx.diskTypes.size) {
const firstType = Array.from(linkCtx.diskTypes)[0];
pic = urljoin(this.publicUrl, `./images/icon_cookie/${diskRules[firstType]?.icon || '网盘.png'}`);
}
const dateTime = pdfh(item, '.tgme_widget_message_date time&&datetime') || '';
const year = dateTime.split('T')[0]?.substring(5) || '';
const remarks = getRemarks(text);
const vodYear = year + (linkCtx.diskTypes.size ? ` ${Array.from(linkCtx.diskTypes).join('/')}` : '');
const linkArr = Array.from(linkCtx.linkSet);
results.push({
vod_name: title,
vod_year: vodYear,
vod_remarks: remarks,
vod_pic: pic,
vod_id: JSON.stringify({
vod_name: title,
vod_pic: pic,
vod_content: text.split('\n').slice(1).join('\n'),
vod_play_from: linkCtx.playFroms.join('$$$'),
vod_play_url: linkCtx.playUrls.join('$$$')
}),
links: linkArr
});
allLinks.push(...linkArr);
}
if (allLinks.length) {
try {
const [magnetLinks, nonMagnetLinks] = [
allLinks.filter(link => diskRules.磁力.reg.test(link)),
allLinks.filter(link => !diskRules.磁力.reg.test(link))
];
const validLinks = new Set([...magnetLinks]);
const apiLinks = nonMagnetLinks;
if (apiLinks.length) {
const res = JSON.parse(await request(`${this.Pan_API}/api/v1/links/check`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ links: apiLinks })
}));
[...(res.valid_links || []), ...(res.pending_links || [])].forEach(link => validLinks.add(link));
}
results = results.filter(item => item.links.some(link => validLinks.has(link)));
} catch (e) {
console.error('链接校验失败:', e.message);
}
}
results.forEach(item => delete item.links);
const nextPage = $('link[rel="prev"]').attr('href')?.split('?')?.[1] || '';
return { results: results.reverse(), nextPage: nextPage ? `?${nextPage}` : "0" };
},
一级: async function() {
const { input, MY_PAGE } = this;
if (!input || (MY_PAGE !== 1 && (!pageCache.video[input] || pageCache.video[input] === "0"))) return [];
const url = getPageUrl(input, MY_PAGE !== 1 ? input : '', pageCache.video);
const html = await req_proxy(url, 'get', this.headers);
const { results, nextPage } = await this.parseMessages(html);
pageCache.video[input] = nextPage;
return results;
},
二级: function(ids) {
return JSON.parse(ids);
},
搜索: async function() {
const { KEY, MY_PAGE } = this;
if (!KEY) return [];
const { class: channels } = await this.class_parse();
if (!channels?.length) return [];
if (MY_PAGE === 1 || searchState.key !== KEY) {
pageCache.search = {};
searchState = { key: KEY, idx: 0 };
}
let { idx } = searchState;
if (idx >= channels.length && !Object.values(pageCache.search).some(p => p && p !== "0")) return [];
if (idx >= channels.length) idx = searchState.idx = 0;
const batch = channels.slice(idx, idx + MAX_CONCUR);
const results = (await Promise.all(batch.map(async channel => {
const chanName = channel.type_id?.replace(/^\//, '');
if (!chanName || pageCache.search[chanName] === "0") return [];
const baseUrl = `${this.host}/s/${chanName}?q=${encodeURIComponent(KEY)}`;
const url = getPageUrl(baseUrl, chanName, pageCache.search);
try {
const html = await req_proxy(url, 'get', this.headers);
const { results: chanRes, nextPage } = await this.parseMessages(html);
pageCache.search[chanName] = nextPage === "0" ? "0" : nextPage;
return chanRes.map(item => ({
...item,
vod_remarks: `${channel.type_name || chanName} ${item.vod_remarks}`.trim()
}));
} catch (e) {
console.error(`处理频道${chanName}失败:`, e.message);
pageCache.search[chanName] = "0";
return [];
}
}))).flat();
searchState.idx = idx + MAX_CONCUR;
return results;
}
};