diff --git a/index.js b/index.js index ad0d997..c645dd2 100644 --- a/index.js +++ b/index.js @@ -137,4 +137,4 @@ channelQueue.process(10, async function (job, done) { check() setInterval(() => { check() -}, 300000) \ No newline at end of file +}, 15 * 60000) \ No newline at end of file diff --git a/utils/metadata.js b/utils/metadata.js index 58d14df..1e13435 100644 --- a/utils/metadata.js +++ b/utils/metadata.js @@ -2,13 +2,21 @@ const fetch = require('node-fetch') const maxRetries = 5 async function getInstance() { - const instances = await (await fetch('https://api.invidious.io/instances.json?pretty=1')).json() + const instances = await (await fetch('https://api.invidious.io/instances.json?pretty=1', { + headers: { + 'User-Agent': 'Mozilla/5.0 (compatible; PreserveTube/0.0; +https://preservetube.com)' + } + })).json() const sorted = instances.filter(o => o[1].type == 'https' && o[0] != 'invidious.io.lol' && o[0] != 'invidious.0011.lt') return `https://${sorted[Math.floor(Math.random() * sorted.length)][0]}` } async function getPipedInstance() { - const instances = await (await fetch('https://piped-instances.kavin.rocks/')).json() + const instances = await (await fetch('https://piped-instances.kavin.rocks/', { + headers: { + 'User-Agent': 'Mozilla/5.0 (compatible; PreserveTube/0.0; +https://preservetube.com)' + } + })).json() return (instances[Math.floor(Math.random() * instances.length)]).api_url } @@ -16,7 +24,11 @@ async function getVideoMetadata(id) { 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&pretty=1`) + const response = await fetch(`${instance}/api/v1/videos/${id}?fields=videoId,title,descriptionHtml,videoThumbnails,published,authorId&pretty=1`, { + headers: { + 'User-Agent': 'Mozilla/5.0 (compatible; PreserveTube/0.0; +https://preservetube.com)' + } + }) if (response.ok) { const json = await response.json() @@ -36,7 +48,11 @@ async function getChannel(id) { for (let retries = 0; retries < maxRetries; retries++) { try { const instance = await getInstance() - const response = await fetch(`${instance}/api/v1/channels/${id}?pretty=1`) + const response = await fetch(`${instance}/api/v1/channels/${id}?pretty=1`, { + headers: { + 'User-Agent': 'Mozilla/5.0 (compatible; PreserveTube/0.0; +https://preservetube.com)' + } + }) if (response.ok) { const json = await response.json() @@ -57,13 +73,21 @@ async function getChannelVideos(id) { try { const videos = [] const instance = await getPipedInstance() - const json = await (await fetch(`${instance}/channel/${id}`)).json() + const json = await (await fetch(`${instance}/channel/${id}`, { + headers: { + 'User-Agent': 'Mozilla/5.0 (compatible; PreserveTube/0.0; +https://preservetube.com)' + } + })).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() + const page = await (await fetch(`${instance}/nextpage/channel/${id}?nextpage=${encodeURIComponent(payload)}`, { + headers: { + 'User-Agent': 'Mozilla/5.0 (compatible; PreserveTube/0.0; +https://preservetube.com)' + } + })).json() videos.push(...page.relatedStreams) if (videos.length >= 120) return resolve(videos) @@ -79,7 +103,11 @@ async function getChannelVideos(id) { async function getPlaylistVideos(id) { const instance = await getPipedInstance() - const json = await (await fetch(`${instance}/playlists/${id}`)).json() + const json = await (await fetch(`${instance}/playlists/${id}`, { + headers: { + 'User-Agent': 'Mozilla/5.0 (compatible; PreserveTube/0.0; +https://preservetube.com)' + } + })).json() return json }