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

39 lines
1.3 KiB
JavaScript
Raw Normal View History

2023-02-26 22:49:25 +06:00
import { genericUserAgent, maxVideoDuration } from "../../config.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-08-20 22:12:09 +06:00
let html, url,
quality = o.quality === "max" ? 2160 : o.quality,
filename = `vk_${o.userId}_${o.videoId}_`;
html = await fetch(`https://vk.com/video${o.userId}_${o.videoId}`, {
headers: { "user-agent": genericUserAgent }
}).then((r) => { return r.text() }).catch(() => { return false });
2023-08-20 22:12:09 +06:00
if (!html) return { error: 'ErrorCouldntFetch' };
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 > maxVideoDuration / 1000) return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] };
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}`];
filename += `${quality}p.mp4`
if (url && filename) return {
urls: url,
filename: filename
2023-08-20 22:12:09 +06:00
}
2023-02-26 22:49:25 +06:00
return { error: 'ErrorEmptyDownload' }
}