2024-08-03 14:47:13 +06:00
|
|
|
import { cleanString } from "../../misc/utils.js";
|
2024-08-20 21:10:37 +06:00
|
|
|
import { genericUserAgent, env } from "../../config.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}`, {
|
2024-08-20 21:10:37 +06:00
|
|
|
headers: {
|
|
|
|
"user-agent": genericUserAgent
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(r => r.arrayBuffer())
|
|
|
|
.catch(() => {});
|
2023-08-20 22:12:09 +06:00
|
|
|
|
2024-08-20 21:10:37 +06:00
|
|
|
if (!html) return { error: "fetch.fail" };
|
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);
|
|
|
|
|
2024-08-20 21:10:37 +06:00
|
|
|
if (!html.includes(`{"lang":`)) return { error: "fetch.empty" };
|
2023-02-12 13:40:49 +06:00
|
|
|
|
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
|
|
|
|
2024-08-20 21:10:37 +06:00
|
|
|
if (Number(js.mvData.is_active_live) !== 0) {
|
|
|
|
return { error: "content.video.live" };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (js.mvData.duration > env.durationLimit) {
|
2024-08-24 16:56:07 +06:00
|
|
|
return { error: "content.too_long" };
|
2024-08-20 21:10:37 +06:00
|
|
|
}
|
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
|
|
|
}
|
2024-08-20 21:10:37 +06:00
|
|
|
return { error: "fetch.empty" }
|
2023-02-12 13:40:49 +06:00
|
|
|
}
|