-
Notifications
You must be signed in to change notification settings - Fork 278
/
show_queue.py
1145 lines (943 loc) · 42.8 KB
/
show_queue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding=utf-8
# Author: Nic Wolfe <[email protected]>
#
# This file is part of Medusa.
#
# Medusa is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Medusa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Medusa. If not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals
import logging
import os
import traceback
from builtins import object
from imdbpie.exceptions import ImdbAPIError
from medusa import (
app,
generic_queue,
name_cache,
notifiers,
scene_numbering,
ui,
)
from medusa.black_and_white_list import BlackAndWhiteList
from medusa.common import WANTED, statusStrings
from medusa.helper.common import episode_num, sanitize_filename
from medusa.helper.exceptions import (
CantRefreshShowException,
CantRemoveShowException,
CantUpdateShowException,
EpisodeDeletedException,
MultipleShowObjectsException,
ShowDirectoryNotFoundException
)
from medusa.helpers import (
chmod_as_parent,
delete_empty_folders,
get_showname_from_indexer,
make_dir,
)
from medusa.helpers.externals import check_existing_shows
from medusa.image_cache import replace_images
from medusa.indexers.indexer_api import indexerApi
from medusa.indexers.indexer_exceptions import (
IndexerAttributeNotFound,
IndexerError,
IndexerException,
IndexerShowAlreadyInLibrary,
IndexerShowIncomplete,
IndexerShowNotFound,
IndexerShowNotFoundInLanguage,
)
from medusa.logger.adapters.style import BraceAdapter
from medusa.tv import Series
from requests import RequestException
from six import binary_type, text_type, viewitems
from traktor import TraktException
log = BraceAdapter(logging.getLogger(__name__))
log.logger.addHandler(logging.NullHandler())
class ShowQueueActions(object):
def __init__(self):
pass
REFRESH = 1
ADD = 2
UPDATE = 3
RENAME = 5
SUBTITLE = 6
REMOVE = 7
SEASON_UPDATE = 8
names = {
REFRESH: 'Refresh',
ADD: 'Add',
UPDATE: 'Update',
RENAME: 'Rename',
SUBTITLE: 'Subtitle',
REMOVE: 'Remove Show',
SEASON_UPDATE: 'Season Update',
}
class ShowQueue(generic_queue.GenericQueue):
mappings = {
ShowQueueActions.ADD: 'This show is in the process of being downloaded - the info below is incomplete.',
ShowQueueActions.UPDATE: 'The information on this page is in the process of being updated.',
ShowQueueActions.SEASON_UPDATE: 'The information on this page is in the process of being updated.',
ShowQueueActions.REFRESH: 'The episodes below are currently being refreshed from disk',
ShowQueueActions.SUBTITLE: 'Currently downloading subtitles for this show',
}
queue_mappings = {
ShowQueueActions.REFRESH: 'This show is queued to be refreshed.',
ShowQueueActions.UPDATE: 'This show is queued and awaiting an update.',
ShowQueueActions.SEASON_UPDATE: 'This show is queued and awaiting a season update.',
ShowQueueActions.SUBTITLE: 'This show is queued and awaiting subtitles download.',
}
def __init__(self):
generic_queue.GenericQueue.__init__(self)
self.queue_name = 'SHOWQUEUE'
def _isInQueue(self, show, actions):
if not show:
return False
return show.series_id in [x.show.series_id if x.show else 0 for x in self.queue if x.action_id in actions]
def _isBeingSomethinged(self, show, actions):
return self.currentItem is not None and show == self.currentItem.show and self.currentItem.action_id in actions
def isInUpdateQueue(self, show):
return self._isInQueue(show, (ShowQueueActions.UPDATE,))
def isInRefreshQueue(self, show):
return self._isInQueue(show, (ShowQueueActions.REFRESH,))
def isInRenameQueue(self, show):
return self._isInQueue(show, (ShowQueueActions.RENAME,))
def isInSubtitleQueue(self, show):
return self._isInQueue(show, (ShowQueueActions.SUBTITLE,))
def isInRemoveQueue(self, show):
return self._isInQueue(show, (ShowQueueActions.REMOVE,))
def isBeingAdded(self, show):
return self._isBeingSomethinged(show, (ShowQueueActions.ADD,))
def isBeingUpdated(self, show):
return self._isBeingSomethinged(show, (ShowQueueActions.UPDATE,))
def isBeingRefreshed(self, show):
return self._isBeingSomethinged(show, (ShowQueueActions.REFRESH,))
def isBeingRenamed(self, show):
return self._isBeingSomethinged(show, (ShowQueueActions.RENAME,))
def isBeingSubtitled(self, show):
return self._isBeingSomethinged(show, (ShowQueueActions.SUBTITLE,))
def isBeingRemoved(self, show):
return self._isBeingSomethinged(show, (ShowQueueActions.REMOVE,))
def _getLoadingShowList(self):
return [x for x in self.queue + [self.currentItem] if x is not None and x.isLoading]
def getQueueActionMessage(self, show):
return self.get_queue_action(show)[1]
def get_queue_action(self, show):
for action, message in viewitems(self.mappings):
if self._isBeingSomethinged(show, (action, )):
return action, message
for action, message in viewitems(self.queue_mappings):
if self._isInQueue(show, (action, )):
return action, message
return None, None
loadingShowList = property(_getLoadingShowList)
def updateShow(self, show, season=None):
if self.isBeingAdded(show):
raise CantUpdateShowException(
'{show_name} is still being added,'
' wait until it is finished before you update.'.format(show_name=show.name)
)
if self.isBeingUpdated(show):
raise CantUpdateShowException(
'{show_name} is already being updated by Post-processor or manually started,'
" can't update again until it's done.".format(show_name=show.name)
)
if self.isInUpdateQueue(show):
raise CantUpdateShowException(
'{show_name} is in process of being updated by Post-processor or manually started,'
" can't update again until it's done.".format(show_name=show.name)
)
queue_item_update_show = QueueItemUpdate(show) if season is None else QueueItemSeasonUpdate(show, season)
self.add_item(queue_item_update_show)
return queue_item_update_show
def refreshShow(self, show, force=False):
if self.isBeingRefreshed(show) and not force:
raise CantRefreshShowException('This show is already being refreshed, not refreshing again.')
if (self.isBeingUpdated(show) or self.isInUpdateQueue(show)) and not force:
log.debug('A refresh was attempted but there is already an update queued or in progress.'
" Since updates do a refresh at the end anyway I'm skipping this request.")
return
queueItemObj = QueueItemRefresh(show, force=force)
log.debug('{id}: Queueing show refresh for {show}', {'id': show.series_id, 'show': show.name})
self.add_item(queueItemObj)
return queueItemObj
def renameShowEpisodes(self, show):
queueItemObj = QueueItemRename(show)
self.add_item(queueItemObj)
return queueItemObj
def download_subtitles(self, show):
queueItemObj = QueueItemSubtitle(show)
self.add_item(queueItemObj)
return queueItemObj
def addShow(self, indexer, indexer_id, showDir, default_status=None, quality=None, season_folders=None,
lang=None, subtitles=None, anime=None, scene=None, paused=None, blacklist=None, whitelist=None,
default_status_after=None, root_dir=None):
if lang is None:
lang = app.INDEXER_DEFAULT_LANGUAGE
queueItemObj = QueueItemAdd(indexer, indexer_id, showDir, default_status, quality, season_folders, lang,
subtitles, anime, scene, paused, blacklist, whitelist, default_status_after,
root_dir)
self.add_item(queueItemObj)
return queueItemObj
def removeShow(self, show, full=False):
if show is None:
raise CantRemoveShowException('Failed removing show: Show does not exist')
if not hasattr(show, 'indexerid'):
raise CantRemoveShowException('Failed removing show: Show does not have an indexer id')
if self.isBeingRemoved(show):
raise CantRemoveShowException('[{!s}]: Show is already being removed'.format(show.series_id))
if self.isInRemoveQueue(show):
raise CantRemoveShowException('[{!s}]: Show is already queued to be removed'.format(show.series_id))
# remove other queued actions for this show.
for item in self.queue:
if item and item.show and item != self.currentItem and show.identifier == item.show.identifier:
self.queue.remove(item)
queue_item_obj = QueueItemRemove(show=show, full=full)
self.add_item(queue_item_obj)
# Show removal has been queued, let's update the app.RECENTLY_DELETED global, to keep track of it
app.RECENTLY_DELETED.update([show.indexer_slug])
return queue_item_obj
class ShowQueueItem(generic_queue.QueueItem):
"""
Represents an item in the queue waiting to be executed.
Can be either:
- show being added (may or may not be associated with a show object)
- show being refreshed
- show being updated
- show being force updated
- show being subtitled
"""
def __init__(self, action_id, show):
generic_queue.QueueItem.__init__(self, ShowQueueActions.names[action_id], action_id)
self.show = show
def isInQueue(self):
return self in app.show_queue_scheduler.action.queue + [
app.show_queue_scheduler.action.currentItem] # @UndefinedVariable
def _getName(self):
return text_type(self.show.series_id)
def _isLoading(self):
return False
show_name = property(_getName)
isLoading = property(_isLoading)
class QueueItemAdd(ShowQueueItem):
def __init__(self, indexer, indexer_id, showDir, default_status, quality, season_folders, lang, subtitles, anime,
scene, paused, blacklist, whitelist, default_status_after, root_dir):
if isinstance(showDir, binary_type):
self.showDir = text_type(showDir, 'utf-8')
else:
self.showDir = showDir
self.indexer = indexer
self.indexer_id = indexer_id
self.default_status = default_status
self.quality = quality
self.season_folders = season_folders
self.lang = lang
self.subtitles = subtitles
self.anime = anime
self.scene = scene
self.paused = paused
self.blacklist = blacklist
self.whitelist = whitelist
self.default_status_after = default_status_after
self.root_dir = root_dir
self.show = None
# this will initialize self.show to None
ShowQueueItem.__init__(self, ShowQueueActions.ADD, self.show)
# Process add show in priority
self.priority = generic_queue.QueuePriorities.HIGH
def _getName(self):
"""
Returns the show name if there is a show object created, if not returns
the dir that the show is being added to.
"""
if self.show is None:
return self.showDir
return self.show.name
show_name = property(_getName)
def _isLoading(self):
"""
Returns True if we've gotten far enough to have a show object, or False
if we still only know the folder name.
"""
if self.show is None:
return True
return False
isLoading = property(_isLoading)
def run(self):
ShowQueueItem.run(self)
log.info(
'Starting to add show by {0}',
('ShowDir: {0}'.format(self.showDir)
if self.showDir else
'Indexer Id: {0}'.format(self.indexer_id))
)
# make sure the Indexer IDs are valid
try:
l_indexer_api_params = indexerApi(self.indexer).api_params.copy()
if self.lang:
l_indexer_api_params['language'] = self.lang
log.info('{indexer_name}: {indexer_params!r}', {
'indexer_name': indexerApi(self.indexer).name,
'indexer_params': l_indexer_api_params
})
indexer_api = indexerApi(self.indexer).indexer(**l_indexer_api_params)
s = indexer_api[self.indexer_id]
# Let's try to create the show Dir if it's not provided. This way we force the show dir
# to build build using the Indexers provided series name
if not self.showDir and self.root_dir:
show_name = get_showname_from_indexer(self.indexer, self.indexer_id, self.lang)
if show_name:
self.showDir = os.path.join(self.root_dir, sanitize_filename(show_name))
dir_exists = make_dir(self.showDir)
if not dir_exists:
log.info("Unable to create the folder {0}, can't add the show", self.showDir)
return
chmod_as_parent(self.showDir)
else:
log.info("Unable to get a show {0}, can't add the show", self.showDir)
return
# this usually only happens if they have an NFO in their show dir which gave us a Indexer ID that
# has no proper english version of the show
if getattr(s, 'seriesname', None) is None:
log.error(
'Show in {path} has no name on {indexer}, probably searched with the wrong language.',
{'path': self.showDir, 'indexer': indexerApi(self.indexer).name}
)
ui.notifications.error(
'Unable to add show',
'Show in {path} has no name on {indexer}, probably the wrong language.'
' Delete .nfo and manually add the correct language.'.format(
path=self.showDir, indexer=indexerApi(self.indexer).name)
)
self._finishEarly()
return
# if the show has no episodes/seasons
if not s:
log.info(
'Show {series_name} is on {indexer_name} but contains no season/episode data.',
{'series_name': s['seriesname'], 'indexer_name': indexerApi(self.indexer).name}
)
ui.notifications.error(
'Unable to add show',
'Show {series_name} is on {indexer_name} but contains no season/episode data.'.format(
series_name=s['seriesname'], indexer_name=indexerApi(self.indexer).name)
)
self._finishEarly()
return
# Check if we can already find this show in our current showList.
try:
check_existing_shows(s, self.indexer)
except IndexerShowAlreadyInLibrary as error:
log.warning(
'Could not add the show {series}, as it already is in your library.'
' Error: {error}',
{'series': s['seriesname'], 'error': error}
)
ui.notifications.error(
'Unable to add show',
'reason: {0}'.format(error)
)
self._finishEarly()
# Clean up leftover if the newly created directory is empty.
delete_empty_folders(self.showDir)
return
# TODO: Add more specific indexer exceptions, that should provide the user with some accurate feedback.
except IndexerShowNotFound:
log.warning(
'{id}: Unable to look up the show in {path} using id {id} on {indexer}.'
' Delete metadata files from the folder and try adding it again.',
{'id': self.indexer_id, 'path': self.showDir, 'indexer': indexerApi(self.indexer).name}
)
ui.notifications.error(
'Unable to add show',
'Unable to look up the show in {path} using id {id} on {indexer}.'
' Delete metadata files from the folder and try adding it again.'.format(
path=self.showDir, id=self.indexer_id, indexer=indexerApi(self.indexer).name)
)
self._finishEarly()
return
except IndexerShowIncomplete as error:
log.warning(
'{id}: Error while loading information from indexer {indexer}. Error: {error}',
{'id': self.indexer_id, 'indexer': indexerApi(self.indexer).name, 'error': error}
)
ui.notifications.error(
'Unable to add show',
'Unable to look up the show in {path} on {indexer} using ID {id}'
' Reason: {error}'.format(
path=self.showDir, indexer=indexerApi(self.indexer).name,
id=self.indexer_id, error=error)
)
self._finishEarly()
return
except IndexerShowNotFoundInLanguage as error:
log.warning(
'{id}: Data retrieved from {indexer} was incomplete. The indexer does not provide'
' show information in the searched language {language}. Aborting: {error_msg}',
{'id': self.indexer_id, 'indexer': indexerApi(self.indexer).name,
'language': error.language, 'error_msg': error}
)
ui.notifications.error(
'Error adding show!',
'Unable to add show {indexer_id} on {indexer} with this language: {language}'.format(
indexer_id=self.indexer_id, indexer=indexerApi(self.indexer).name, language=error.language)
)
self._finishEarly()
return
except Exception as error:
log.error(
'{id}: Error while loading information from indexer {indexer}. Error: {error!r}',
{'id': self.indexer_id, 'indexer': indexerApi(self.indexer).name, 'error': error}
)
ui.notifications.error(
'Unable to add show',
'Unable to look up the show in {path} on {indexer} using ID {id}.'.format(
path=self.showDir, indexer=indexerApi(self.indexer).name, id=self.indexer_id)
)
self._finishEarly()
return
try:
newShow = Series(self.indexer, self.indexer_id, self.lang)
newShow.load_from_indexer(indexer_api)
self.show = newShow
# set up initial values
self.show.location = self.showDir
self.show.subtitles = self.subtitles if self.subtitles is not None else app.SUBTITLES_DEFAULT
self.show.quality = self.quality if self.quality else app.QUALITY_DEFAULT
self.show.season_folders = self.season_folders if self.season_folders is not None \
else app.SEASON_FOLDERS_DEFAULT
self.show.anime = self.anime if self.anime is not None else app.ANIME_DEFAULT
self.show.scene = self.scene if self.scene is not None else app.SCENE_DEFAULT
self.show.paused = self.paused if self.paused is not None else False
# set up default new/missing episode status
log.info(
'Setting all previously aired episodes to the specified status: {status}',
{'status': statusStrings[self.default_status]}
)
self.show.default_ep_status = self.default_status
if self.show.anime:
self.show.release_groups = BlackAndWhiteList(self.show)
if self.blacklist:
self.show.release_groups.set_black_keywords(self.blacklist)
if self.whitelist:
self.show.release_groups.set_white_keywords(self.whitelist)
# # be smartish about this
# if self.show.genre and "talk show" in self.show.genre.lower():
# self.show.air_by_date = 1
# if self.show.genre and "documentary" in self.show.genre.lower():
# self.show.air_by_date = 0
# if self.show.classification and "sports" in self.show.classification.lower():
# self.show.sports = 1
except IndexerException as error:
log.error(
'Unable to add show due to an error with {indexer}: {error}',
{'indexer': indexerApi(self.indexer).name, 'error': error}
)
ui.notifications.error(
'Unable to add {series_name} due to an error with {indexer_name}'.format(
series_name=self.show.name if self.show else 'show',
indexer_name=indexerApi(self.indexer).name
)
)
self._finishEarly()
return
except MultipleShowObjectsException:
log.warning(
'The show in {show_dir} is already in your show list, skipping',
{'show_dir': self.showDir}
)
ui.notifications.error(
'Show skipped',
'The show in {show_dir} is already in your show list'.format(
show_dir=self.showDir)
)
self._finishEarly()
return
except Exception as error:
log.error('Error trying to add show: {0}', error)
log.debug(traceback.format_exc())
self._finishEarly()
raise
log.debug('Retrieving show info from IMDb')
try:
self.show.load_imdb_info()
except ImdbAPIError as error:
log.info('Something wrong on IMDb api: {0}', error)
except RequestException as error:
log.warning('Error loading IMDb info: {0}', error)
try:
log.debug(
'{id}: Saving new show to database',
{'id': self.show.series_id}
)
self.show.save_to_db()
except Exception as error:
log.error('Error saving the show to the database: {0}', error)
log.debug(traceback.format_exc())
self._finishEarly()
raise
# add it to the show list
app.showList.append(self.show)
try:
self.show.load_episodes_from_indexer(tvapi=indexer_api)
except Exception as error:
log.error(
'Error with {indexer}, not creating episode list: {error}',
{'indexer': indexerApi(self.show.indexer).name, 'error': error}
)
log.debug(traceback.format_exc())
# update internal name cache
name_cache.build_name_cache(self.show)
try:
self.show.load_episodes_from_dir()
except Exception as error:
log.error('Error searching dir for episodes: {0}', error)
log.debug(traceback.format_exc())
# if they set default ep status to WANTED then run the backlog to search for episodes
# FIXME: This needs to be a backlog queue item!!!
if self.show.default_ep_status == WANTED:
log.info('Launching backlog for this show since its episodes are WANTED')
app.backlog_search_scheduler.action.search_backlog([self.show])
self.show.write_metadata()
self.show.update_metadata()
self.show.populate_cache()
self.show.flush_episodes()
if app.USE_TRAKT:
# if there are specific episodes that need to be added by trakt
app.trakt_checker_scheduler.action.manage_new_show(self.show)
# add show to trakt.tv library
if app.TRAKT_SYNC:
app.trakt_checker_scheduler.action.add_show_trakt_library(self.show)
if app.TRAKT_SYNC_WATCHLIST:
log.info('update watchlist')
notifiers.trakt_notifier.update_watchlist(show_obj=self.show)
# Load XEM data to DB for show
scene_numbering.xem_refresh(self.show, force=True)
# check if show has XEM mapping so we can determine if searches
# should go by scene numbering or indexer numbering.
if not self.scene and scene_numbering.get_xem_numbering_for_show(self.show):
self.show.scene = 1
# After initial add, set to default_status_after.
self.show.default_ep_status = self.default_status_after
try:
log.debug(
'{id}: Saving new show info to database',
{'id': self.show.series_id}
)
self.show.save_to_db()
except Exception as error:
log.warning(
'{id}: Error saving new show info to database: {error_msg}',
{'id': self.show.series_id, 'error_msg': error}
)
log.error(traceback.format_exc())
self.finish()
def _finishEarly(self):
if self.show is not None:
app.show_queue_scheduler.action.removeShow(self.show)
self.finish()
class QueueItemRefresh(ShowQueueItem):
def __init__(self, show=None, force=False):
ShowQueueItem.__init__(self, ShowQueueActions.REFRESH, show)
# do refreshes first because they're quick
self.priority = generic_queue.QueuePriorities.HIGH
# force refresh certain items
self.force = force
def run(self):
ShowQueueItem.run(self)
log.info(
'{id}: Performing refresh on {show}',
{'id': self.show.series_id, 'show': self.show.name}
)
try:
self.show.refresh_dir()
if self.force:
self.show.update_metadata()
self.show.write_metadata()
self.show.populate_cache()
# Load XEM data to DB for show
scene_numbering.xem_refresh(self.show)
except Exception as error:
log.error(
'{id}: Error while refreshing show {show}. Error: {error_msg}',
{'id': self.show.series_id, 'show': self.show.name, 'error_msg': error}
)
self.finish()
class QueueItemRename(ShowQueueItem):
def __init__(self, show=None):
ShowQueueItem.__init__(self, ShowQueueActions.RENAME, show)
def run(self):
ShowQueueItem.run(self)
log.info(
'Performing rename on {series_name}',
{'series_name': self.show.name}
)
try:
self.show.validate_location
except ShowDirectoryNotFoundException:
log.warning(
"Can't perform rename on {series_name} when the show dir is missing.",
{'series_name': self.show.name}
)
return
ep_obj_rename_list = []
ep_obj_list = self.show.get_all_episodes(has_location=True)
for cur_ep_obj in ep_obj_list:
# Only want to rename if we have a location
if cur_ep_obj.location:
if cur_ep_obj.related_episodes:
# do we have one of multi-episodes in the rename list already
have_already = False
for cur_related_ep in cur_ep_obj.related_episodes + [cur_ep_obj]:
if cur_related_ep in ep_obj_rename_list:
have_already = True
break
if not have_already:
ep_obj_rename_list.append(cur_ep_obj)
else:
ep_obj_rename_list.append(cur_ep_obj)
for cur_ep_obj in ep_obj_rename_list:
cur_ep_obj.rename()
self.finish()
class QueueItemSubtitle(ShowQueueItem):
def __init__(self, show=None):
ShowQueueItem.__init__(self, ShowQueueActions.SUBTITLE, show)
def run(self):
ShowQueueItem.run(self)
log.info(
'{id}: Downloading subtitles for {show}',
{'id': self.show.series_id, 'show': self.show.name}
)
self.show.download_subtitles()
self.finish()
class QueueItemUpdate(ShowQueueItem):
def __init__(self, show):
"""Use QueueItem to perform full show updates.
:param show: Show object to update.
"""
ShowQueueItem.__init__(self, ShowQueueActions.UPDATE, show)
self.priority = generic_queue.QueuePriorities.HIGH
def run(self):
ShowQueueItem.run(self)
log.debug(
'{id}: Beginning update of {show}',
{'id': self.show.series_id, 'show': self.show.name}
)
log.debug(
'{id}: Retrieving show info from {indexer}',
{'id': self.show.series_id, 'indexer': indexerApi(self.show.indexer).name}
)
try:
# Let's make sure we refresh the indexer_api object attached to the show object.
self.show.create_indexer()
self.show.load_from_indexer()
except IndexerError as error:
log.warning(
'{id}: Unable to contact {indexer}. Aborting: {error_msg}',
{'id': self.show.series_id, 'indexer': indexerApi(self.show.indexer).name, 'error_msg': error}
)
return
except IndexerAttributeNotFound as error:
log.warning(
'{id}: Data retrieved from {indexer} was incomplete. Aborting: {error_msg}',
{'id': self.show.series_id, 'indexer': indexerApi(self.show.indexer).name, 'error_msg': error}
)
return
except IndexerShowNotFoundInLanguage as error:
log.warning(
'{id}: Data retrieved from {indexer} was incomplete. The indexer does not provide'
' show information in the searched language {language}. Aborting: {error_msg}',
{'id': self.show.series_id, 'indexer': indexerApi(self.show.indexer).name,
'language': error.language, 'error_msg': error}
)
ui.notifications.error(
'Error changing language show!',
'Unable to change language for show {show_name}'
' on {indexer} to language: {language}'.format(
show_name=self.show.name, indexer=indexerApi(self.show.indexer).name,
language=error.language)
)
return
log.debug(
'{id}: Retrieving show info from IMDb',
{'id': self.show.series_id}
)
try:
self.show.load_imdb_info()
except ImdbAPIError as error:
log.info(
'{id}: Something wrong on IMDb api: {error_msg}',
{'id': self.show.series_id, 'error_msg': error}
)
except RequestException as error:
log.warning(
'{id}: Error loading IMDb info: {error_msg}',
{'id': self.show.series_id, 'error_msg': error}
)
# have to save show before reading episodes from db
try:
log.debug(
'{id}: Saving new IMDb show info to database',
{'id': self.show.series_id}
)
self.show.save_to_db()
except Exception as error:
log.warning(
'{id}: Error saving new IMDb show info to database: {error_msg}',
{'id': self.show.series_id, 'error_msg': error}
)
log.error(traceback.format_exc())
# get episode list from DB
try:
episodes_from_db = self.show.load_episodes_from_db()
except IndexerException as error:
log.warning(
'{id}: Unable to contact {indexer}. Aborting: {error_msg}',
{'id': self.show.series_id, 'indexer': indexerApi(self.show.indexer).name,
'error_msg': error}
)
return
# get episode list from the indexer
try:
episodes_from_indexer = self.show.load_episodes_from_indexer()
except IndexerException as error:
log.warning(
'{id}: Unable to get info from {indexer}. The show info will not be refreshed.'
' Error: {error_msg}',
{'id': self.show.series_id, 'indexer': indexerApi(self.show.indexer).name,
'error_msg': error}
)
episodes_from_indexer = None
if episodes_from_indexer is None:
log.warning(
'{id}: No data returned from {indexer} during full show update.'
' Unable to update this show',
{'id': self.show.series_id, 'indexer': indexerApi(self.show.indexer).name}
)
else:
# for each ep we found on the Indexer delete it from the DB list
for cur_season in episodes_from_indexer:
for cur_episode in episodes_from_indexer[cur_season]:
if cur_season in episodes_from_db and cur_episode in episodes_from_db[cur_season]:
del episodes_from_db[cur_season][cur_episode]
# remaining episodes in the DB list are not on the indexer, just delete them from the DB
for cur_season in episodes_from_db:
for cur_episode in episodes_from_db[cur_season]:
log.debug(
'{id}: Permanently deleting episode {show} {ep} from the database',
{'id': self.show.series_id, 'show': self.show.name,
'ep': episode_num(cur_season, cur_episode)}
)
# Create the ep object only because Im going to delete it
ep_obj = self.show.get_episode(cur_season, cur_episode)
try:
ep_obj.delete_episode()
except EpisodeDeletedException:
log.debug(
'{id}: Episode {show} {ep} successfully deleted from the database',
{'id': self.show.series_id, 'show': self.show.name,
'ep': episode_num(cur_season, cur_episode)}
)
# Save only after all changes were applied
try:
log.debug(
'{id}: Saving all updated show info to database',
{'id': self.show.series_id}
)
self.show.save_to_db()
except Exception as error:
log.warning(
'{id}: Error saving all updated show info to database: {error_msg}',
{'id': self.show.series_id, 'error_msg': error}
)
log.error(traceback.format_exc())
# Replace the images in cache
log.info(
'{id}: Replacing images for show {show}',
{'id': self.show.series_id, 'show': self.show.name}
)
replace_images(self.show)
log.debug(
'{id}: Finished update of {show}',
{'id': self.show.series_id, 'show': self.show.name}
)
# Refresh show needs to be forced since current execution locks the queue
app.show_queue_scheduler.action.refreshShow(self.show, True)
self.finish()
class QueueItemSeasonUpdate(ShowQueueItem):
def __init__(self, show, season):
"""Use QueueItem to partly show updates based on season.
:param show: Show object to update.
:param season: Season of the show to update, or list of seasons (int).
"""
ShowQueueItem.__init__(self, ShowQueueActions.SEASON_UPDATE, show)
self.priority = generic_queue.QueuePriorities.HIGH
self.seasons = [season] if not isinstance(season, list) else season
def run(self):
ShowQueueItem.run(self)
log.info(
'{id}: Beginning update of {show}{season}',
{'id': self.show.series_id,
'show': self.show.name,
'season': ' with season(s) [{0}]'.format(
','.join(text_type(s) for s in self.seasons) if self.seasons else '')
}
)
log.debug(
'{id}: Retrieving show info from {indexer}',
{'id': self.show.series_id, 'indexer': indexerApi(self.show.indexer).name}
)
try:
# Let's make sure we refresh the indexer_api object attached to the show object.
self.show.create_indexer()
self.show.load_from_indexer()
except IndexerError as error:
log.warning(
'{id}: Unable to contact {indexer}. Aborting: {error_msg}',
{'id': self.show.series_id, 'indexer': indexerApi(self.show.indexer).name,
'error_msg': error}
)
return
except IndexerAttributeNotFound as error:
log.warning(
'{id}: Data retrieved from {indexer} was incomplete.'
' Aborting: {error_msg}',
{'id': self.show.series_id, 'indexer': indexerApi(self.show.indexer).name,