mirror of https://github.com/preservetube/auto.git
				
				
				
			maybe no memory leak this time?
This commit is contained in:
		
							parent
							
								
									f79e49053d
								
							
						
					
					
						commit
						8ec961ca38
					
				
							
								
								
									
										62
									
								
								index.js
								
								
								
								
							
							
						
						
									
										62
									
								
								index.js
								
								
								
								
							| 
						 | 
					@ -16,7 +16,7 @@ const prisma = new PrismaClient()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const queue = new PQueue({ concurrency: 4 })
 | 
					const queue = new PQueue({ concurrency: 4 })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function handleCheck() {
 | 
					async function check() {
 | 
				
			||||||
    const channels = await prisma.autodownload.findMany()
 | 
					    const channels = await prisma.autodownload.findMany()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (c of channels) {
 | 
					    for (c of channels) {
 | 
				
			||||||
| 
						 | 
					@ -24,13 +24,13 @@ async function handleCheck() {
 | 
				
			||||||
            logger.info({ message: `${c.channel} is already being downloaded` })
 | 
					            logger.info({ message: `${c.channel} is already being downloaded` })
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            await redis.set(c.channel, 'downloading')
 | 
					            await redis.set(c.channel, 'downloading')
 | 
				
			||||||
            await handleDownload(c.channel)
 | 
					            await checkChannel(c.channel)
 | 
				
			||||||
            await redis.del(c.channel)
 | 
					            await redis.del(c.channel)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function handleDownload(channelId) {
 | 
					async function checkChannel(channelId) {
 | 
				
			||||||
    logger.info({ message: `Checking ${channelId} for new videos...` })
 | 
					    logger.info({ message: `Checking ${channelId} for new videos...` })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const videos = await metadata.getChannelVideos(channelId)
 | 
					    const videos = await metadata.getChannelVideos(channelId)
 | 
				
			||||||
| 
						 | 
					@ -65,35 +65,39 @@ async function handleDownload(channelId) {
 | 
				
			||||||
        logger.info({ message: `Added ${video.title} to the queue, ${id}` })
 | 
					        logger.info({ message: `Added ${video.title} to the queue, ${id}` })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        queue.add(async () => {
 | 
					        queue.add(async () => {
 | 
				
			||||||
            logger.info({ message: `Starting to download ${video.title}, ${id}` })
 | 
					            await downloadVideo(video, id)
 | 
				
			||||||
 | 
					 | 
				
			||||||
            const download = await ytdlp.downloadVideo('https://www.youtube.com' + video.url)
 | 
					 | 
				
			||||||
            if (download.fail) {
 | 
					 | 
				
			||||||
                logger.info({ message: `Failed downloading ${video.title}, ${id} -> ${download.message}` })
 | 
					 | 
				
			||||||
                await redis.del(id)
 | 
					 | 
				
			||||||
                return
 | 
					 | 
				
			||||||
            } else {
 | 
					 | 
				
			||||||
                const file = fs.readdirSync("./videos").find(f => f.includes(id))
 | 
					 | 
				
			||||||
                if (file) {
 | 
					 | 
				
			||||||
                    fs.renameSync(`./videos/${file}`, `./videos/${id}.webm`)
 | 
					 | 
				
			||||||
                    logger.info({ message: `Downloaded ${video.title}, ${id}` })
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                    const videoUrl = await upload.uploadVideo(`./videos/${id}.webm`)
 | 
					 | 
				
			||||||
                    logger.info({ message: `Uploaded ${video.title}, ${id}` })
 | 
					 | 
				
			||||||
                    fs.unlinkSync(`./videos/${id}.webm`)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                    await database.createDatabaseVideo(id, videoUrl)
 | 
					 | 
				
			||||||
                    await redis.del(id)
 | 
					 | 
				
			||||||
                } else {
 | 
					 | 
				
			||||||
                    await redis.set(id, 'error')
 | 
					 | 
				
			||||||
                    logger.info({ message: `Couldn't find file for ${video.title}, ${id}` })
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        })
 | 
					        })
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
handleCheck()
 | 
					async function downloadVideo(video, id) {
 | 
				
			||||||
 | 
					    logger.info({ message: `Starting to download ${video.title}, ${id}` })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const download = await ytdlp.downloadVideo('https://www.youtube.com' + video.url)
 | 
				
			||||||
 | 
					    if (download.fail) {
 | 
				
			||||||
 | 
					        logger.info({ message: `Failed downloading ${video.title}, ${id} -> ${download.message}` })
 | 
				
			||||||
 | 
					        await redis.del(id)
 | 
				
			||||||
 | 
					        return
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        const file = fs.readdirSync("./videos").find(f => f.includes(id))
 | 
				
			||||||
 | 
					        if (file) {
 | 
				
			||||||
 | 
					            fs.renameSync(`./videos/${file}`, `./videos/${id}.webm`)
 | 
				
			||||||
 | 
					            logger.info({ message: `Downloaded ${video.title}, ${id}` })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const videoUrl = await upload.uploadVideo(`./videos/${id}.webm`)
 | 
				
			||||||
 | 
					            logger.info({ message: `Uploaded ${video.title}, ${id}` })
 | 
				
			||||||
 | 
					            fs.unlinkSync(`./videos/${id}.webm`)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            await database.createDatabaseVideo(id, videoUrl)
 | 
				
			||||||
 | 
					            await redis.del(id)
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            await redis.set(id, 'error')
 | 
				
			||||||
 | 
					            logger.info({ message: `Couldn't find file for ${video.title}, ${id}` })
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					check()
 | 
				
			||||||
// setInterval(() => {
 | 
					// setInterval(() => {
 | 
				
			||||||
//     handleCheck()
 | 
					//     check()
 | 
				
			||||||
// }, 300000)
 | 
					// }, 300000)
 | 
				
			||||||
		Loading…
	
		Reference in New Issue