2023-02-12 08:40:49 +01:00
|
|
|
import { genericUserAgent, maxVideoDuration } from "../../config.js";
|
|
|
|
|
2023-02-13 14:44:58 +01:00
|
|
|
// TO-DO: quality picking, bilibili.tv support, and higher quality downloads (currently requires an account)
|
2024-02-15 02:42:15 +01:00
|
|
|
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}`, {
|
2023-02-12 08:40:49 +01:00
|
|
|
headers: { "user-agent": genericUserAgent }
|
|
|
|
}).then((r) => { return r.text() }).catch(() => { return false });
|
|
|
|
if (!html) return { error: 'ErrorCouldntFetch' };
|
|
|
|
if (!(html.includes('<script>window.__playinfo__=') && html.includes('"video_codecid"'))) return { error: 'ErrorEmptyDownload' };
|
|
|
|
|
|
|
|
let streamData = JSON.parse(html.split('<script>window.__playinfo__=')[1].split('</script>')[0]);
|
|
|
|
if (streamData.data.timelength > maxVideoDuration) return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] };
|
|
|
|
|
2023-08-22 21:03:31 +02:00
|
|
|
let video = streamData["data"]["dash"]["video"].filter(v =>
|
|
|
|
!v["baseUrl"].includes("https://upos-sz-mirrorcosov.bilivideo.com/")
|
|
|
|
).sort((a, b) => Number(b.bandwidth) - Number(a.bandwidth));
|
2023-02-12 08:40:49 +01:00
|
|
|
|
2023-08-22 21:03:31 +02:00
|
|
|
let audio = streamData["data"]["dash"]["audio"].filter(a =>
|
|
|
|
!a["baseUrl"].includes("https://upos-sz-mirrorcosov.bilivideo.com/")
|
|
|
|
).sort((a, b) => Number(b.bandwidth) - Number(a.bandwidth));
|
2023-02-12 08:40:49 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
urls: [video[0]["baseUrl"], audio[0]["baseUrl"]],
|
2024-02-15 02:42:15 +01:00
|
|
|
audioFilename: `bilibili_${id}_audio`,
|
|
|
|
filename: `bilibili_${id}_${video[0]["width"]}x${video[0]["height"]}.mp4`
|
2023-02-12 08:40:49 +01:00
|
|
|
};
|
|
|
|
}
|