Skip to content

best way to save what you love

bilibili: add support for b23.tv links

dumbmoron6e1eddad82cc
diff --git a/src/modules/processing/match.js b/src/modules/processing/match.js
index cc2516f2..95558c8b 100644
--- a/src/modules/processing/match.js
+++ b/src/modules/processing/match.js
@@ -56,9 +56,7 @@ export default async function(host, patternMatch, url, lang, obj) {
});
break;
case "bilibili":
- r = await bilibili({
- id: patternMatch.id.slice(0, 12)
- });
+ r = await bilibili(patternMatch);
break;
case "youtube":
let fetchInfo = {
diff --git a/src/modules/processing/services/bilibili.js b/src/modules/processing/services/bilibili.js
index 0194ee46..5664e8cb 100644
--- a/src/modules/processing/services/bilibili.js
+++ b/src/modules/processing/services/bilibili.js
@@ -1,8 +1,23 @@
import { genericUserAgent, maxVideoDuration } from "../../config.js";
// TO-DO: quality picking, bilibili.tv support, and higher quality downloads (currently requires an account)
-export default async function(obj) {
- let html = await fetch(`https://bilibili.com/video/${obj.id}`, {
+export default async function({ id, shortLink }) {
+ if (shortLink) {
+ id = await fetch(`https://b23.tv/${shortLink}`, { redirect: 'manual' })
+ .then(r => r.status > 300 && r.status < 400 && r.headers.get('location'))
+ .then(url => {
+ const path = new URL(url).pathname;
+ if (path.startsWith('/video/'))
+ return path.split('/')[2];
+ })
+ .catch(() => {})
+ }
+
+ if (!id) {
+ return { error: 'ErrorCouldntFetch' };
+ }
+
+ let html = await fetch(`https://bilibili.com/video/${id}`, {
headers: { "user-agent": genericUserAgent }
}).then((r) => { return r.text() }).catch(() => { return false });
if (!html) return { error: 'ErrorCouldntFetch' };
@@ -21,7 +36,7 @@ export default async function(obj) {
return {
urls: [video[0]["baseUrl"], audio[0]["baseUrl"]],
- audioFilename: `bilibili_${obj.id}_audio`,
- filename: `bilibili_${obj.id}_${video[0]["width"]}x${video[0]["height"]}.mp4`
+ audioFilename: `bilibili_${id}_audio`,
+ filename: `bilibili_${id}_${video[0]["width"]}x${video[0]["height"]}.mp4`
};
}
diff --git a/src/modules/processing/servicesConfig.json b/src/modules/processing/servicesConfig.json
index 804f5978..bec1dab2 100644
--- a/src/modules/processing/servicesConfig.json
+++ b/src/modules/processing/servicesConfig.json
@@ -3,7 +3,7 @@
"config": {
"bilibili": {
"alias": "bilibili.com videos",
- "patterns": ["video/:id"],
+ "patterns": ["video/:id", "_shortLink/:shortLink"],
"enabled": true
},
"reddit": {
diff --git a/src/modules/processing/servicesPatternTesters.js b/src/modules/processing/servicesPatternTesters.js
index 970e8f40..f3418445 100644
--- a/src/modules/processing/servicesPatternTesters.js
+++ b/src/modules/processing/servicesPatternTesters.js
@@ -1,6 +1,6 @@
export const testers = {
"bilibili": (patternMatch) =>
- patternMatch.id?.length <= 12,
+ patternMatch.id?.length <= 12 || patternMatch.shortLink?.length <= 16,
"instagram": (patternMatch) =>
patternMatch.postId?.length <= 12
diff --git a/src/modules/processing/url.js b/src/modules/processing/url.js
index 9c87889d..753e9d5d 100644
--- a/src/modules/processing/url.js
+++ b/src/modules/processing/url.js
@@ -16,6 +16,7 @@ export function aliasURL(url) {
url.search = `?v=${encodeURIComponent(parts[2])}`
}
break;
+
case "youtu":
if (url.hostname === 'youtu.be' && parts.length >= 2) {
/* youtu.be urls can be weird, e.g. https://youtu.be/<id>//asdasd// still works
@@ -25,6 +26,7 @@ export function aliasURL(url) {
}`)
}
break;
+
case "pin":
if (url.hostname === 'pin.it' && parts.length === 2) {
url = new URL(`https://pinterest.com/url_shortener/${
@@ -46,6 +48,12 @@ export function aliasURL(url) {
url = new URL(`https://twitch.tv/_/clip/${parts[1]}`);
}
break;
+
+ case "b23":
+ if (url.hostname === 'b23.tv' && parts.length === 2) {
+ url = new URL(`https://bilibili.com/_shortLink/${parts[1]}`)
+ }
+ break;
}
return url
diff --git a/src/test/tests.json b/src/test/tests.json
index a298d152..3161b00d 100644
--- a/src/test/tests.json
+++ b/src/test/tests.json
@@ -746,6 +746,14 @@
"code": 200,
"status": "stream"
}
+ }, {
+ "name": "b23.tv shortlink",
+ "url": "https://b23.tv/lbMyOI9",
+ "params": {},
+ "expected": {
+ "code": 200,
+ "status": "stream"
+ }
}],
"tumblr": [{
"name": "at.tumblr link",