Skip to content

Commit 73eae16

Browse files
committed
api/bilibili: add support for video parts/episodes
different videos share the same id, kind of weird
1 parent 1d5db46 commit 73eae16

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

api/src/processing/service-config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const services = {
77
bilibili: {
88
patterns: [
99
"video/:comId",
10+
"video/:comId?p=:partId",
1011
"_shortLink/:comShortLink",
1112
"_tv/:lang/video/:tvId",
1213
"_tv/video/:tvId"

api/src/processing/service-patterns.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
export const testers = {
22
"bilibili": pattern =>
3-
pattern.comId?.length <= 12 || pattern.comShortLink?.length <= 16
4-
|| pattern.tvId?.length <= 24,
3+
pattern.comId?.length <= 12 ||
4+
pattern.comShortLink?.length <= 16 ||
5+
pattern.tvId?.length <= 24 ||
6+
pattern.comId?.length <= 12 && pattern.partId?.length <= 3,
57

68
"dailymotion": pattern => pattern.id?.length <= 32,
79

api/src/processing/services/bilibili.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@ function extractBestQuality(dashData) {
1717
return [ bestVideo, bestAudio ];
1818
}
1919

20-
async function com_download(id) {
21-
const html = await fetch(`https://bilibili.com/video/${id}`, {
20+
async function com_download(id, partId) {
21+
const url = new URL(`https://bilibili.com/video/${id}`);
22+
23+
if (partId) {
24+
url.searchParams.set('p', partId);
25+
}
26+
27+
const html = await fetch(url, {
2228
headers: {
2329
"user-agent": genericUserAgent
2430
}
@@ -47,10 +53,15 @@ async function com_download(id) {
4753
return { error: "fetch.empty" };
4854
}
4955

56+
let filenameBase = `bilibili_${id}`;
57+
if (partId) {
58+
filenameBase += `_${partId}`;
59+
}
60+
5061
return {
5162
urls: [video.baseUrl, audio.baseUrl],
52-
audioFilename: `bilibili_${id}_audio`,
53-
filename: `bilibili_${id}_${video.width}x${video.height}.mp4`,
63+
audioFilename: `${filenameBase}_audio`,
64+
filename: `${filenameBase}_${video.width}x${video.height}.mp4`,
5465
};
5566
}
5667

@@ -89,14 +100,14 @@ async function tv_download(id) {
89100
};
90101
}
91102

92-
export default async function({ comId, tvId, comShortLink }) {
103+
export default async function({ comId, tvId, comShortLink, partId }) {
93104
if (comShortLink) {
94105
const patternMatch = await resolveRedirectingURL(`https://b23.tv/${comShortLink}`);
95106
comId = patternMatch?.comId;
96107
}
97108

98109
if (comId) {
99-
return com_download(comId);
110+
return com_download(comId, partId);
100111
} else if (tvId) {
101112
return tv_download(tvId);
102113
}

api/src/processing/url.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ function cleanURL(url) {
147147
limitQuery('v');
148148
}
149149
break;
150+
case "bilibili":
150151
case "rutube":
151152
if (url.searchParams.get('p')) {
152153
limitQuery('p');

api/src/util/tests/bilibili.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,14 @@
5656
"code": 200,
5757
"status": "tunnel"
5858
}
59+
},
60+
{
61+
"name": "bilibili.com link with part id",
62+
"url": "https://www.bilibili.com/video/BV1uo4y1K72s?spm_id_from=333.788.videopod.episodes&p=6",
63+
"params": {},
64+
"expected": {
65+
"code": 200,
66+
"status": "tunnel"
67+
}
5968
}
6069
]

0 commit comments

Comments
 (0)