backend/utils/metadata.js

57 lines
1.9 KiB
JavaScript
Raw Normal View History

2023-03-03 16:44:40 +00:00
const fetch = require('node-fetch')
2023-08-04 10:25:18 +01:00
async function getPipedInstance() {
2023-10-19 17:13:32 +01:00
const instances = await (await fetch('https://piped-instances.kavin.rocks/', {
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; PreserveTube/0.0; +https://preservetube.com)'
}
2023-10-19 17:13:32 +01:00
})).json()
2023-08-04 10:25:18 +01:00
return (instances[Math.floor(Math.random() * instances.length)]).api_url
}
2023-03-24 15:07:31 +00:00
async function getVideoMetadata(id) {
2024-04-04 21:06:33 +01:00
return await (await fetch(`${process.env.METADATA}/video/${id}`)).json()
}
async function getChannel(id) {
2024-04-04 21:06:33 +01:00
return await (await fetch(`${process.env.METADATA}/channel/${id}`)).json()
}
2023-03-24 14:20:06 +00:00
async function getChannelVideos(id) {
2024-04-04 21:06:33 +01:00
return await (await fetch(`${process.env.METADATA}/videos/${id}`)).json()
}
async function getPlaylistVideos(id) {
2023-08-04 10:25:18 +01:00
const instance = await getPipedInstance()
2023-10-19 17:13:32 +01:00
const json = await (await fetch(`${instance}/playlists/${id}`, {
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; PreserveTube/0.0; +https://preservetube.com)'
}
})).json()
2024-03-30 15:51:13 +00:00
return json
2023-03-03 16:44:40 +00:00
}
2024-06-29 11:16:36 +01:00
async function getVideoDownload(json, quality) {
const adaptiveFormats = json['streaming_data']['adaptive_formats'];
let video = adaptiveFormats.find(f => f.quality_label === `${quality}p` && !f.has_audio);
if (!video) { // stupid bullshit. basically finding the lowest quality if the quality specified isnt there
video = adaptiveFormats.filter(f => !f.has_audio).reduce((prev, current) => {
if (!prev || parseInt(current.quality_label) < parseInt(prev.quality_label)) {
return current;
}
return prev;
}, null);
2024-03-30 13:32:37 +00:00
}
2024-03-29 17:48:59 +00:00
2024-06-29 11:16:36 +01:00
const audio = adaptiveFormats.find(f => f.has_audio);
return {
url: [
video.url,
audio.url
]
};
2024-03-29 17:48:59 +00:00
}
2024-06-29 11:16:36 +01:00
module.exports = { getVideoMetadata, getChannel, getChannelVideos, getPlaylistVideos, getVideoDownload }