updating instance logic

This commit is contained in:
unknown 2023-03-24 15:20:06 +01:00
parent 7916399990
commit 16e87dd51f
5 changed files with 71 additions and 56 deletions

View File

@ -33,9 +33,8 @@ exports.getVideo = async (req, res) => {
} }
exports.getChannel = async (req, res) => { exports.getChannel = async (req, res) => {
const instance = await metadata.getInstance() const videos = await metadata.getChannelVideos(req.params.id)
const videos = await metadata.getChannelVideos(instance, req.params.id) const channel = await metadata.getChannel(req.params.id)
const channel = await metadata.getChannel(instance, req.params.id)
if (videos.error) return res.json({ error: '404' }) if (videos.error) return res.json({ error: '404' })
const archived = await prisma.videos.findMany({ const archived = await prisma.videos.findMany({
@ -66,7 +65,7 @@ exports.getChannel = async (req, res) => {
const index = allVideos.findIndex(o => o.id == v.id) const index = allVideos.findIndex(o => o.id == v.id)
allVideos[index] = v allVideos[index] = v
} else { } else {
const live = await metadata.getVideoMetadata(instance, v.id) const live = await metadata.getVideoMetadata(v.id)
allVideos.push({ allVideos.push({
...v, ...v,
@ -86,8 +85,7 @@ exports.getChannel = async (req, res) => {
} }
exports.getPlaylist = async (req, res) => { exports.getPlaylist = async (req, res) => {
const instance = await metadata.getInstance() const playlist = await metadata.getPlaylistVideos(req.params.id)
const playlist = await metadata.getPlaylistVideos(instance, req.params.id)
if (playlist.error) return res.json({ error: '404' }) if (playlist.error) return res.json({ error: '404' })
const playlistArchived = await prisma.videos.findMany({ const playlistArchived = await prisma.videos.findMany({
@ -118,7 +116,7 @@ exports.getPlaylist = async (req, res) => {
const index = allVideos.findIndex(o => o.id == v.id) const index = allVideos.findIndex(o => o.id == v.id)
allVideos[index] = v allVideos[index] = v
} else { } else {
const live = await metadata.getVideoMetadata(instance, v.id) const live = await metadata.getVideoMetadata(v.id)
allVideos.push({ allVideos.push({
...v, ...v,

View File

@ -110,8 +110,7 @@ exports.playlist = async (ws, req) => {
}) })
async function startDownloading() { async function startDownloading() {
const instance = await metadata.getInstance() const playlist = await metadata.getPlaylistVideos(playlistId)
const playlist = await metadata.getPlaylistVideos(instance, playlistId)
for (video of playlist.relatedStreams) { for (video of playlist.relatedStreams) {
const id = video.url.match(/[?&]v=([^&]+)/)[1] const id = video.url.match(/[?&]v=([^&]+)/)[1]
@ -200,8 +199,7 @@ exports.channel = async (ws, req) => {
}) })
async function startDownloading() { async function startDownloading() {
const instance = await metadata.getInstance() const videos = await metadata.getChannelVideos(channelId)
const videos = await metadata.getChannelVideos(instance, channelId)
for (video of videos) { for (video of videos) {
const id = video.url.match(/[?&]v=([^&]+)/)[1] const id = video.url.match(/[?&]v=([^&]+)/)[1]

View File

@ -22,8 +22,7 @@ async function handleCheck() {
async function handleDownload(channelId) { async function handleDownload(channelId) {
logger.info({ message: `Checking ${channelId} for new videos...` }) logger.info({ message: `Checking ${channelId} for new videos...` })
const instance = await metadata.getInstance() const videos = await metadata.getChannelVideos(channelId)
const videos = await metadata.getChannelVideos(instance, channelId)
for (video of videos) { for (video of videos) {
const id = video.url.match(/[?&]v=([^&]+)/)[1] const id = video.url.match(/[?&]v=([^&]+)/)[1]

View File

@ -1,47 +1,52 @@
const fetch = require('node-fetch') const fetch = require('node-fetch')
async function getInstance() { async function getInstance() {
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 instances = await (await fetch('https://piped-instances.kavin.rocks/')).json()
const list = instances.filter(i => !i.cdn) return (instances[Math.floor(Math.random() * instances.length)]).api_url
return (list[Math.floor(Math.random() * list.length)]).api_url }
async function getVideoMetadata(id) {
for (let i = 0; i < 5; i++) {
const video = await actualRequest()
if (video) return video
} }
async function testInstance(instance) { async function actualRequest() {
try { try {
await (await fetch(`${instance}/streams/WDogskpmM7M`)).json() const instance = await getInstance()
return true const json = await (await fetch(`${instance}/streams/${id}`)).json()
return json
} catch (e) { } catch (e) {
return false return false
} }
} }
} }
async function getVideoMetadata(instance, id) { async function getChannel(id) {
const json = await (await fetch(`${instance}/streams/${id}`)).json() for (let i = 0; i < 5; i++) {
return json const channel = await actualRequest()
} if (channel) return channel
}
async function getChannel(instance, id) { async function actualRequest() {
try {
const instance = await getInstance()
const json = await (await fetch(`${instance}/channel/${id}`)).json() const json = await (await fetch(`${instance}/channel/${id}`)).json()
return json return json
} catch (e) {
return false
}
}
} }
async function getChannelVideos(instance, id) { async function getChannelVideos(instance, id) {
for (let i = 0; i < 5; i++) {
const videos = await actualRequest()
if (videos) return videos
}
async function actualRequest() {
try {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
const videos = [] const videos = []
const json = await (await fetch(`${instance}/channel/${id}`)).json() const json = await (await fetch(`${instance}/channel/${id}`)).json()
@ -56,11 +61,27 @@ async function getChannelVideos(instance, id) {
else resolve(videos) else resolve(videos)
} }
}) })
} catch (e) {
return false
}
}
} }
async function getPlaylistVideos(instance, id) { 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() const json = await (await fetch(`${instance}/playlists/${id}`)).json()
return json return json
} catch (e) {
return false
}
}
} }
module.exports = { getInstance, getVideoMetadata, getChannel, getChannelVideos, getPlaylistVideos } module.exports = { getInstance, getVideoMetadata, getChannel, getChannelVideos, getPlaylistVideos }

View File

@ -5,8 +5,7 @@ const metadata = require('./metadata.js')
const upload = require('./upload.js') const upload = require('./upload.js')
async function createDatabaseVideo(id, videoUrl, playlistId) { async function createDatabaseVideo(id, videoUrl, playlistId) {
const instance = await metadata.getInstance() const data = await metadata.getVideoMetadata(id)
const data = await metadata.getVideoMetadata(instance, id)
const uploaderAvatar = await upload.uploadImage((data.uploaderUrl).replace('/channel/', ''), data.uploaderAvatar) const uploaderAvatar = await upload.uploadImage((data.uploaderUrl).replace('/channel/', ''), data.uploaderAvatar)
const thumbnailUrl = await upload.uploadImage(id, data.thumbnailUrl) const thumbnailUrl = await upload.uploadImage(id, data.thumbnailUrl)