Skip to content

Commit 3a5a219

Browse files
develon2015root
authored andcommitted
feat: 添加X.com多视频解析支持
1 parent bc66d66 commit 3a5a219

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

index.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function main() {
6666

6767
let y2b = url.match(/^https?:\/\/(?:youtu.be\/|(?:www|m).youtube.com\/(?:watch|shorts)(?:\/|\?v=))([\w-]{11})$/);
6868
let bilibili = url.match(/^https?:\/\/(?:www\.|m\.)?bilibili\.com\/video\/([\w\d]{11,14})\/?(?:\?p=(\d+))?$/);
69-
let xcom = url.match(/^https?:\/\/(?:www)?x\.com\/[^\/]+\/status\/(\d+)$/);
69+
let xcom = url.match(/^https?:\/\/(?:www)?x\.com\/[^\/]+\/status\/(\d+)(?:\/video\/(\d+))?$/);
7070
let website;
7171
switch (true) {
7272
case y2b != null:
@@ -94,7 +94,7 @@ function main() {
9494
// console.log(JSON.stringify(msg, null, 1));
9595
res.send(msg);
9696
});
97-
thread.postMessage({ op: 'parse', website, url, videoID: (y2b || bilibili || xcom)[1], p: bilibili?.[2] });
97+
thread.postMessage({ op: 'parse', website, url, videoID: (y2b || bilibili || xcom)[1], p: (bilibili || xcom)?.[2] });
9898
});
9999

100100
let queue = [];
@@ -367,25 +367,39 @@ function task() {
367367
} // case subtitle end
368368

369369
case 'parse': {
370+
let { website, url, videoID, p } = msg;
370371
let audios = [], videos = [];
371372
let bestAudio = {}, bestVideo = {};
372373

373374
let rs = { title: '', thumbnail: '', formats: [] };
374375
try {
375-
let cmd = `yt-dlp --print-json --skip-download ${config.cookie !== undefined ? `--cookies ${config.cookie}` : ''} '${msg.url}' 2> /dev/null`
376+
let cmd = `yt-dlp --print-json --skip-download ${config.cookie !== undefined ? `--cookies ${config.cookie}` : ''} '${getWebsiteUrl(website, videoID, p)}' 2> /dev/null`
376377
console.log('解析视频, 命令:', cmd);
377-
rs = child_process.execSync(cmd, { maxBuffer: 10 * 1024 * 1024 }).toString(); // 10MB 缓冲区
378-
try {
379-
rs = JSON.parse(rs);
380-
} catch (error) {
381-
let cmd = `yt-dlp --print-json --skip-download ${config.cookie !== undefined ? `--cookies ${config.cookie}` : ''} '${msg.url}?p=1' 2> /dev/null`;
382-
console.log('尝试分P, 命令:', cmd);
383-
rs = child_process.execSync(cmd).toString();
384-
rs = JSON.parse(rs);
385-
msg.p = '1';
386-
msg.url = `${msg.url}?p=1`;
378+
rs = child_process.execSync(cmd, { maxBuffer: 10 * 1024 * 1024 }).toString(); // 10MB 缓冲区
379+
switch (website) {
380+
case 'xcom': {
381+
let tmp = rs.split('\n').filter(it => it != '');
382+
rs = JSON.parse(tmp[p ? (Number(p) - 1) : 0]);
383+
if (!p && tmp.length > 1) {
384+
msg.p = '1';
385+
msg.url = getWebsiteUrl(website, videoID, msg.p);
386+
}
387+
break;
388+
}
389+
default: {
390+
try {
391+
rs = JSON.parse(rs);
392+
} catch (error) {
393+
let cmd = `yt-dlp --print-json --skip-download ${config.cookie !== undefined ? `--cookies ${config.cookie}` : ''} '${msg.url}?p=1' 2> /dev/null`;
394+
console.log('尝试分P, 命令:', cmd);
395+
rs = child_process.execSync(cmd).toString();
396+
rs = JSON.parse(rs);
397+
msg.p = '1';
398+
msg.url = `${msg.url}?p=1`;
399+
}
400+
console.log('解析完成:', rs.title, msg.url);
401+
}
387402
}
388-
console.log('解析完成:', rs.title, msg.url);
389403
} catch (error) {
390404
console.log(error.toString());
391405
worker_threads.parentPort.postMessage({
@@ -438,10 +452,13 @@ function task() {
438452
let cmd = //`cd '${__dirname}' && (cd tmp > /dev/null || (mkdir tmp && cd tmp)) &&` +
439453
`yt-dlp ${config.cookie !== undefined ? `--cookies ${config.cookie}` : ''} ${getWebsiteUrl(website, videoID, p)} -f ${format.replace('x', '+')} ` +
440454
`-o '${fullpath}/${videoID}.%(ext)s' ${recode !== undefined ? `--recode ${recode}` : ''} -k --write-info-json`;
455+
// if (website == 'xcom') cmd += ` --playlist-items ${p ?? 1}`; // 可能导致dest捕获异常
456+
if (website == 'xcom') cmd += ` --no-playlist`;
441457
console.log('下载视频, 命令:', cmd);
442458
try {
443459
let dest = 'Unknown dest';
444460
let ps = child_process.execSync(cmd).toString().split('\n');
461+
// let regex = new RegExp(`^\\[download\\].*${fullpath}/(${videoID}\\.[\\w]+).*$`);
445462
let regex = new RegExp(`^.*${fullpath}/(${videoID}\\.[\\w]+).*$`);
446463
ps.forEach(it => {
447464
console.log(it);

utils.js

100755100644
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ function getWebsiteUrl(website, id, p) {
88
return `https://youtu.be/${id}`;
99
case 'bilibili':
1010
return `https://www.bilibili.com/video/${id}${p ? `?p=${p}` : ''}`;
11+
case 'xcom':
12+
if (p)
13+
return `https://x.com/i/status/${id}/video/${p ? p : 1}`;
14+
return `https://x.com/i/status/${id}`;
1115
}
1216
}
1317

0 commit comments

Comments
 (0)