From d5225714d05bfb752619671e8d22fed791c6bd6b Mon Sep 17 00:00:00 2001 From: unknown <89595418+unknownsrc@users.noreply.github.com> Date: Thu, 23 Mar 2023 17:01:46 +0100 Subject: [PATCH] download every single video from channel --- controller/video.js | 9 +++++---- controller/websocket.js | 5 +++-- utils/auto.js | 4 ++-- utils/metadata.js | 21 +++++++++++++++++++-- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/controller/video.js b/controller/video.js index 4049752..687a015 100644 --- a/controller/video.js +++ b/controller/video.js @@ -35,6 +35,7 @@ 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) if (videos.error) return res.json({ error: '404' }) const archived = await prisma.videos.findMany({ @@ -51,7 +52,7 @@ exports.getChannel = async (req, res) => { }) var allVideos = [] - allVideos = allVideos.concat((videos.relatedStreams).map(video => { + allVideos = allVideos.concat((videos).map(video => { return { id: video.url.replace('/watch?v=', ''), published: (new Date(video.uploaded)).toISOString().slice(0,10), @@ -77,9 +78,9 @@ exports.getChannel = async (req, res) => { allVideos.sort((a, b) => new Date(b.published) - new Date(a.published)) res.json({ - name: videos.name, - avatar: videos.avatarUrl, - verified: videos.verified, + name: channel.name, + avatar: channel.avatarUrl, + verified: channel.verified, videos: allVideos }) } diff --git a/controller/websocket.js b/controller/websocket.js index 16ec41c..82ab9bd 100644 --- a/controller/websocket.js +++ b/controller/websocket.js @@ -201,8 +201,9 @@ exports.channel = async (ws, req) => { async function startDownloading() { const instance = await metadata.getInstance() - const channel = await metadata.getChannelVideos(instance, channelId) - for (video of channel.relatedStreams) { + const videos = await metadata.getChannelVideos(instance, channelId) + + for (video of videos) { const id = video.url.match(/[?&]v=([^&]+)/)[1] const already = await prisma.videos.findFirst({ diff --git a/utils/auto.js b/utils/auto.js index a40b94b..be7192c 100644 --- a/utils/auto.js +++ b/utils/auto.js @@ -23,8 +23,8 @@ async function handleDownload(channelId) { logger.info({ message: `Checking ${channelId} for new videos...` }) const instance = await metadata.getInstance() - const channel = await metadata.getChannelVideos(instance, channelId) - for (video of channel.relatedStreams) { + const videos = await metadata.getChannelVideos(instance, channelId) + for (video of videos) { const id = video.url.match(/[?&]v=([^&]+)/)[1] const already = await prisma.videos.findFirst({ diff --git a/utils/metadata.js b/utils/metadata.js index 95a445b..020f4ff 100644 --- a/utils/metadata.js +++ b/utils/metadata.js @@ -36,14 +36,31 @@ async function getVideoMetadata(instance, id) { return json } -async function getChannelVideos(instance, id) { +async function getChannel(instance, id) { const json = await (await fetch(`${instance}/channel/${id}`)).json() return json } +async function getChannelVideos(instance, id) { + return new Promise(async (resolve, reject) => { + const 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) + } + }) +} + async function getPlaylistVideos(instance, id) { const json = await (await fetch(`${instance}/playlists/${id}`)).json() return json } -module.exports = { getInstance, getVideoMetadata, getChannelVideos, getPlaylistVideos } \ No newline at end of file +module.exports = { getInstance, getVideoMetadata, getChannel, getChannelVideos, getPlaylistVideos } \ No newline at end of file