download every single video from channel

This commit is contained in:
unknown 2023-03-23 17:01:46 +01:00
parent 28bbbd7e58
commit d5225714d0
4 changed files with 29 additions and 10 deletions

View File

@ -35,6 +35,7 @@ exports.getVideo = async (req, res) => {
exports.getChannel = async (req, res) => { exports.getChannel = async (req, res) => {
const instance = await metadata.getInstance() const instance = await metadata.getInstance()
const videos = await metadata.getChannelVideos(instance, req.params.id) 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' }) if (videos.error) return res.json({ error: '404' })
const archived = await prisma.videos.findMany({ const archived = await prisma.videos.findMany({
@ -51,7 +52,7 @@ exports.getChannel = async (req, res) => {
}) })
var allVideos = [] var allVideos = []
allVideos = allVideos.concat((videos.relatedStreams).map(video => { allVideos = allVideos.concat((videos).map(video => {
return { return {
id: video.url.replace('/watch?v=', ''), id: video.url.replace('/watch?v=', ''),
published: (new Date(video.uploaded)).toISOString().slice(0,10), 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)) allVideos.sort((a, b) => new Date(b.published) - new Date(a.published))
res.json({ res.json({
name: videos.name, name: channel.name,
avatar: videos.avatarUrl, avatar: channel.avatarUrl,
verified: videos.verified, verified: channel.verified,
videos: allVideos videos: allVideos
}) })
} }

View File

@ -201,8 +201,9 @@ exports.channel = async (ws, req) => {
async function startDownloading() { async function startDownloading() {
const instance = await metadata.getInstance() const instance = await metadata.getInstance()
const channel = await metadata.getChannelVideos(instance, channelId) const videos = await metadata.getChannelVideos(instance, channelId)
for (video of channel.relatedStreams) {
for (video of videos) {
const id = video.url.match(/[?&]v=([^&]+)/)[1] const id = video.url.match(/[?&]v=([^&]+)/)[1]
const already = await prisma.videos.findFirst({ const already = await prisma.videos.findFirst({

View File

@ -23,8 +23,8 @@ 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 instance = await metadata.getInstance()
const channel = await metadata.getChannelVideos(instance, channelId) const videos = await metadata.getChannelVideos(instance, channelId)
for (video of channel.relatedStreams) { for (video of videos) {
const id = video.url.match(/[?&]v=([^&]+)/)[1] const id = video.url.match(/[?&]v=([^&]+)/)[1]
const already = await prisma.videos.findFirst({ const already = await prisma.videos.findFirst({

View File

@ -36,14 +36,31 @@ async function getVideoMetadata(instance, id) {
return json return json
} }
async function getChannelVideos(instance, id) { async function getChannel(instance, id) {
const json = await (await fetch(`${instance}/channel/${id}`)).json() const json = await (await fetch(`${instance}/channel/${id}`)).json()
return 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) { async function getPlaylistVideos(instance, id) {
const json = await (await fetch(`${instance}/playlists/${id}`)).json() const json = await (await fetch(`${instance}/playlists/${id}`)).json()
return json return json
} }
module.exports = { getInstance, getVideoMetadata, getChannelVideos, getPlaylistVideos } module.exports = { getInstance, getVideoMetadata, getChannel, getChannelVideos, getPlaylistVideos }