auto/utils/metadata.js

83 lines
2.6 KiB
JavaScript
Raw Normal View History

2023-03-28 14:52:17 +00:00
const fetch = require('node-fetch')
async function getInstance() {
for (let i = 0; i < 5; i++) {
const test = await actualRequest()
if (test) return test
2023-03-28 14:52:17 +00:00
}
async function getActualInstance() {
const instances = await (await fetch('https://piped-instances.kavin.rocks/')).json()
return (instances[Math.floor(Math.random() * instances.length)]).api_url
}
async function testInstance(instance) {
2023-03-28 14:52:17 +00:00
try {
const videoRequest = await fetch(`${instance}/streams/dQw4w9WgXcQ`)
const videoJson = await videoRequest.json()
const thumbnailRequest = await fetch(videoJson.thumbnailUrl)
return (videoRequest.status == 200) && (thumbnailRequest.status == 200)
2023-03-28 14:52:17 +00:00
} catch (e) {
return false
}
}
async function actualRequest() {
try {
const instance = await getActualInstance()
const instanceTest = await testInstance(instance)
if (instanceTest) return instance
else return false
2023-03-28 14:52:17 +00:00
} catch (e) {
return false
}
}
}
2023-03-28 14:52:17 +00:00
async function getVideoMetadata(id) {
const instance = await getInstance()
const json = await (await fetch(`${instance}/streams/${id}`)).json()
return json
2023-03-28 14:52:17 +00:00
}
async function getChannel(id) {
const instance = await getInstance()
const json = await (await fetch(`${instance}/channel/${id}`)).json()
return json
}
2023-03-28 14:52:17 +00:00
async function getChannelVideos(id) {
return new Promise(async (resolve, reject) => {
2023-03-28 14:52:17 +00:00
try {
const videos = []
2023-03-28 14:52:17 +00:00
const instance = await getInstance()
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-07-12 14:35:48 +00:00
if (videos.length >= 60) resolve(videos)
if (page.nextpage) await getNextPage(page.nextpage)
else resolve(videos)
}
2023-03-28 14:52:17 +00:00
} catch (e) {
resolve(false)
2023-03-28 14:52:17 +00:00
}
})
}
async function getPlaylistVideos(id) {
const instance = await getInstance()
const json = await (await fetch(`${instance}/playlists/${id}`)).json()
return json
2023-03-28 14:52:17 +00:00
}
module.exports = { getInstance, getVideoMetadata, getChannel, getChannelVideos, getPlaylistVideos }