@@ -44,32 +44,31 @@ function stopSpinner () {
4444
4545const dryRun = process . argv . slice ( 2 ) . some ( arg => arg === '--dry-run' )
4646
47+ const getWatchedShows = Promise . coroutine ( function * ( section ) {
48+ console . log ( `TV URI : ${ section } ` )
49+
50+ const allShows = path . join ( section , 'allLeaves' )
51+
52+ const viewed = ep => ep . viewCount
53+
54+ const allEpisodes = yield client . find ( allShows )
55+ const viewedEpisodes = allEpisodes . filter ( viewed )
56+
57+ return JSONPath ( { json : viewedEpisodes , path : '$..file' , resultType : 'parent' } ) . filter ( ep => ! ignore ( ep . file ) )
58+ } )
59+
4760Promise . coroutine ( function * ( ) {
4861 if ( dryRun ) {
4962 console . log ( logSymbols . info , 'Dry Run' )
5063 }
5164
52- const tvSections = yield client . find ( '/library/sections' , { type : 'show' } )
53-
54- if ( tvSections . length > 1 ) {
55- throw new Error ( 'Multiple TV sections found.' )
56- }
65+ const tvSections = ( yield client . find ( '/library/sections' , { type : 'show' } ) ) . map ( section => section . uri )
5766
5867 if ( tvSections . length === 0 ) {
5968 throw new Error ( 'No TV sections were found.' )
6069 }
6170
62- const televisionSection = tvSections [ 0 ] . uri
63- console . log ( `TV URI : ${ televisionSection } ` )
64-
65- const allShows = path . join ( televisionSection , 'allLeaves' )
66-
67- const viewed = ep => ep . viewCount
68-
69- const allEpisodes = yield client . find ( allShows )
70- const viewedEpisodes = allEpisodes . filter ( viewed )
71-
72- const filesToDelete = JSONPath ( { json : viewedEpisodes , path : '$..file' , resultType : 'parent' } ) . filter ( ep => ! ignore ( ep . file ) )
71+ const filesToDelete = [ ] . concat . apply ( [ ] , yield Promise . map ( tvSections , getWatchedShows ) )
7372
7473 filesToDelete . forEach ( ep => console . log ( ' %s %s | %s' , logSymbols . info , path . basename ( ep . file ) , chalk . green ( humanize . filesize ( ep . size ) ) ) )
7574
@@ -81,8 +80,7 @@ Promise.coroutine(function * () {
8180 if ( ! dryRun ) {
8281 try {
8382 yield Promise . map ( filesToDelete , ep => fs . unlinkAsync ( decodeURIComponent ( ep . file ) ) , { concurrency : 10 } )
84- // yield Promise.all(filesToDelete.map(ep => fs.unlinkAsync(decodeURIComponent(ep.file))))
85- yield client . perform ( path . join ( televisionSection , 'refresh' ) )
83+ yield Promise . map ( tvSections , televisionSection => client . perform ( path . join ( televisionSection , 'refresh' ) ) )
8684 } catch ( err ) {
8785 console . error ( 'Delete failed' , err )
8886 }
@@ -97,12 +95,12 @@ Promise.coroutine(function * () {
9795 console . log ( '%s Total%s deleted: %d episodes' , logSymbols . success , wouldBe , totalEpisodes )
9896 console . log ( '%s Space%s recovered: %s' , logSymbols . success , wouldBe , chalk . green ( humanize . filesize ( totalBytes ) ) )
9997 }
100-
101- function ignore ( filepath ) {
102- if ( dnd == null ) {
103- return false
104- }
105- return dnd . some ( show => ~ filepath . toLowerCase ( ) . indexOf ( show . toLowerCase ( ) ) )
106- }
10798} ) ( )
10899. catch ( error => console . error ( error ) )
100+
101+ function ignore ( filepath ) {
102+ if ( dnd == null ) {
103+ return false
104+ }
105+ return dnd . some ( show => ~ filepath . toLowerCase ( ) . indexOf ( show . toLowerCase ( ) ) )
106+ }
0 commit comments