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

View File

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

View File

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

View File

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

View File

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