package Ġcom . github . w um o . b ilib ili . service . download Ċ Ċ import Ġcom . github . w um o . b ilib ili . api . Med ias Ċ import Ġcom . github . w um o . b ilib ili . api . Videos . quality Desc Ċ import Ġcom . github . w um o . b ilib ili . model . Media Page Ċ import Ġcom . github . w um o . b ilib ili . model . MediaType Ċ import Ġcom . github . w um o . b ilib ili . service . State ful Service Ċ import Ġcom . github . w um o . b ilib ili . service . Consumer Service Ċ import Ġcom . github . w um o . b ilib ili . service . download . Download History . record Ċ import Ġcom . github . w um o . b ilib ili . service . download . Download Service . download Config Ċ import Ġcom . github . w um o . b ilib ili . service . download . State .* Ċ import Ġcom . github . w um o . b ilib ili . service . download . State Monitor . media Pages To Download Ċ import Ġcom . github . w um o . b ilib ili . service . download . State Monitor . monitor Ċ import Ġcom . github . w um o . b ilib ili . service . download . State Monitor . state Updated Media Page Monitor s Ċ import Ġcom . github . w um o . b ilib ili . util . api Scope Ċ import Ġcom . github . w um o . b ilib ili . util . escape Path Ċ import Ġcom . github . w um o . b ilib ili . util . now Epoch Second Ċ import Ġcom . github . w um o . b ilib ili . util . to Valid FileName Ċ import Ġkotlinx . coroutines . Cancellation Exception Ċ import Ġkotlinx . coroutines . Comp letable Job Ċ import Ġjava . nio . file . Paths Ċ Ċ const Ġval Ġdefault Media Format Ġ= Ġ" mk v " Ċ Ċ class ĠMedia Page Stream er : ĠStateful Service < Media Page >( api Scope ) Ġ{ ĊĠ Ġlateinit Ġvar Ġlegacy Page Stream er : ĠConsumer Service < Download Page > ĊĠ Ġlateinit Ġvar Ġmodern Page Stream er : ĠConsumer Service < Download Page > ĊĠ Ġlateinit Ġvar Ġdan mak u Downloader : ĠConsumer Service < Download Page > ĊĠĠĊĠ Ġoverride Ġfun Ġon Offer ( task : ĠMedia Page , Ġjob : ĠComp letable Job ) Ġ{ ĊĠĠĠ Ġval Ġpage Monitor Ġ= Ġmonitor ( task ) ĊĠĠĠ Ġval Ġdownload Page Ġ= Ġrecord ( task ) ĊĠĠĠ Ġjob . invoke On Completion Ġ{ ĊĠĠĠĠĠ Ġmedia Pages To Download . decrement AndGet () ĊĠĠĠĠĠ Ġwhen ( it ) Ġ{ ĊĠĠĠĠĠĠĠ Ġnull Ġ-> Ġ{ ĊĠĠĠĠĠĠĠĠĠ Ġpage Monitor . state . set ( Finished ) ĊĠĠĠĠĠĠĠĠĠ Ġstate Updated Media Page Monitor s Ġ+= Ġpage Monitor ĊĠĠĠĠĠĠĠĠĠ Ġdownload Page . done Ġ= Ġtrue ĊĠĠĠĠĠĠĠĠĠ Ġdownload Page . ts Ġ= Ġnow Epoch Second () ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġis ĠC ancellation Exception Ġ-> Ġ{ ĊĠĠĠĠĠĠĠĠĠ Ġpage Monitor . state . set ( Paused ) ĊĠĠĠĠĠĠĠĠĠ Ġstate Updated Media Page Monitor s Ġ+= Ġpage Monitor ĊĠĠĠĠĠĠĠĠĠ Ġdownload Page . done Ġ= Ġfalse ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĠĠĠ Ġmedia Pages To Download . increment AndGet () ĊĠ Ġ} ĊĠĠĊĠ Ġoverride Ġsuspend Ġfun Ġon Task ( task : ĠMedia Page , Ġjob : ĠComp letable Job ) Ġ{ ĊĠĠĠ Ġval Ġpage Monitor Ġ= Ġmonitor ( task ) ĊĠĠĠ Ġval Ġdownload Page Ġ= Ġrecord ( task ) ĊĠĠĠĠĊĠĠĠ Ġpage Monitor . state . set ( Scheduled ) ĊĠĠĠ Ġstate Updated Media Page Monitor s Ġ+= Ġpage Monitor ĊĠĠĠ Ġval Ġpage Info Ġ= ĠMed ias . extract Info ( task ) ĊĠĠĠ Ġval Ġbest Quality Ġ= Ġif ( page Info . quality Ġ== Ġpage Info . best Quality ) Ġ"[ best ]" Ġelse Ġ"" ĊĠĠĠ Ġval Ġquality Desc Ġ= Ġpage Info . quality . quality Desc () ĊĠĠĠ Ġval Ġname Ġ= Ġ"${ task . media Title . escape Path ()} -${ task . title . escape Path ()} Ġ[ p ${ task . p Idx } ][$ quality Desc ]$ best Quality " ĊĠĠĠ Ġval ĠfileName Ġ= Ġname . to Valid FileName (). trim () ĊĠĠĠ Ġval Ġfolder Ġ= ĠPaths . get ( download Config . dir , Ġtask . folder ). toString () ĊĠĠĠ Ġdownload Page . info Ġ= Ġpage Info ĊĠĠĠ Ġdownload Page . progress Ġ= Ġdownload Page . progress ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ?: ĠPage Progress ( folder , ĠfileName , Ġdefault Media Format , Ġ 0 , Ġ 0 ) ĊĠĠĠ Ġpage Monitor . info Ġ= Ġdownload Page ĊĠĠĠ Ġval ĠfilePath Ġ= Ġdownload Page . progress !!. filePath () ĊĠĠĠĠĊĠĠĠ Ġif ( filePath . to File (). exists ()) Ġreturn ĊĠĠĠ Ġdownload Page . done Ġ= Ġfalse ĊĠĠĠĠĊĠĠĠ Ġ( if ( download Page . info !!. legacy ) Ġlegacy Page Stream er Ġelse Ġmodern Page Stream er ) ĊĠĠĠĠĠ Ġ. offer ( download Page , Ġjob ) ĊĠĠĠ Ġif ( task . type Ġ!= ĠMediaType . Audio ) ĊĠĠĠĠĠ Ġdan mak u Downloader . offer ( download Page ) ĊĠ Ġ} ĊĠĠĊĠ Ġoverride Ġfun Ġon Remove ( task : ĠMedia Page ) Ġ{ ĊĠĠĠ Ġval Ġpage Monitor Ġ= Ġmonitor ( task ) ĊĠĠĠ Ġpage Monitor . state . set ( Cancelled ) ĊĠĠĠ Ġstate Updated Media Page Monitor s Ġ+= Ġpage Monitor ĊĠĠĠ ĠDownload History . remove ( task ) ĊĠ Ġ} Ċ }