updating instance logic
This commit is contained in:
parent
7916399990
commit
16e87dd51f
|
@ -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,
|
||||||
|
|
|
@ -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]
|
||||||
|
|
|
@ -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]
|
||||||
|
|
||||||
|
|
|
@ -1,66 +1,87 @@
|
||||||
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()
|
||||||
|
return (instances[Math.floor(Math.random() * instances.length)]).api_url
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getVideoMetadata(id) {
|
||||||
for (let i = 0; i < 5; i++) {
|
for (let i = 0; i < 5; i++) {
|
||||||
const instance = await actuallyGettingInstance()
|
const video = await actualRequest()
|
||||||
if (instance) return instance
|
if (video) return video
|
||||||
}
|
}
|
||||||
|
|
||||||
async function actuallyGettingInstance() {
|
async function actualRequest() {
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
async function testInstance(instance) {
|
|
||||||
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() {
|
||||||
const json = await (await fetch(`${instance}/channel/${id}`)).json()
|
try {
|
||||||
return json
|
const instance = await getInstance()
|
||||||
|
const json = await (await fetch(`${instance}/channel/${id}`)).json()
|
||||||
|
return json
|
||||||
|
} catch (e) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getChannelVideos(instance, id) {
|
async function getChannelVideos(instance, id) {
|
||||||
return new Promise(async (resolve, reject) => {
|
for (let i = 0; i < 5; i++) {
|
||||||
const videos = []
|
const videos = await actualRequest()
|
||||||
const json = await (await fetch(`${instance}/channel/${id}`)).json()
|
if (videos) return videos
|
||||||
videos.push(...json.relatedStreams)
|
}
|
||||||
if (json.nextpage) await getNextPage(json.nextpage)
|
|
||||||
else resolve(videos)
|
|
||||||
|
|
||||||
async function getNextPage(payload) {
|
async function actualRequest() {
|
||||||
const page = await (await fetch(`${instance}/nextpage/channel/${id}?nextpage=${encodeURIComponent(payload)}`)).json()
|
try {
|
||||||
videos.push(...page.relatedStreams)
|
return new Promise(async (resolve, reject) => {
|
||||||
if (page.nextpage) await getNextPage(page.nextpage)
|
const videos = []
|
||||||
else resolve(videos)
|
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)
|
||||||
|
if (page.nextpage) await getNextPage(page.nextpage)
|
||||||
|
else resolve(videos)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} catch (e) {
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getPlaylistVideos(instance, id) {
|
async function getPlaylistVideos(id) {
|
||||||
const json = await (await fetch(`${instance}/playlists/${id}`)).json()
|
for (let i = 0; i < 5; i++) {
|
||||||
return json
|
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 }
|
module.exports = { getInstance, getVideoMetadata, getChannel, getChannelVideos, getPlaylistVideos }
|
|
@ -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)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue