From b441b494eff7366ee64212cc99d5daaca966fc57 Mon Sep 17 00:00:00 2001 From: wukko Date: Sun, 20 Aug 2023 22:12:09 +0600 Subject: [PATCH] fix vk downloads --- package.json | 1 - src/modules/processing/services/vk.js | 55 +++++++++------------------ src/test/tests.json | 10 +++++ 3 files changed, 27 insertions(+), 39 deletions(-) diff --git a/package.json b/package.json index 0ed6132f..5f15447a 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,6 @@ "set-cookie-parser": "2.6.0", "undici": "^5.19.1", "url-pattern": "1.0.3", - "xml-js": "^1.6.11", "youtubei.js": "^5.4.0" } } diff --git a/src/modules/processing/services/vk.js b/src/modules/processing/services/vk.js index dc1ad39d..995d0132 100644 --- a/src/modules/processing/services/vk.js +++ b/src/modules/processing/services/vk.js @@ -1,59 +1,38 @@ -import { xml2json } from "xml-js"; import { genericUserAgent, maxVideoDuration } from "../../config.js"; -const representationMatch = { - "2160": 7, - "1440": 6, - "1080": 5, - "720": 4, - "480": 3, - "360": 2, - "240": 1, - "144": 0 -}, resolutionMatch = { - "3840": "2160", - "2560": "1440", - "1920": "1080", - "1280": "720", - "852": "480", - "640": "360", - "426": "240", - // "256": "144" -} +const resolutions = ["2160", "1440", "1080", "720", "480", "360", "240"]; export default async function(o) { - let html, url, filename = `vk_${o.userId}_${o.videoId}_`; + 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 }); + if (!html) return { error: 'ErrorCouldntFetch' }; if (!html.includes(`{"lang":`)) return { error: 'ErrorEmptyDownload' }; - let quality = o.quality === "max" ? 7 : representationMatch[o.quality], - js = JSON.parse('{"lang":' + html.split(`{"lang":`)[1].split(']);')[0]); + let js = JSON.parse('{"lang":' + html.split(`{"lang":`)[1].split(']);')[0]); if (Number(js.mvData.is_active_live) !== 0) return { error: 'ErrorLiveVideo' }; if (js.mvData.duration > maxVideoDuration / 1000) return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] }; - if (js.player.params[0]["manifest"]) { - let mpd = JSON.parse(xml2json(js.player.params[0]["manifest"], { compact: true, spaces: 4 })), - repr = mpd.MPD.Period.AdaptationSet.Representation ? mpd.MPD.Period.AdaptationSet.Representation : mpd.MPD.Period.AdaptationSet[0]["Representation"], - bestQuality = repr[repr.length - 1], - resolutionPick = Number(bestQuality._attributes.width) > Number(bestQuality._attributes.height) ? 'width': 'height'; - - if (Number(bestQuality._attributes.id) > Number(quality)) bestQuality = repr[quality]; - - url = js.player.params[0][`url${resolutionMatch[bestQuality._attributes[resolutionPick]]}`]; - filename += `${bestQuality._attributes.width}x${bestQuality._attributes.height}.mp4` - - } else if (js.player.params[0]["url240"]) { // fallback for when video is too old - url = js.player.params[0]["url240"]; - filename += `320x240.mp4` + for (let i in resolutions) { + if (js.player.params[0][`url${resolutions[i]}`]) { + quality = resolutions[i]; + break + } } + 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 - }; + } return { error: 'ErrorEmptyDownload' } } diff --git a/src/test/tests.json b/src/test/tests.json index 3efc01df..34155509 100644 --- a/src/test/tests.json +++ b/src/test/tests.json @@ -510,6 +510,16 @@ "code": 200, "status": "stream" } + }, { + "name": "4k video", + "url": "https://vk.com/video-1112285_456248465", + "params": { + "vQuality": "max" + }, + "expected": { + "code": 200, + "status": "stream" + } }, { "name": "ancient video (fallback to 240p)", "url": "https://vk.com/video-1959_28496479",