download every single video from channel
This commit is contained in:
		
							parent
							
								
									28bbbd7e58
								
							
						
					
					
						commit
						d5225714d0
					
				|  | @ -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  | ||||
|     }) | ||||
| } | ||||
|  |  | |||
|  | @ -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({ | ||||
|  |  | |||
|  | @ -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({ | ||||
|  |  | |||
|  | @ -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 } | ||||
| module.exports = { getInstance, getVideoMetadata, getChannel, getChannelVideos, getPlaylistVideos } | ||||
		Loading…
	
		Reference in New Issue