mirror of https://github.com/preservetube/auto.git
updating thumbnail getting once again... also changing how it checks if instance is ok
This commit is contained in:
parent
989c4144cd
commit
a393b4e266
|
@ -7,7 +7,7 @@ const upload = require('./upload.js')
|
||||||
async function createDatabaseVideo(id, videoUrl, playlistId) {
|
async function createDatabaseVideo(id, videoUrl, playlistId) {
|
||||||
const data = await metadata.getVideoMetadata(id)
|
const data = await metadata.getVideoMetadata(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, `https://i.ytimg.com/vi_webp/${id}/maxresdefault.webp`)
|
const thumbnailUrl = await upload.uploadImage(id, data.thumbnailUrl)
|
||||||
|
|
||||||
await prisma.videos.create({
|
await prisma.videos.create({
|
||||||
data: {
|
data: {
|
||||||
|
|
|
@ -1,91 +1,83 @@
|
||||||
const fetch = require('node-fetch')
|
const fetch = require('node-fetch')
|
||||||
|
|
||||||
async function getInstance() {
|
async function getInstance() {
|
||||||
const instances = await (await fetch('https://piped-instances.kavin.rocks/')).json()
|
for (let i = 0; i < 5; i++) {
|
||||||
return (instances[Math.floor(Math.random() * instances.length)]).api_url
|
const test = await actualRequest()
|
||||||
|
if (test) return test
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
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)
|
||||||
|
} catch (e) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function actualRequest() {
|
||||||
|
try {
|
||||||
|
const instance = await getActualInstance()
|
||||||
|
const instanceTest = await testInstance(instance)
|
||||||
|
|
||||||
|
if (instanceTest) return instance
|
||||||
|
else return false
|
||||||
|
} catch (e) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getVideoMetadata(id) {
|
async function getVideoMetadata(id) {
|
||||||
for (let i = 0; i < 5; i++) {
|
const instance = await getInstance()
|
||||||
const video = await actualRequest()
|
const json = await (await fetch(`${instance}/streams/${id}`)).json()
|
||||||
if (video) return video
|
return json
|
||||||
}
|
|
||||||
|
|
||||||
async function actualRequest() {
|
|
||||||
try {
|
|
||||||
const instance = await getInstance()
|
|
||||||
const json = await (await fetch(`${instance}/streams/${id}`)).json()
|
|
||||||
return json
|
|
||||||
} catch (e) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getChannel(id) {
|
async function getChannel(id) {
|
||||||
for (let i = 0; i < 5; i++) {
|
const instance = await getInstance()
|
||||||
const channel = await actualRequest()
|
const json = await (await fetch(`${instance}/channel/${id}`)).json()
|
||||||
if (channel) return channel
|
return json
|
||||||
}
|
|
||||||
|
|
||||||
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(id) {
|
async function getChannelVideos(id) {
|
||||||
for (let i = 0; i < 5; i++) {
|
return new Promise(async (resolve, reject) => {
|
||||||
const videos = await actualRequest()
|
try {
|
||||||
if (videos) return videos
|
const videos = []
|
||||||
}
|
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)
|
||||||
|
|
||||||
async function actualRequest() {
|
if (videos.length >= 210) resolve(videos)
|
||||||
return new Promise(async (resolve, reject) => {
|
if (page.nextpage) await getNextPage(page.nextpage)
|
||||||
try {
|
|
||||||
const videos = []
|
|
||||||
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)
|
else resolve(videos)
|
||||||
|
|
||||||
async function getNextPage(payload) {
|
|
||||||
const page = await (await fetch(`${instance}/nextpage/channel/${id}?nextpage=${encodeURIComponent(payload)}`)).json()
|
|
||||||
videos.push(...page.relatedStreams)
|
|
||||||
|
|
||||||
if (videos.length >= 210) resolve(videos)
|
|
||||||
if (page.nextpage) await getNextPage(page.nextpage)
|
|
||||||
else resolve(videos)
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (e) {
|
|
||||||
resolve(false)
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
} catch (e) {
|
||||||
|
resolve(false)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getPlaylistVideos(id) {
|
async function getPlaylistVideos(id) {
|
||||||
for (let i = 0; i < 5; i++) {
|
const instance = await getInstance()
|
||||||
const playlists = await actualRequest()
|
const json = await (await fetch(`${instance}/playlists/${id}`)).json()
|
||||||
if (playlists) return playlists
|
return json
|
||||||
}
|
|
||||||
|
|
||||||
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 }
|
module.exports = { getInstance, getVideoMetadata, getChannel, getChannelVideos, getPlaylistVideos }
|
Loading…
Reference in New Issue