-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaolusm-mini-library.js
More file actions
526 lines (474 loc) · 14.6 KB
/
Copy pathtaolusm-mini-library.js
File metadata and controls
526 lines (474 loc) · 14.6 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// @name TaoluSM Mini Library
const TAOLUSM_DEFAULT_BASE = 'https://taolusm.com';
const TAOLUSM_LOGO = 'https://taolusm.com/favicon.ico';
const TAOLUSM_UA =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1 Safari/605.1.15';
const WidgetMetadata = {
id: 'taolusm-mini-library',
name: '套路SM',
title: '套路SM',
version: '1.0.0',
author: 'EL',
logo: TAOLUSM_LOGO,
icon: TAOLUSM_LOGO,
site: TAOLUSM_DEFAULT_BASE,
description: '套路SM 自定义媒体库,支持首页、分类、搜索、详情和播放解析。'
};
const TAOLUSM_SECTIONS = [
{ id: 'latest', title: '最新视频', path: '/', style: 'discover.spotlight' },
{ id: 'zhibo', title: '直播回放', path: '/zhibo', style: 'discover.ranked' },
{ id: 'juqing', title: '剧情调教', path: '/juqing', style: 'discover.posterCompact' },
{ id: 'japanese-korean', title: '日韩视频', path: '/japanese-korean', style: 'discover.posterCompact' },
{ id: 'western', title: '欧美视频', path: '/western', style: 'discover.posterCompact' },
{ id: 'free', title: '免费试看', path: '/free', style: 'discover.posterCompact' }
];
function getManifest() {
return {
id: WidgetMetadata.id,
name: WidgetMetadata.name,
title: WidgetMetadata.title,
version: WidgetMetadata.version,
author: WidgetMetadata.author,
logo: WidgetMetadata.logo,
icon: WidgetMetadata.icon,
site: WidgetMetadata.site,
description: WidgetMetadata.description,
capabilities: {
home: true,
category: true,
detail: true,
search: true,
playback: true,
resourceVersions: true,
aggregation: true,
playbackHistory: true,
resourceMatching: false,
resourceMatch: {
enabled: false,
parameters: ['title', 'alternativeTitles', 'mediaType']
}
},
aggregation: {
search: true,
playbackHistory: true,
resourceMatching: false
},
parameters: [
{
name: 'baseURL',
title: '站点地址',
type: 'input',
defaultValue: TAOLUSM_DEFAULT_BASE,
required: true,
description: '套路SM 当前可访问域名。'
}
]
};
}
async function getHome(ctx) {
let latestItems = [];
try {
const html = await fetchText(ctx, categoryURL(ctx, '/'));
latestItems = parseListHtml(ctx, html).slice(0, 18);
} catch (error) {
latestItems = [];
}
return {
pageType: 'home',
id: 'taolusm-home',
title: '套路SM',
heroAspectRatio: '16:9',
hero: latestItems.slice(0, 8).map(toWideItem),
sections: [
{
id: 'taolusm-categories',
title: '分类',
style: 'discover.annualCategories',
lazy: false,
items: TAOLUSM_SECTIONS.map(function (section) {
return categoryCard(ctx, section);
})
},
{
id: 'latest',
title: '最新视频',
style: 'discover.spotlight',
lazy: false,
moreAction: categoryAction(ctx, TAOLUSM_SECTIONS[0]),
items: latestItems
}
].concat(TAOLUSM_SECTIONS.slice(1).map(function (section) {
return sectionShell(ctx, section);
}))
};
}
async function getHomeSection(ctx) {
const sectionId = stringValue(ctx && (ctx.sectionId || ctx.id));
const section = findSection(sectionId) || TAOLUSM_SECTIONS[0];
try {
const html = await fetchText(ctx, categoryURL(ctx, section.path));
return {
id: section.id,
title: section.title,
style: section.style,
lazy: false,
moreAction: categoryAction(ctx, section),
items: parseListHtml(ctx, html).slice(0, 18)
};
} catch (error) {
return emptySection(section.id, section.title, section.style, error);
}
}
async function getCategory(ctx) {
const page = positiveInt(contextValue(ctx, 'page'), 1);
const pageId = normalizePageId(ctx && (ctx.pageId || ctx.id || ctx.category || ctx.genreId));
const section = findSection(pageId) || TAOLUSM_SECTIONS[0];
const url = pagedURL(categoryURL(ctx, section.path), page);
const html = await fetchText(ctx, url);
const items = parseListHtml(ctx, html);
return {
pageType: 'category',
id: section.id,
title: section.title,
style: 'media.posterGrid',
itemAspectRatio: '16:9',
items: items,
page: page,
hasMore: hasNextPage(html, page)
};
}
async function getDetail(ctx) {
const id = itemIdFromContext(ctx);
if (!id) throw new Error('套路SM 详情参数无效');
const detailURL = baseURL(ctx) + '/v/' + encodeURIComponent(id);
const html = await fetchText(ctx, detailURL);
const title = cleanTitle(firstNonEmpty(
metaContent(html, 'property', 'og:title'),
pageTitle(html),
id
));
const poster = absoluteURL(ctx, firstNonEmpty(
metaContent(html, 'property', 'og:image'),
metaContent(html, 'name', 'twitter:image'),
pickImageForId(html, id),
pickImage(html)
));
const overview = cleanText(firstNonEmpty(
metaContent(html, 'name', 'description'),
metaContent(html, 'property', 'og:description')
));
const recommendations = parseListHtml(ctx, html)
.filter(function (item) { return item.id !== id; })
.slice(0, 18);
const headers = mediaHeaders(ctx);
return {
id: id,
title: title,
type: 'movie',
poster: poster,
backdrop: poster,
imageHeaders: headers,
posterHeaders: headers,
backdropHeaders: headers,
detailImageAspectRatio: '16:9',
overview: overview,
resourceGroups: resourceGroupsFor(ctx, id, title),
recommendations: recommendations.length ? [
{
id: 'related',
title: '相关推荐',
style: 'discover.standard',
items: recommendations
}
] : []
};
}
function getResourceVersions(ctx) {
const id = itemIdFromContext(ctx);
if (!id) return [];
return resourceGroupsFor(ctx, id, stringValue(ctx && ctx.title) || '在线播放');
}
function resolvePlayback(ctx) {
const id = itemIdFromContext(ctx);
const url = stringValue(ctx && ctx.url) || (id ? downloadURL(ctx, id) : '');
if (!url) throw new Error('套路SM 播放参数无效');
return {
url: url,
container: 'mp4',
headers: mediaHeaders(ctx),
isLive: false,
streamKind: 'file'
};
}
async function search(ctx) {
const query = cleanText(contextValue(ctx, 'query') || contextValue(ctx, 'keyword') || contextValue(ctx, 'text'));
const page = positiveInt(contextValue(ctx, 'page'), 1);
if (!query) {
return {
pageType: 'search',
title: '搜索结果',
items: [],
page: page,
hasMore: false
};
}
const url = pagedURL(baseURL(ctx) + '/search/' + encodeURIComponent(query), page);
const html = await fetchText(ctx, url);
const items = parseListHtml(ctx, html);
return {
pageType: 'search',
title: query,
style: 'media.posterGrid',
itemAspectRatio: '16:9',
items: items,
page: page,
hasMore: hasNextPage(html, page)
};
}
function onSearch(ctx) {
return search(ctx);
}
function parseListHtml(ctx, html) {
const items = [];
const seen = {};
const cardRegex = /<a\b[^>]*href=["']([^"']*\/v\/(\d+)[^"']*)["'][^>]*title=["']([^"']*)["'][^>]*>[\s\S]*?<\/a>/gi;
let match;
while ((match = cardRegex.exec(html || '')) !== null) {
const block = match[0];
const id = match[2];
if (!id || seen[id]) continue;
seen[id] = true;
const title = decodeEntities(match[3]).trim() || id;
const thumb = absoluteURL(ctx, firstNonEmpty(
firstMatch(block, /data-src=["']([^"']*covers\/\d+\.webp[^"']*)["']/i),
firstMatch(block, /src=["']([^"']*covers\/\d+\.webp[^"']*)["']/i),
firstMatch(block, /data-src=["']([^"']+)["']/i),
firstMatch(block, /src=["']([^"']+)["']/i)
));
const duration = firstMatch(block, /right-1[^>]*>\s*(\d+:\d+(?::\d+)?)\s*</i);
const views = firstMatch(block, /left-1[^>]*>[\s\S]*?<\/svg>\s*<span>\s*([\d.]+[KMk]?)\s*<\/span>/i);
const subtitleParts = [];
if (duration) subtitleParts.push(duration);
if (views) subtitleParts.push('观看 ' + views);
items.push({
id: id,
title: title,
subtitle: subtitleParts.join(' · '),
type: 'movie',
poster: thumb,
backdrop: thumb,
imageHeaders: mediaHeaders(ctx),
aspectRatio: '16:9',
durationText: duration || undefined,
remarks: duration || undefined,
metadataText: views ? '观看 ' + views : undefined,
action: {
type: 'detail',
itemId: id,
id: id,
title: title
}
});
}
return items;
}
function resourceGroupsFor(ctx, id, title) {
return [
{
id: 'download',
title: '在线播放',
versions: [
{
id: 'download-' + id,
name: '默认线路',
title: title || '默认线路',
subtitle: '站点下载直链',
url: downloadURL(ctx, id),
container: 'mp4',
default: true,
headers: mediaHeaders(ctx)
}
]
}
];
}
function categoryCard(ctx, section) {
return {
id: 'category-' + section.id,
title: section.title,
type: 'category',
aspectRatio: '16:9',
action: categoryAction(ctx, section)
};
}
function categoryAction(ctx, section) {
return {
type: 'category',
id: section.id,
pageId: section.id,
title: section.title,
url: categoryURL(ctx, section.path),
itemAspectRatio: '16:9'
};
}
function sectionShell(ctx, section) {
return {
id: section.id,
title: section.title,
style: section.style,
lazy: true,
loadAction: {
type: 'custom',
id: section.id,
sectionId: section.id,
title: section.title
},
moreAction: categoryAction(ctx, section),
items: []
};
}
function emptySection(id, title, style, error) {
return {
id: id,
title: title,
style: style || 'discover.standard',
lazy: false,
items: [],
error: stringValue(error && (error.message || error))
};
}
function toWideItem(item) {
const copy = {};
Object.keys(item).forEach(function (key) {
copy[key] = item[key];
});
copy.aspectRatio = '16:9';
return copy;
}
async function fetchText(ctx, url) {
const response = await Widget.http.get(url, {
headers: mediaHeaders(ctx)
});
const data = response && (response.data || response.body || response.text);
if (!data) throw new Error('请求失败: ' + url);
return String(data);
}
function mediaHeaders(ctx) {
return {
Referer: baseURL(ctx) + '/',
'User-Agent': TAOLUSM_UA
};
}
function baseURL(ctx) {
const value = stringValue(contextValue(ctx, 'baseURL') || TAOLUSM_DEFAULT_BASE).replace(/\/+$/, '');
return value || TAOLUSM_DEFAULT_BASE;
}
function categoryURL(ctx, path) {
if (/^https?:\/\//i.test(path || '')) return path;
const normalized = path && path.charAt(0) === '/' ? path : '/' + (path || '');
return baseURL(ctx) + normalized;
}
function downloadURL(ctx, id) {
return baseURL(ctx) + '/download/' + encodeURIComponent(id);
}
function pagedURL(url, page) {
if (page <= 1) return url;
return url + (url.indexOf('?') >= 0 ? '&' : '?') + 'page=' + encodeURIComponent(page);
}
function findSection(id) {
const normalized = normalizePageId(id);
for (let i = 0; i < TAOLUSM_SECTIONS.length; i += 1) {
if (TAOLUSM_SECTIONS[i].id === normalized) return TAOLUSM_SECTIONS[i];
}
return null;
}
function normalizePageId(id) {
const value = stringValue(id);
if (value === 'japaneseKorean') return 'japanese-korean';
return value.replace(/^category-/, '') || 'latest';
}
function itemIdFromContext(ctx) {
return stringValue(ctx && (ctx.itemId || ctx.id || ctx.link || ctx.vid || ctx.videoId)).replace(/\D+/g, '');
}
function hasNextPage(html, page) {
if (!html) return false;
const nextPage = page + 1;
const escaped = String(nextPage).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const patterns = [
new RegExp('[?&]page=' + escaped + '(["\'&<#\\s])', 'i'),
new RegExp('>\\s*' + escaped + '\\s*<', 'i'),
/rel=["']next["']/i
];
return patterns.some(function (pattern) { return pattern.test(html); });
}
function metaContent(html, attr, value) {
const re = new RegExp("<meta[^>]*" + attr + "=[\"']" + escapeRegExp(value) + "[\"'][^>]*content=[\"']([^\"']*)[\"'][^>]*>", 'i');
const reversed = new RegExp("<meta[^>]*content=[\"']([^\"']*)[\"'][^>]*" + attr + "=[\"']" + escapeRegExp(value) + "[\"'][^>]*>", 'i');
return decodeEntities(firstMatch(html, re) || firstMatch(html, reversed));
}
function pageTitle(html) {
return decodeEntities(firstMatch(html, /<title[^>]*>([\s\S]*?)<\/title>/i));
}
function pickImageForId(html, id) {
return firstMatch(html, new RegExp("(?:data-src|src)=[\"']([^\"']*covers/" + escapeRegExp(id) + "\\.webp[^\"']*)[\"']", 'i'));
}
function pickImage(html) {
return firstMatch(html, /(?:data-src|src)=["']([^"']+\.(?:webp|jpg|jpeg|png)[^"']*)["']/i);
}
function firstMatch(text, pattern) {
const match = pattern.exec(text || '');
return match ? match[1] || '' : '';
}
function firstNonEmpty() {
for (let i = 0; i < arguments.length; i += 1) {
const value = stringValue(arguments[i]);
if (value) return value;
}
return '';
}
function cleanTitle(text) {
return cleanText(text).replace(/\s*-\s*.*$/, '').trim();
}
function cleanText(text) {
return decodeEntities(stripTags(stringValue(text))).replace(/\s+/g, ' ').trim();
}
function stripTags(text) {
return stringValue(text).replace(/<[^>]+>/g, ' ');
}
function decodeEntities(text) {
return stringValue(text)
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, "'")
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/&#(\d+);/g, function (_, code) {
return String.fromCharCode(Number(code));
});
}
function absoluteURL(ctx, url) {
const value = stringValue(url);
if (!value) return '';
if (/^https?:\/\//i.test(value)) return value;
if (value.indexOf('//') === 0) return 'https:' + value;
if (value.charAt(0) === '/') return baseURL(ctx) + value;
return baseURL(ctx) + '/' + value;
}
function contextValue(ctx, key) {
if (!ctx || !key) return '';
if (ctx[key] !== undefined && ctx[key] !== null) return ctx[key];
if (ctx.params && ctx.params[key] !== undefined && ctx.params[key] !== null) return ctx.params[key];
if (ctx.parameters && ctx.parameters[key] !== undefined && ctx.parameters[key] !== null) return ctx.parameters[key];
return '';
}
function positiveInt(value, fallback) {
const number = parseInt(value, 10);
return Number.isFinite(number) && number > 0 ? number : fallback;
}
function stringValue(value) {
if (value === undefined || value === null) return '';
return String(value).trim();
}
function escapeRegExp(text) {
return stringValue(text).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}