@@ -3,6 +3,7 @@ import { db } from '@main/stores/queue-database'
3
3
import { queries , SelectDownload } from '@main/stores/queue-database.helpers'
4
4
import { downloads } from '@main/stores/queue-database.schema'
5
5
import { logger } from '@shared/logger'
6
+ import { queuePromiseStack } from '@shared/promises/helper'
6
7
import { TRPCError } from '@trpc/server'
7
8
import { observable } from '@trpc/server/observable'
8
9
import { desc } from 'drizzle-orm'
@@ -13,7 +14,12 @@ import { VideoInfo } from 'yt-dlp-wrap/types'
13
14
import { YTDLDownloadStatus , YTDLItem , YTDLStatus } from 'ytdlp-desktop/types'
14
15
import { z } from 'zod'
15
16
import { publicProcedure , router } from './trpc'
16
- import { MAX_STREAM_CONCURRENT_FRAGMENTS , ytdl , YTDLP_CACHE_PATH } from './ytdlp.core'
17
+ import {
18
+ MAX_PARALLEL_DOWNLOADS ,
19
+ MAX_STREAM_CONCURRENT_FRAGMENTS ,
20
+ ytdl ,
21
+ YTDLP_CACHE_PATH
22
+ } from './ytdlp.core'
17
23
import { ytdlpDownloadQueue , ytdlpEvents } from './ytdlp.ee'
18
24
const log = logger . child ( 'ytdlp.api' )
19
25
export const ytdlpRouter = router ( {
@@ -26,9 +32,10 @@ export const ytdlpRouter = router({
26
32
} )
27
33
)
28
34
. mutation ( async ( { input : { url } , ctx } ) => {
29
- const files = await Promise . all ( url . map ( ( u ) => queueYtdlMetaCheck ( u ) . catch ( ( ) => null ) ) ) . then (
30
- ( files ) => files . filter ( ( s ) => ! ! s )
31
- )
35
+ const files = await queuePromiseStack (
36
+ url . map ( ( u ) => ( ) => queueYtdlMetaCheck ( u ) . catch ( ( ) => null ) ) ,
37
+ MAX_PARALLEL_DOWNLOADS
38
+ ) . then ( ( files ) => files . filter ( ( s ) => ! ! s ) )
32
39
const asyncResult = ytdlpDownloadQueue . addAll (
33
40
files . map (
34
41
( f ) => ( ) =>
@@ -228,7 +235,7 @@ const queueYtdlMetaCheck = async (
228
235
) : Promise < { dbFile : SelectDownload ; videoInfo : VideoInfo } > => {
229
236
if ( typeof url !== 'string' || ! / ^ h t t p s / gi. test ( url ) ) throw new Error ( 'Invalid url format' )
230
237
ytdlpEvents . emit ( 'status' , { action : 'getVideoInfo' , state : 'progressing' } )
231
- log . debug ( " meta" , `added url ${ url } ` )
238
+ log . debug ( ' meta' , `added url ${ url } ` )
232
239
const existingDbFile = await queries . downloads . findDownloadByExactUrl ( url )
233
240
let [ dbFile ] = await queries . downloads . createDownload ( {
234
241
metaId : existingDbFile ?. metaId ?? '' ,
@@ -244,8 +251,9 @@ const queueYtdlMetaCheck = async (
244
251
retryCount : 0
245
252
} )
246
253
ytdlpEvents . emit ( 'list' , [ dbFile ] )
247
- if ( ! dbFile . meta ) {
254
+ if ( ! dbFile . meta ?. filename ) {
248
255
const { value : videoInfo , error : videoInfoError } = await ytdl . getVideoInfo ( url )
256
+ log . debug ( 'metadata' , omit ( videoInfo , 'thumbnails' , 'formats' ) )
249
257
if ( videoInfoError || ! videoInfo ) {
250
258
if ( videoInfoError ) log . error ( 'getVideoInfo' , videoInfoError )
251
259
await deleteDownloadItem ( dbFile )
@@ -254,6 +262,9 @@ const queueYtdlMetaCheck = async (
254
262
message : 'URL not supported or video not found'
255
263
} )
256
264
}
265
+ dbFile . meta = omit ( videoInfo , 'formats' , 'thumbnails' ) as VideoInfo
266
+ dbFile . metaId = videoInfo . id
267
+ dbFile = await queries . downloads . updateDownload ( dbFile . id , dbFile )
257
268
return { dbFile, videoInfo }
258
269
}
259
270
return { dbFile, videoInfo : dbFile . meta }
@@ -267,7 +278,7 @@ const queueYtdlDownload = async (dbFile: SelectDownload, videoInfo: VideoInfo) =
267
278
const controller = new AbortController ( )
268
279
const deleteEntry = ( ) => deleteDownloadItem ( dbFile )
269
280
const updateEntry = ( ) =>
270
- queries . downloads . updateDownload ( dbFile . id , dbFile ) . then ( ( [ s ] ) => {
281
+ queries . downloads . updateDownload ( dbFile . id , dbFile ) . then ( ( s ) => {
271
282
ytdlpEvents . emit ( 'list' , [ s ] )
272
283
dbFile = s
273
284
return s
@@ -300,7 +311,7 @@ const queueYtdlDownload = async (dbFile: SelectDownload, videoInfo: VideoInfo) =
300
311
]
301
312
if ( settings . flags ?. nomtime ) execArgs . push ( '--no-mtime' )
302
313
if ( settings . flags ?. custom ) execArgs . push ( ...settings . flags . custom . split ( ' ' ) )
303
- const stream = ytdl . exec ( uniq ( execArgs ) , { } , controller . signal )
314
+ const stream = ytdl . ytdlp . exec ( uniq ( execArgs ) , { } , controller . signal )
304
315
305
316
async function cancel ( id : any ) {
306
317
if ( id && id === dbFile . id ) {
0 commit comments