@@ -46,72 +46,104 @@ class AvailabilitySync {
46
46
const pageSize = 50 ;
47
47
48
48
try {
49
- for await ( const mediaPage of this . loadAvailableMediaPaginated (
50
- pageSize
51
- ) ) {
49
+ for await ( const media of this . loadAvailableMediaPaginated ( pageSize ) ) {
52
50
try {
53
51
if ( ! this . running ) {
54
52
throw new Error ( 'Job aborted' ) ;
55
53
}
56
54
57
- for ( const media of mediaPage ) {
58
- const mediaExists = await this . mediaExists ( media ) ;
55
+ const mediaExists = await this . mediaExists ( media ) ;
56
+
57
+ //We can not delete media so if both versions do not exist, we will change both columns to unknown or null
58
+ if ( ! mediaExists ) {
59
+ if (
60
+ media . status !== MediaStatus . UNKNOWN ||
61
+ media . status4k !== MediaStatus . UNKNOWN
62
+ ) {
63
+ const request = await requestRepository . find ( {
64
+ relations : {
65
+ media : true ,
66
+ } ,
67
+ where : { media : { id : media . id } } ,
68
+ } ) ;
59
69
60
- //We can not delete media so if both versions do not exist, we will change both columns to unknown or null
61
- if ( ! mediaExists ) {
62
- if (
63
- media . status !== MediaStatus . UNKNOWN &&
64
- media . status4k !== MediaStatus . UNKNOWN
65
- ) {
66
- const request = await requestRepository . find ( {
67
- relations : {
68
- media : true ,
69
- } ,
70
- where : { media : { id : media . id } } ,
71
- } ) ;
72
-
73
- logger . debug (
74
- `${ media . tmdbId } does not exist in any of your media instances. We will change its status to unknown.` ,
75
- { label : 'AvailabilitySync' }
76
- ) ;
70
+ logger . debug (
71
+ `${
72
+ media . mediaType === 'tv' ? media . tvdbId : media . tmdbId
73
+ } does not exist in any of your media instances. We will change its status to unknown.`,
74
+ { label : 'AvailabilitySync' }
75
+ ) ;
76
+
77
+ await mediaRepository . update ( media . id , {
78
+ status : MediaStatus . UNKNOWN ,
79
+ status4k : MediaStatus . UNKNOWN ,
80
+ serviceId : null ,
81
+ serviceId4k : null ,
82
+ externalServiceId : null ,
83
+ externalServiceId4k : null ,
84
+ externalServiceSlug : null ,
85
+ externalServiceSlug4k : null ,
86
+ ratingKey : null ,
87
+ ratingKey4k : null ,
88
+ } ) ;
77
89
78
- await mediaRepository . update ( media . id , {
79
- status : MediaStatus . UNKNOWN ,
80
- status4k : MediaStatus . UNKNOWN ,
81
- serviceId : null ,
82
- serviceId4k : null ,
83
- externalServiceId : null ,
84
- externalServiceId4k : null ,
85
- externalServiceSlug : null ,
86
- externalServiceSlug4k : null ,
87
- ratingKey : null ,
88
- ratingKey4k : null ,
89
- } ) ;
90
-
91
- await requestRepository . remove ( request ) ;
92
- }
93
- continue ;
90
+ await requestRepository . remove ( request ) ;
94
91
}
92
+ }
95
93
96
- if ( media . mediaType === 'tv' ) {
97
- // ok, the show itself exists, but do all it's seasons?
98
- const seasons = await seasonRepository . find ( {
99
- where : [
100
- { status : MediaStatus . AVAILABLE , media : { id : media . id } } ,
101
- { status4k : MediaStatus . AVAILABLE , media : { id : media . id } } ,
102
- ] ,
103
- } ) ;
104
-
105
- let didDeleteSeasons = false ;
106
- for ( const season of seasons ) {
94
+ if ( media . mediaType === 'tv' ) {
95
+ // ok, the show itself exists, but do all it's seasons?
96
+ const seasons = await seasonRepository . find ( {
97
+ where : [
98
+ { status : MediaStatus . AVAILABLE , media : { id : media . id } } ,
99
+ {
100
+ status : MediaStatus . PARTIALLY_AVAILABLE ,
101
+ media : { id : media . id } ,
102
+ } ,
103
+ { status4k : MediaStatus . AVAILABLE , media : { id : media . id } } ,
104
+ {
105
+ status4k : MediaStatus . PARTIALLY_AVAILABLE ,
106
+ media : { id : media . id } ,
107
+ } ,
108
+ ] ,
109
+ } ) ;
110
+
111
+ let didDeleteSeasons = false ;
112
+ for ( const season of seasons ) {
113
+ if (
114
+ ! mediaExists &&
115
+ ( season . status !== MediaStatus . UNKNOWN ||
116
+ season . status4k !== MediaStatus . UNKNOWN )
117
+ ) {
118
+ await seasonRepository . update (
119
+ { id : season . id } ,
120
+ {
121
+ status : MediaStatus . UNKNOWN ,
122
+ status4k : MediaStatus . UNKNOWN ,
123
+ }
124
+ ) ;
125
+ } else {
107
126
const seasonExists = await this . seasonExists ( media , season ) ;
108
127
109
128
if ( ! seasonExists ) {
110
129
logger . debug (
111
- `Removing season ${ season . seasonNumber } , media id: ${ media . tmdbId } because it doesn't appear in any library .` ,
130
+ `Removing season ${ season . seasonNumber } , media id: ${ media . tvdbId } because it does not exist in any of your media instances .` ,
112
131
{ label : 'AvailabilitySync' }
113
132
) ;
114
133
134
+ if (
135
+ season . status !== MediaStatus . UNKNOWN ||
136
+ season . status4k !== MediaStatus . UNKNOWN
137
+ ) {
138
+ await seasonRepository . update (
139
+ { id : season . id } ,
140
+ {
141
+ status : MediaStatus . UNKNOWN ,
142
+ status4k : MediaStatus . UNKNOWN ,
143
+ }
144
+ ) ;
145
+ }
146
+
115
147
const seasonToBeDeleted =
116
148
await seasonRequestRepository . findOne ( {
117
149
relations : {
@@ -129,8 +161,6 @@ class AvailabilitySync {
129
161
} ,
130
162
} ) ;
131
163
132
- await seasonRepository . delete ( season . id ) ;
133
-
134
164
if ( seasonToBeDeleted ) {
135
165
await seasonRequestRepository . remove ( seasonToBeDeleted ) ;
136
166
}
@@ -145,7 +175,7 @@ class AvailabilitySync {
145
175
media . status4k === MediaStatus . AVAILABLE
146
176
) {
147
177
logger . debug (
148
- `Marking media id: ${ media . tmdbId } as PARTIALLY_AVAILABLE because we deleted some of its seasons.` ,
178
+ `Marking media id: ${ media . tvdbId } as PARTIALLY_AVAILABLE because we deleted some of its seasons.` ,
149
179
{ label : 'AvailabilitySync' }
150
180
) ;
151
181
@@ -198,20 +228,15 @@ class AvailabilitySync {
198
228
{ status4k : MediaStatus . PARTIALLY_AVAILABLE } ,
199
229
] ;
200
230
201
- let mediaPage = await mediaRepository . find ( {
202
- where : whereOptions ,
203
- skip : offset ,
204
- take : pageSize ,
205
- } ) ;
231
+ let mediaPage : Media [ ] ;
206
232
207
233
do {
208
- yield mediaPage ;
209
- offset += pageSize ;
210
- mediaPage = await mediaRepository . find ( {
234
+ yield * ( mediaPage = await mediaRepository . find ( {
211
235
where : whereOptions ,
212
236
skip : offset ,
213
237
take : pageSize ,
214
- } ) ;
238
+ } ) ) ;
239
+ offset += pageSize ;
215
240
} while ( mediaPage . length > 0 ) ;
216
241
}
217
242
@@ -285,7 +310,6 @@ class AvailabilitySync {
285
310
286
311
//check if both exist or if a single non-4k or 4k exists
287
312
//if both do not exist we will return false
288
-
289
313
if ( ! server . is4k && ! meta . id ) {
290
314
existsInRadarr = false ;
291
315
}
@@ -309,7 +333,6 @@ class AvailabilitySync {
309
333
310
334
//if only a single non-4k or 4k exists, then change entity columns accordingly
311
335
//related media request will then be deleted
312
-
313
336
if ( ! existsInRadarr && existsInRadarr4k && ! existsInPlex ) {
314
337
if ( media . status !== MediaStatus . UNKNOWN ) {
315
338
this . mediaUpdater ( media , false ) ;
@@ -353,7 +376,6 @@ class AvailabilitySync {
353
376
354
377
//check if both exist or if a single non-4k or 4k exists
355
378
//if both do not exist we will return false
356
-
357
379
if ( ! server . is4k && ! meta . id ) {
358
380
existsInSonarr = false ;
359
381
}
@@ -377,7 +399,6 @@ class AvailabilitySync {
377
399
378
400
//if only a single non-4k or 4k exists, then change entity columns accordingly
379
401
//related media request will then be deleted
380
-
381
402
if ( ! existsInSonarr && existsInSonarr4k && ! existsInPlex ) {
382
403
if ( media . status !== MediaStatus . UNKNOWN ) {
383
404
this . mediaUpdater ( media , false ) ;
@@ -402,7 +423,7 @@ class AvailabilitySync {
402
423
season : Season ,
403
424
seasonExistsInPlex : boolean ,
404
425
seasonExistsInPlex4k : boolean
405
- ) {
426
+ ) : Promise < boolean > {
406
427
if ( ! media . tvdbId ) {
407
428
return false ;
408
429
}
@@ -470,7 +491,6 @@ class AvailabilitySync {
470
491
471
492
//if season does not exist, we will change status to unknown and delete related season request
472
493
//if parent media request is empty(all related seasons have been removed), parent is automatically deleted
473
-
474
494
if (
475
495
! seasonExistsInSonarr &&
476
496
seasonExistsInSonarr4k &&
@@ -491,7 +511,7 @@ class AvailabilitySync {
491
511
492
512
if ( media . status === MediaStatus . AVAILABLE ) {
493
513
logger . debug (
494
- `Marking media id: ${ media . tmdbId } as PARTIALLY_AVAILABLE because we deleted one of its seasons.` ,
514
+ `Marking media id: ${ media . tvdbId } as PARTIALLY_AVAILABLE because we deleted one of its seasons.` ,
495
515
{ label : 'AvailabilitySync' }
496
516
) ;
497
517
await mediaRepository . update ( media . id , {
@@ -521,7 +541,7 @@ class AvailabilitySync {
521
541
522
542
if ( media . status4k === MediaStatus . AVAILABLE ) {
523
543
logger . debug (
524
- `Marking media id: ${ media . tmdbId } as PARTIALLY_AVAILABLE because we deleted one of its seasons.` ,
544
+ `Marking media id: ${ media . tvdbId } as PARTIALLY_AVAILABLE because we deleted one of its seasons.` ,
525
545
{ label : 'AvailabilitySync' }
526
546
) ;
527
547
await mediaRepository . update ( media . id , {
@@ -546,7 +566,6 @@ class AvailabilitySync {
546
566
let existsInPlex4k = false ;
547
567
548
568
//check each plex instance to see if media exists
549
-
550
569
try {
551
570
if ( ratingKey ) {
552
571
const meta = await this . plexClient ?. getMetadata ( ratingKey ) ;
@@ -566,16 +585,13 @@ class AvailabilitySync {
566
585
throw ex ;
567
586
}
568
587
}
569
-
570
- //base case for if both exist in plex
571
-
588
+ //base case for if both media versions exist in plex
572
589
if ( existsInPlex && existsInPlex4k ) {
573
590
return true ;
574
591
}
575
592
576
593
//we then check radarr or sonarr has that specific media. If not, then we will move to delete
577
594
//if a non-4k or 4k version exists in at least one of the instances, we will only update that specific version
578
-
579
595
if ( media . mediaType === 'movie' ) {
580
596
const existsInRadarr = await this . mediaExistsInRadarr (
581
597
media ,
@@ -584,7 +600,6 @@ class AvailabilitySync {
584
600
) ;
585
601
586
602
//if true, media exists in at least one radarr or plex instance.
587
-
588
603
if ( existsInRadarr ) {
589
604
logger . warn (
590
605
`${ media . tmdbId } exists in at least one radarr or plex instance. Media will be updated if set to available.` ,
@@ -605,10 +620,9 @@ class AvailabilitySync {
605
620
) ;
606
621
607
622
//if true, media exists in at least one sonarr or plex instance.
608
-
609
623
if ( existsInSonarr ) {
610
624
logger . warn (
611
- `${ media . tmdbId } exists in at least one sonarr or plex instance. Media will be updated if set to available.` ,
625
+ `${ media . tvdbId } exists in at least one sonarr or plex instance. Media will be updated if set to available.` ,
612
626
{
613
627
label : 'AvailabilitySync' ,
614
628
}
@@ -658,6 +672,7 @@ class AvailabilitySync {
658
672
}
659
673
}
660
674
675
+ //base case for if both season versions exist in plex
661
676
if ( seasonExistsInPlex && seasonExistsInPlex4k ) {
662
677
return true ;
663
678
}
0 commit comments