backend/utils/metadata.js

66 lines
2.1 KiB
JavaScript
Raw Normal View History

2023-03-03 16:44:40 +00:00
const fetch = require('node-fetch')
async function getInstance() {
2023-03-10 16:34:39 +00:00
for (let i = 0; i < 5; i++) {
const instance = await actuallyGettingInstance()
if (instance) return instance
}
async function actuallyGettingInstance() {
const instance = await getRandomInstance()
const test = await testInstance(instance)
if (test) return instance
else {
return false
}
}
async function getRandomInstance() {
const instances = await (await fetch('https://piped-instances.kavin.rocks/')).json()
const list = instances.filter(i => !i.cdn)
return (list[Math.floor(Math.random() * list.length)]).api_url
}
async function testInstance(instance) {
try {
await (await fetch(`${instance}/streams/WDogskpmM7M`)).json()
return true
} catch (e) {
return false
}
}
2023-03-03 16:44:40 +00:00
}
async function getVideoMetadata(instance, id) {
const json = await (await fetch(`${instance}/streams/${id}`)).json()
return json
}
async function getChannel(instance, id) {
2023-03-03 16:44:40 +00:00
const json = await (await fetch(`${instance}/channel/${id}`)).json()
return json
}
async function getChannelVideos(instance, id) {
return new Promise(async (resolve, reject) => {
const videos = []
const json = await (await fetch(`${instance}/channel/${id}`)).json()
videos.push(...json.relatedStreams)
if (json.nextpage) await getNextPage(json.nextpage)
else resolve(videos)
async function getNextPage(payload) {
const page = await (await fetch(`${instance}/nextpage/channel/${id}?nextpage=${encodeURIComponent(payload)}`)).json()
videos.push(...page.relatedStreams)
if (page.nextpage) await getNextPage(page.nextpage)
else resolve(videos)
}
})
}
2023-03-03 16:44:40 +00:00
async function getPlaylistVideos(instance, id) {
const json = await (await fetch(`${instance}/playlists/${id}`)).json()
return json
}
module.exports = { getInstance, getVideoMetadata, getChannel, getChannelVideos, getPlaylistVideos }