1
0
Fork 0
mirror of https://github.com/wukko/cobalt.git synced 2025-04-06 10:11:38 +02:00
cobalt/api/src/modules/processing/services/vk.js

56 lines
1.9 KiB
JavaScript
Raw Normal View History

import { genericUserAgent, env } from "../../config.js";
2023-10-13 21:52:15 +06:00
import { cleanString } from "../../sub/utils.js";
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
html = await fetch(`https://vk.com/video${o.userId}_${o.videoId}`, {
headers: { "user-agent": genericUserAgent }
}).then(r => r.arrayBuffer()).catch(() => {});
2023-08-20 22:12:09 +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);
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-26 22:49:25 +06:00
if (Number(js.mvData.is_active_live) !== 0) return { error: 'ErrorLiveVideo' };
if (js.mvData.duration > env.durationLimit)
return { error: ['ErrorLengthLimit', env.durationLimit / 60] };
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-08-20 22:12:09 +06:00
if (Number(quality) > Number(o.quality)) quality = o.quality;
url = js.player.params[0][`url${quality}`];
2023-10-13 21:52:15 +06:00
let fileMetadata = {
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 {
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' }
}