backend/utils/metadata.js

62 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-03-03 16:44:40 +00:00
const fetch = require('node-fetch')
const maxRetries = 5
2023-03-03 16:44:40 +00:00
2023-08-04 09:25:18 +00:00
async function getPipedInstance() {
2023-10-19 16:13:32 +00: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 16:13:32 +00:00
})).json()
2023-08-04 09:25:18 +00: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 20:06:33 +00:00
return await (await fetch(`${process.env.METADATA}/video/${id}`)).json()
}
async function getChannel(id) {
2024-04-04 20:06:33 +00: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 20:06:33 +00:00
return await (await fetch(`${process.env.METADATA}/videos/${id}`)).json()
}
async function getPlaylistVideos(id) {
2023-08-04 09:25:18 +00:00
const instance = await getPipedInstance()
2023-10-19 16:13:32 +00: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-03-29 17:48:59 +00:00
async function getVideoDownload(url, quality) {
2024-03-30 13:32:37 +00:00
let json
for (let retries = 0; retries < maxRetries; retries++) {
try {
2024-04-04 18:27:51 +00:00
json = await (await fetch('http://gluetun:9000/api/json', {
2024-03-30 13:32:37 +00:00
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
'url': url,
'vQuality': quality
})
})).json()
if (json.error) continue
return json
} catch (error) {
continue
}
}
2024-03-29 17:48:59 +00:00
return json
}
module.exports = { getVideoMetadata, getChannel, getChannelVideos, getPlaylistVideos, getVideoDownload }