2023-03-03 16:44:40 +00:00
|
|
|
const fetch = require('node-fetch')
|
2023-08-18 19:31:40 +00:00
|
|
|
const maxRetries = 5
|
2023-03-03 16:44:40 +00:00
|
|
|
|
|
|
|
async function getInstance() {
|
2023-08-04 09:25:18 +00:00
|
|
|
const instances = await (await fetch('https://api.invidious.io/instances.json?pretty=1')).json()
|
2023-09-07 20:23:02 +00:00
|
|
|
const sorted = instances.filter(o => o[1].type == 'https' && o[0] != 'invidious.io.lol' && o[0] != 'invidious.0011.lt')
|
2023-08-11 21:02:51 +00:00
|
|
|
return `https://${sorted[Math.floor(Math.random() * sorted.length)][0]}`
|
2023-08-04 09:25:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getPipedInstance() {
|
|
|
|
const instances = await (await fetch('https://piped-instances.kavin.rocks/')).json()
|
|
|
|
return (instances[Math.floor(Math.random() * instances.length)]).api_url
|
2023-06-22 11:15:02 +00:00
|
|
|
}
|
2023-03-24 15:07:31 +00:00
|
|
|
|
2023-06-22 11:15:02 +00:00
|
|
|
async function getVideoMetadata(id) {
|
2023-08-18 19:31:40 +00:00
|
|
|
for (let retries = 0; retries < maxRetries; retries++) {
|
|
|
|
try {
|
|
|
|
const instance = await getInstance()
|
|
|
|
const response = await fetch(`${instance}/api/v1/videos/${id}?fields=videoId,title,descriptionHtml,videoThumbnails,published,authorId,error&pretty=1`)
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
const json = await response.json()
|
|
|
|
return json
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2023-03-23 16:01:46 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 11:15:02 +00:00
|
|
|
async function getChannel(id) {
|
2023-08-18 19:31:40 +00:00
|
|
|
for (let retries = 0; retries < maxRetries; retries++) {
|
|
|
|
try {
|
|
|
|
const instance = await getInstance()
|
|
|
|
const response = await fetch(`${instance}/api/v1/channels/${id}?pretty=1`)
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
const json = await response.json()
|
|
|
|
return json
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2023-06-22 11:15:02 +00:00
|
|
|
}
|
2023-03-24 14:20:06 +00:00
|
|
|
|
2023-06-22 11:15:02 +00:00
|
|
|
async function getChannelVideos(id) {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
2023-03-24 14:20:06 +00:00
|
|
|
try {
|
2023-06-22 11:15:02 +00:00
|
|
|
const videos = []
|
2023-08-04 09:25:18 +00:00
|
|
|
const instance = await getPipedInstance()
|
2023-06-22 11:15:02 +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-07-06 14:19:36 +00:00
|
|
|
if (videos.length >= 60) resolve(videos)
|
2023-06-22 11:15:02 +00:00
|
|
|
if (page.nextpage) await getNextPage(page.nextpage)
|
|
|
|
else resolve(videos)
|
|
|
|
}
|
|
|
|
|
2023-03-24 14:20:06 +00:00
|
|
|
} catch (e) {
|
2023-06-22 11:15:02 +00:00
|
|
|
resolve(false)
|
2023-03-24 14:20:06 +00:00
|
|
|
}
|
2023-06-22 11:15:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getPlaylistVideos(id) {
|
2023-08-04 09:25:18 +00:00
|
|
|
const instance = await getPipedInstance()
|
2023-06-22 11:15:02 +00:00
|
|
|
const json = await (await fetch(`${instance}/playlists/${id}`)).json()
|
|
|
|
return json
|
2023-03-03 16:44:40 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 16:01:46 +00:00
|
|
|
module.exports = { getInstance, getVideoMetadata, getChannel, getChannelVideos, getPlaylistVideos }
|