backend/utils/metadata.js

91 lines
2.7 KiB
JavaScript
Raw Normal View History

2023-03-03 16:44:40 +00:00
const fetch = require('node-fetch')
async function getInstance() {
2023-03-24 14:20:06 +00:00
const instances = await (await fetch('https://piped-instances.kavin.rocks/')).json()
return (instances[Math.floor(Math.random() * instances.length)]).api_url
}
async function getVideoMetadata(id) {
2023-03-10 16:34:39 +00:00
for (let i = 0; i < 5; i++) {
2023-03-24 14:20:06 +00:00
const video = await actualRequest()
if (video) return video
2023-03-10 16:34:39 +00:00
}
2023-03-24 14:20:06 +00:00
async function actualRequest() {
try {
const instance = await getInstance()
const json = await (await fetch(`${instance}/streams/${id}`)).json()
return json
} catch (e) {
2023-03-10 16:34:39 +00:00
return false
}
}
2023-03-24 14:20:06 +00:00
}
2023-03-10 16:34:39 +00:00
2023-03-24 14:20:06 +00:00
async function getChannel(id) {
for (let i = 0; i < 5; i++) {
const channel = await actualRequest()
if (channel) return channel
2023-03-10 16:34:39 +00:00
}
2023-03-24 14:20:06 +00:00
async function actualRequest() {
2023-03-10 16:34:39 +00:00
try {
2023-03-24 14:20:06 +00:00
const instance = await getInstance()
const json = await (await fetch(`${instance}/channel/${id}`)).json()
return json
2023-03-10 16:34:39 +00:00
} catch (e) {
2023-03-24 14:20:06 +00:00
return false
2023-03-10 16:34:39 +00:00
}
}
2023-03-03 16:44:40 +00:00
}
2023-03-24 14:23:10 +00:00
async function getChannelVideos(id) {
2023-03-24 14:20:06 +00:00
for (let i = 0; i < 5; i++) {
const videos = await actualRequest()
if (videos) return videos
}
async function actualRequest() {
2023-03-24 15:07:31 +00:00
return new Promise(async (resolve, reject) => {
try {
2023-03-24 14:20:06 +00:00
const videos = []
2023-03-24 14:23:10 +00:00
const instance = await getInstance()
2023-03-24 14:20:06 +00:00
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)
2023-03-24 18:27:35 +00:00
if (videos.length >= 210) resolve(videos)
2023-03-24 14:20:06 +00:00
if (page.nextpage) await getNextPage(page.nextpage)
else resolve(videos)
}
2023-03-24 15:07:31 +00:00
} catch (e) {
2023-03-24 15:39:01 +00:00
resolve(false)
2023-03-24 15:07:31 +00:00
}
})
2023-03-24 14:20:06 +00:00
}
}
2023-03-24 14:20:06 +00:00
async function getPlaylistVideos(id) {
for (let i = 0; i < 5; i++) {
const playlists = await actualRequest()
if (playlists) return playlists
}
async function actualRequest() {
try {
const instance = await getInstance()
const json = await (await fetch(`${instance}/playlists/${id}`)).json()
return json
} catch (e) {
return false
}
}
2023-03-03 16:44:40 +00:00
}
module.exports = { getInstance, getVideoMetadata, getChannel, getChannelVideos, getPlaylistVideos }