faster auto downloads...

This commit is contained in:
unknown 2023-03-25 12:12:39 +01:00
parent d883f709a0
commit d5f3a89231
1 changed files with 17 additions and 8 deletions

View File

@ -24,8 +24,9 @@ async function handleDownload(channelId) {
const videos = await metadata.getChannelVideos(channelId) const videos = await metadata.getChannelVideos(channelId)
if (!videos) return logger.info({ message: `Failed requesting Youtube for ${channelId}` }) if (!videos) return logger.info({ message: `Failed requesting Youtube for ${channelId}` })
let downloadIndex = 0
for (video of videos) { await Promise.all(videos.map(async (video) => {
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({
@ -34,25 +35,29 @@ async function handleDownload(channelId) {
} }
}) })
if (already) continue if (already) return
if (await redis.get(id)) { if (await redis.get(id)) {
logger.info({ message: `Someone is already downloading ${video.title}, ${id}` }) logger.info({ message: `Someone is already downloading ${video.title}, ${id}` })
continue return
} }
if (video.duration > 5400) { if (video.duration > 5400) {
logger.info({ message: `${video.title} is longer than 1h30m, ${id}` }) logger.info({ message: `${video.title} is longer than 1h30m, ${id}` })
continue return
} }
await redis.set(id, 'downloading') await redis.set(id, 'downloading')
downloadIndex++
await delay(downloadIndex * 15000)
logger.info({ message: `Starting to download ${video.title}, ${id}` }) logger.info({ message: `Starting to download ${video.title}, ${id}` })
const download = await ytdlp.downloadVideo('https://www.youtube.com' + video.url) const download = await ytdlp.downloadVideo('https://www.youtube.com' + video.url)
if (download.fail) { if (download.fail) {
logger.info({ message: `Failed downloading ${video.title}, ${id} -> ${download.message}` }) logger.info({ message: `Failed downloading ${video.title}, ${id} -> ${download.message}` })
await redis.del(id) await redis.del(id)
continue return
} else { } else {
const file = fs.readdirSync("./videos").find(f => f.includes(id)) const file = fs.readdirSync("./videos").find(f => f.includes(id))
if (file) { if (file) {
@ -70,7 +75,11 @@ async function handleDownload(channelId) {
logger.info({ message: `Couldn't find file for ${video.title}, ${id}` }) logger.info({ message: `Couldn't find file for ${video.title}, ${id}` })
} }
} }
} }))
} }
module.exports = { handleCheck } async function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
module.exports = { handleCheck, handleDownload }