forked from yydfys/CatPawOpen
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathffm3u8.js
More file actions
280 lines (269 loc) · 9.05 KB
/
ffm3u8.js
File metadata and controls
280 lines (269 loc) · 9.05 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
import * as HLS from 'hls-parser';
import req from '../../util/req.js';
let url = '';
let categories = [];
async function request(reqUrl) {
let res = await req(reqUrl, {
method: 'get',
});
return res.data;
}
async function init(inReq, _outResp) {
url = inReq.server.config.ffm3u8.url;
categories = inReq.server.config.ffm3u8.categories;
return {};
}
async function home(_inReq, _outResp) {
const data = await request(url);
let classes = [];
for (const cls of data.class) {
const n = cls.type_name.toString().trim();
if (categories && categories.length > 0) {
if (categories.indexOf(n) < 0) continue;
}
classes.push({
type_id: cls.type_id.toString(),
type_name: n,
});
}
if (categories && categories.length > 0) {
classes = classes.sort((a, b) => {
return categories.indexOf(a.type_name) - categories.indexOf(b.type_name);
});
}
return {
class: classes,
};
}
async function category(inReq, _outResp) {
const tid = inReq.body.id;
const pg = inReq.body.page;
let page = pg || 1;
if (page == 0) page = 1;
const data = await request(url + `?ac=detail&t=${tid}&pg=${page}`);
let videos = [];
for (const vod of data.list) {
videos.push({
vod_id: vod.vod_id.toString(),
vod_name: vod.vod_name.toString(),
vod_pic: vod.vod_pic,
vod_remarks: vod.vod_remarks,
});
}
return {
page: parseInt(data.page),
pagecount: data.pagecount,
total: data.total,
list: videos,
};
}
async function detail(inReq, _outResp) {
const ids = !Array.isArray(inReq.body.id) ? [inReq.body.id] : inReq.body.id;
const videos = [];
for (const id of ids) {
const data = (await request(url + `?ac=detail&ids=${id}`)).list[0];
let vod = {
vod_id: data.vod_id,
vod_name: data.vod_name,
vod_pic: data.vod_pic,
type_name: data.type_name,
vod_year: data.vod_year,
vod_area: data.vod_area,
vod_remarks: data.vod_remarks,
vod_actor: data.vod_actor,
vod_director: data.vod_director,
vod_content: data.vod_content.trim(),
vod_play_from: data.vod_play_from,
vod_play_url: data.vod_play_url,
};
videos.push(vod);
}
return {
list: videos,
};
}
async function proxy(inReq, outResp) {
const what = inReq.params.what;
const purl = decodeURIComponent(inReq.params.ids);
if (what == 'hls') {
const resp = await req(purl, {
method: 'get',
});
const plist = HLS.parse(resp.data);
if (plist.variants) {
for (const v of plist.variants) {
if (!v.uri.startsWith('http')) {
v.uri = new URL(v.uri, purl).toString();
}
}
plist.variants.map((variant) => {
variant.uri = inReq.server.prefix + '/proxy/hls/' + encodeURIComponent(variant.uri) + '/.m3u8';
});
}
if (plist.segments) {
for (const s of plist.segments) {
if (!s.uri.startsWith('http')) {
s.uri = new URL(s.uri, purl).toString();
}
if (s.key && s.key.uri && !s.key.uri.startsWith('http')) {
s.key.uri = new URL(s.key.uri, purl).toString();
}
}
plist.segments.map((seg) => {
seg.uri = inReq.server.prefix + '/proxy/ts/' + encodeURIComponent(seg.uri) + '/.ts';
});
}
const hls = HLS.stringify(plist);
let hlsHeaders = {};
if (resp.headers['content-length']) {
Object.assign(hlsHeaders, resp.headers, { 'content-length': hls.length.toString() });
} else {
Object.assign(hlsHeaders, resp.headers);
}
delete hlsHeaders['transfer-encoding'];
delete hlsHeaders['cache-control'];
if (hlsHeaders['content-encoding'] == 'gzip') {
delete hlsHeaders['content-encoding'];
}
outResp.code(resp.status).headers(hlsHeaders);
return hls;
} else {
outResp.redirect(purl);
return;
}
}
async function play(inReq, _outResp) {
const id = inReq.body.id;
if (id.indexOf('.m3u8') < 0) {
const sniffer = await inReq.server.messageToDart({
action: 'sniff',
opt: {
url: id,
timeout: 10000,
rule: 'http((?!http).){12,}?\\.m3u8(?!\\?)',
},
});
if (sniffer && sniffer.url) {
const hds = {};
if (sniffer.headers) {
if (sniffer.headers['user-agent']) {
hds['User-Agent'] = sniffer.headers['user-agent'];
}
if (sniffer.headers['referer']) {
hds['Referer'] = sniffer.headers['referer'];
}
}
return {
parse: 0,
url: sniffer.url,
header: hds,
};
}
}
return {
parse: 0,
url: inReq.server.address().dynamic + inReq.server.prefix + '/proxy/hls/' + encodeURIComponent(id) + '/.m3u8',
};
}
async function search(inReq, _outResp) {
const wd = inReq.body.wd;
const data = await request(url + `?ac=detail&wd=${wd}`);
let videos = [];
for (const vod of data.list) {
videos.push({
vod_id: vod.vod_id.toString(),
vod_name: vod.vod_name.toString(),
vod_pic: vod.vod_pic,
vod_remarks: vod.vod_remarks,
});
}
return {
page: parseInt(data.page),
pagecount: data.pagecount,
total: data.total,
list: videos,
};
}
async function test(inReq, outResp) {
try {
const printErr = function (json) {
if (json.statusCode && json.statusCode == 500) {
console.error(json);
}
};
const prefix = inReq.server.prefix;
const dataResult = {};
let resp = await inReq.server.inject().post(`${prefix}/init`);
dataResult.init = resp.json();
printErr(resp.json());
resp = await inReq.server.inject().post(`${prefix}/home`);
dataResult.home = resp.json();
printErr(resp.json());
if (dataResult.home.class.length > 0) {
resp = await inReq.server.inject().post(`${prefix}/category`).payload({
id: dataResult.home.class[0].type_id,
page: 1,
filter: true,
filters: {},
});
dataResult.category = resp.json();
printErr(resp.json());
if (dataResult.category.list.length > 0) {
resp = await inReq.server.inject().post(`${prefix}/detail`).payload({
id: dataResult.category.list[0].vod_id, // dataResult.category.list.map((v) => v.vod_id),
});
dataResult.detail = resp.json();
printErr(resp.json());
if (dataResult.detail.list && dataResult.detail.list.length > 0) {
dataResult.play = [];
for (const vod of dataResult.detail.list) {
const flags = vod.vod_play_from.split('$$$');
const ids = vod.vod_play_url.split('$$$');
for (let j = 0; j < flags.length; j++) {
const flag = flags[j];
const urls = ids[j].split('#');
for (let i = 0; i < urls.length && i < 2; i++) {
resp = await inReq.server
.inject()
.post(`${prefix}/play`)
.payload({
flag: flag,
id: urls[i].split('$')[1],
});
dataResult.play.push(resp.json());
}
}
}
}
}
}
resp = await inReq.server.inject().post(`${prefix}/search`).payload({
wd: '爱',
page: 1,
});
dataResult.search = resp.json();
printErr(resp.json());
return dataResult;
} catch (err) {
console.error(err);
outResp.code(500);
return { err: err.message, tip: 'check debug console output' };
}
}
export default {
meta: {
key: 'ffm3u8',
name: '非凡采集',
type: 3,
},
api: async (fastify) => {
fastify.post('/init', init);
fastify.post('/home', home);
fastify.post('/category', category);
fastify.post('/detail', detail);
fastify.post('/play', play);
fastify.post('/search', search);
fastify.get('/proxy/:what/:ids/:end', proxy);
fastify.get('/test', test);
},
};