2024-05-16 20:57:48 +06:00
|
|
|
import { genericUserAgent, env } from "../../config.js";
|
2023-10-13 21:52:15 +06:00
|
|
|
import { cleanString } from "../../sub/utils.js";
|
2023-02-12 13:40:49 +06:00
|
|
|
|
2023-08-20 22:12:09 +06:00
|
|
|
const resolutions = ["2160", "1440", "1080", "720", "480", "360", "240"];
|
2023-02-26 22:49:25 +06:00
|
|
|
|
|
|
|
export default async function(o) {
|
2023-10-13 21:52:15 +06:00
|
|
|
let html, url, quality = o.quality === "max" ? 2160 : o.quality;
|
2023-08-20 22:12:09 +06:00
|
|
|
|
2023-03-08 13:17:33 +06:00
|
|
|
html = await fetch(`https://vk.com/video${o.userId}_${o.videoId}`, {
|
2023-02-12 13:40:49 +06:00
|
|
|
headers: { "user-agent": genericUserAgent }
|
2024-05-16 21:28:42 +06:00
|
|
|
}).then(r => r.arrayBuffer()).catch(() => {});
|
2023-08-20 22:12:09 +06:00
|
|
|
|
2023-02-12 13:40:49 +06:00
|
|
|
if (!html) return { error: 'ErrorCouldntFetch' };
|
2023-10-13 21:52:15 +06:00
|
|
|
|
2024-01-17 13:02:52 +06:00
|
|
|
// decode cyrillic from windows-1251 because vk still uses apis from prehistoric times
|
2023-10-13 21:52:15 +06:00
|
|
|
let decoder = new TextDecoder('windows-1251');
|
|
|
|
html = decoder.decode(html);
|
|
|
|
|
2023-02-12 13:40:49 +06:00
|
|
|
if (!html.includes(`{"lang":`)) return { error: 'ErrorEmptyDownload' };
|
|
|
|
|
2023-08-20 22:12:09 +06:00
|
|
|
let js = JSON.parse('{"lang":' + html.split(`{"lang":`)[1].split(']);')[0]);
|
2023-02-12 13:40:49 +06:00
|
|
|
|
2023-02-26 22:49:25 +06:00
|
|
|
if (Number(js.mvData.is_active_live) !== 0) return { error: 'ErrorLiveVideo' };
|
2024-05-16 21:28:42 +06:00
|
|
|
if (js.mvData.duration > env.durationLimit)
|
|
|
|
return { error: ['ErrorLengthLimit', env.durationLimit / 60] };
|
2023-02-12 13:40:49 +06:00
|
|
|
|
2023-08-20 22:12:09 +06:00
|
|
|
for (let i in resolutions) {
|
|
|
|
if (js.player.params[0][`url${resolutions[i]}`]) {
|
|
|
|
quality = resolutions[i];
|
|
|
|
break
|
|
|
|
}
|
2023-04-29 21:30:59 +06:00
|
|
|
}
|
2023-08-20 22:12:09 +06:00
|
|
|
if (Number(quality) > Number(o.quality)) quality = o.quality;
|
|
|
|
|
|
|
|
url = js.player.params[0][`url${quality}`];
|
2023-04-29 21:30:59 +06:00
|
|
|
|
2023-10-13 21:52:15 +06:00
|
|
|
let fileMetadata = {
|
2023-10-15 14:39:17 +06:00
|
|
|
title: cleanString(js.player.params[0].md_title.trim()),
|
2024-01-17 13:02:52 +06:00
|
|
|
author: cleanString(js.player.params[0].md_author.trim()),
|
2023-10-13 21:52:15 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (url) return {
|
2023-04-29 21:30:59 +06:00
|
|
|
urls: url,
|
2023-10-13 21:52:15 +06:00
|
|
|
filenameAttributes: {
|
|
|
|
service: "vk",
|
|
|
|
id: `${o.userId}_${o.videoId}`,
|
|
|
|
title: fileMetadata.title,
|
2024-01-17 13:02:52 +06:00
|
|
|
author: fileMetadata.author,
|
2023-10-13 21:52:15 +06:00
|
|
|
resolution: `${quality}p`,
|
|
|
|
qualityLabel: `${quality}p`,
|
|
|
|
extension: "mp4"
|
|
|
|
}
|
2023-08-20 22:12:09 +06:00
|
|
|
}
|
2023-02-26 22:49:25 +06:00
|
|
|
return { error: 'ErrorEmptyDownload' }
|
2023-02-12 13:40:49 +06:00
|
|
|
}
|