show_queue.py 23.1 KB
Newer Older
1
2
3
# Author: Nic Wolfe <[email protected]>
# URL: http://code.google.com/p/sickbeard/
#
echel0n's avatar
echel0n committed
4
# This file is part of SickRage.
5
#
echel0n's avatar
echel0n committed
6
# SickRage is free software: you can redistribute it and/or modify
7
8
9
10
# 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.
#
echel0n's avatar
echel0n committed
11
# SickRage is distributed in the hope that it will be useful,
12
13
14
15
16
# 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
echel0n's avatar
echel0n committed
17
# along with SickRage.  If not, see <http://www.gnu.org/licenses/>.
18
19
20
21
22
23
24
25
26
27

from __future__ import with_statement

import traceback

import sickbeard

from lib.imdb import _exceptions as imdb_exceptions
from sickbeard.common import SKIPPED, WANTED
from sickbeard.tv import TVShow
Giovanni's avatar
Giovanni committed
28
from sickbeard import exceptions, logger, ui, db, notifiers
29
30
31
from sickbeard import generic_queue
from sickbeard import name_cache
from sickbeard.exceptions import ex
Thraxis's avatar
Thraxis committed
32
from sickbeard.blackandwhitelist import BlackAndWhiteList, short_group_names
33

34

35
36
37
38
39
40
class ShowQueue(generic_queue.GenericQueue):
    def __init__(self):
        generic_queue.GenericQueue.__init__(self)
        self.queue_name = "SHOWQUEUE"

    def _isInQueue(self, show, actions):
echel0n's avatar
echel0n committed
41
        return show in [x.show for x in self.queue if x.action_id in actions]
42
43

    def _isBeingSomethinged(self, show, actions):
44
        return self.currentItem != None and show == self.currentItem.show and \
echel0n's avatar
echel0n committed
45
               self.currentItem.action_id in actions
46
47
48
49
50
51
52
53
54

    def isInUpdateQueue(self, show):
        return self._isInQueue(show, (ShowQueueActions.UPDATE, ShowQueueActions.FORCEUPDATE))

    def isInRefreshQueue(self, show):
        return self._isInQueue(show, (ShowQueueActions.REFRESH,))

    def isInRenameQueue(self, show):
        return self._isInQueue(show, (ShowQueueActions.RENAME,))
echel0n's avatar
echel0n committed
55

56
57
58
59
60
61
62
63
64
65
66
67
68
69
    def isInSubtitleQueue(self, show):
        return self._isInQueue(show, (ShowQueueActions.SUBTITLE,))

    def isBeingAdded(self, show):
        return self._isBeingSomethinged(show, (ShowQueueActions.ADD,))

    def isBeingUpdated(self, show):
        return self._isBeingSomethinged(show, (ShowQueueActions.UPDATE, ShowQueueActions.FORCEUPDATE))

    def isBeingRefreshed(self, show):
        return self._isBeingSomethinged(show, (ShowQueueActions.REFRESH,))

    def isBeingRenamed(self, show):
        return self._isBeingSomethinged(show, (ShowQueueActions.RENAME,))
echel0n's avatar
echel0n committed
70

71
72
73
74
    def isBeingSubtitled(self, show):
        return self._isBeingSomethinged(show, (ShowQueueActions.SUBTITLE,))

    def _getLoadingShowList(self):
echel0n's avatar
echel0n committed
75
        return [x for x in self.queue + [self.currentItem] if x != None and x.isLoading]
76
77
78
79
80
81

    loadingShowList = property(_getLoadingShowList)

    def updateShow(self, show, force=False):

        if self.isBeingAdded(show):
82
            raise exceptions.CantUpdateException(
Alexandre Beloin's avatar
Alexandre Beloin committed
83
                str(show.name) + u" is still being added, wait until it is finished before you update.")
84
85

        if self.isBeingUpdated(show):
86
            raise exceptions.CantUpdateException(
Alexandre Beloin's avatar
Alexandre Beloin committed
87
                str(show.name) + u" is already being updated by Post-processor or manually started, can't update again until it's done.")
88
89

        if self.isInUpdateQueue(show):
90
            raise exceptions.CantUpdateException(
Alexandre Beloin's avatar
Alexandre Beloin committed
91
                str(show.name) + u" is in process of being updated by Post-processor or manually started, can't update again until it's done.")
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

        if not force:
            queueItemObj = QueueItemUpdate(show)
        else:
            queueItemObj = QueueItemForceUpdate(show)

        self.add_item(queueItemObj)

        return queueItemObj

    def refreshShow(self, show, force=False):

        if self.isBeingRefreshed(show) and not force:
            raise exceptions.CantRefreshException("This show is already being refreshed, not refreshing again.")

        if (self.isBeingUpdated(show) or self.isInUpdateQueue(show)) and not force:
echel0n's avatar
echel0n committed
108
            logger.log(
Alexandre Beloin's avatar
Alexandre Beloin committed
109
                u"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.",
echel0n's avatar
echel0n committed
110
                logger.DEBUG)
111
112
            return

113
        queueItemObj = QueueItemRefresh(show, force=force)
echel0n's avatar
echel0n committed
114

115
116
117
118
119
120
121
122
123
124
125
        self.add_item(queueItemObj)

        return queueItemObj

    def renameShowEpisodes(self, show, force=False):

        queueItemObj = QueueItemRename(show)

        self.add_item(queueItemObj)

        return queueItemObj
echel0n's avatar
echel0n committed
126

127
128
129
130
131
132
133
134
    def downloadSubtitles(self, show, force=False):

        queueItemObj = QueueItemSubtitle(show)

        self.add_item(queueItemObj)

        return queueItemObj

echel0n's avatar
echel0n committed
135
    def addShow(self, indexer, indexer_id, showDir, default_status=None, quality=None, flatten_folders=None,
136
                lang=None, subtitles=None, anime=None, scene=None, paused=None, blacklist=None, whitelist=None):
137
138
139
140

        if lang is None:
            lang = sickbeard.INDEXER_DEFAULT_LANGUAGE

echel0n's avatar
echel0n committed
141
        queueItemObj = QueueItemAdd(indexer, indexer_id, showDir, default_status, quality, flatten_folders, lang,
142
                                    subtitles, anime, scene, paused, blacklist, whitelist)
echel0n's avatar
echel0n committed
143

144
145
146
147
        self.add_item(queueItemObj)

        return queueItemObj

echel0n's avatar
echel0n committed
148

149
class ShowQueueActions:
echel0n's avatar
echel0n committed
150
151
152
153
154
155
156
    REFRESH = 1
    ADD = 2
    UPDATE = 3
    FORCEUPDATE = 4
    RENAME = 5
    SUBTITLE = 6

157
    names = {REFRESH: 'Refresh',
echel0n's avatar
echel0n committed
158
159
160
161
162
163
164
             ADD: 'Add',
             UPDATE: 'Update',
             FORCEUPDATE: 'Force Update',
             RENAME: 'Rename',
             SUBTITLE: 'Subtitle',
    }

165
166
167
168
169
170
171
172
173
174
175
176

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
    """
echel0n's avatar
echel0n committed
177

178
179
180
    def __init__(self, action_id, show):
        generic_queue.QueueItem.__init__(self, ShowQueueActions.names[action_id], action_id)
        self.show = show
echel0n's avatar
echel0n committed
181

182
    def isInQueue(self):
183
184
        return self in sickbeard.showQueueScheduler.action.queue + [
            sickbeard.showQueueScheduler.action.currentItem]  #@UndefinedVariable
185
186
187
188
189
190
191
192
193
194
195
196
197

    def _getName(self):
        return str(self.show.indexerid)

    def _isLoading(self):
        return False

    show_name = property(_getName)

    isLoading = property(_isLoading)


class QueueItemAdd(ShowQueueItem):
198
    def __init__(self, indexer, indexer_id, showDir, default_status, quality, flatten_folders, lang, subtitles, anime,
199
                 scene, paused, blacklist, whitelist):
200
201
202
203
204
205
206
207
208

        self.indexer = indexer
        self.indexer_id = indexer_id
        self.showDir = showDir
        self.default_status = default_status
        self.quality = quality
        self.flatten_folders = flatten_folders
        self.lang = lang
        self.subtitles = subtitles
209
        self.anime = anime
210
        self.scene = scene
211
        self.paused = paused
212
213
        self.blacklist = blacklist
        self.whitelist = whitelist
214

Alexandre Beloin's avatar
Alexandre Beloin committed
215
        if sickbeard.TRAKT_USE_ROLLING_DOWNLOAD and sickbeard.USE_TRAKT:
Giovanni's avatar
Giovanni committed
216
217
            self.paused = sickbeard.TRAKT_ROLLING_ADD_PAUSED

Alexandre Beloin's avatar
Alexandre Beloin committed
218
219
220
        # Process add show in priority
        self.priority = generic_queue.QueuePriorities.HIGH

221
222
223
224
        self.show = None

        # this will initialize self.show to None
        ShowQueueItem.__init__(self, ShowQueueActions.ADD, self.show)
echel0n's avatar
echel0n committed
225

226
227
228
229
230
    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.
        """
231
        if self.show == None:
232
233
234
235
236
237
238
239
240
241
            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.
        """
242
        if self.show == None:
243
244
245
246
247
            return True
        return False

    isLoading = property(_isLoading)

248
    def run(self):
249

250
        ShowQueueItem.run(self)
251
252

        logger.log(u"Starting to add show " + self.showDir)
echel0n's avatar
echel0n committed
253
        # make sure the Indexer IDs are valid
254
        try:
echel0n's avatar
echel0n committed
255

256
            lINDEXER_API_PARMS = sickbeard.indexerApi(self.indexer).api_params.copy()
echel0n's avatar
echel0n committed
257
258
259
            if self.lang:
                lINDEXER_API_PARMS['language'] = self.lang

echel0n's avatar
echel0n committed
260
            logger.log(u"" + str(sickbeard.indexerApi(self.indexer).name) + ": " + repr(lINDEXER_API_PARMS))
echel0n's avatar
echel0n committed
261

262
            t = sickbeard.indexerApi(self.indexer).indexer(**lINDEXER_API_PARMS)
echel0n's avatar
echel0n committed
263
264
            s = t[self.indexer_id]

echel0n's avatar
echel0n committed
265
            # 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
echel0n's avatar
echel0n committed
266
            if getattr(s, 'seriesname', None) is None:
267
268
269
                logger.log(u"Show in " + self.showDir + " has no name on " + str(
                    sickbeard.indexerApi(self.indexer).name) + ", probably the wrong language used to search with.",
                           logger.ERROR)
echel0n's avatar
echel0n committed
270
                ui.notifications.error("Unable to add show",
echel0n's avatar
echel0n committed
271
272
                                       "Show in " + self.showDir + " has no name on " + str(sickbeard.indexerApi(
                                           self.indexer).name) + ", probably the wrong language. Delete .nfo and add manually in the correct language.")
273
274
                self._finishEarly()
                return
echel0n's avatar
echel0n committed
275
276
            # if the show has no episodes/seasons
            if not s:
277
278
                logger.log(u"Show " + str(s['seriesname']) + " is on " + str(
                    sickbeard.indexerApi(self.indexer).name) + " but contains no season/episode data.", logger.ERROR)
echel0n's avatar
echel0n committed
279
                ui.notifications.error("Unable to add show",
echel0n's avatar
echel0n committed
280
281
                                       "Show " + str(s['seriesname']) + " is on " + str(sickbeard.indexerApi(
                                           self.indexer).name) + " but contains no season/episode data.")
echel0n's avatar
echel0n committed
282
283
284
                self._finishEarly()
                return
        except Exception, e:
285
286
            logger.log(u"Unable to find show ID:" + str(self.indexer_id) + " on Indexer: " + str(
                sickbeard.indexerApi(self.indexer).name), logger.ERROR)
echel0n's avatar
echel0n committed
287
            ui.notifications.error("Unable to add show",
echel0n's avatar
echel0n committed
288
289
                                   "Unable to look up the show in " + self.showDir + " on " + str(sickbeard.indexerApi(
                                       self.indexer).name) + " using ID " + str(
echel0n's avatar
echel0n committed
290
                                       self.indexer_id) + ", not using the NFO. Delete .nfo and try adding manually again.")
echel0n's avatar
echel0n committed
291
292
            self._finishEarly()
            return
293

echel0n's avatar
echel0n committed
294
        try:
295
296
297
298
299
300
301
            newShow = TVShow(self.indexer, self.indexer_id, self.lang)
            newShow.loadFromIndexer()

            self.show = newShow

            # set up initial values
            self.show.location = self.showDir
302
            self.show.subtitles = self.subtitles if self.subtitles != None else sickbeard.SUBTITLES_DEFAULT
303
            self.show.quality = self.quality if self.quality else sickbeard.QUALITY_DEFAULT
304
            self.show.flatten_folders = self.flatten_folders if self.flatten_folders != None else sickbeard.FLATTEN_FOLDERS_DEFAULT
305
            self.show.anime = self.anime if self.anime != None else sickbeard.ANIME_DEFAULT
306
            self.show.scene = self.scene if self.scene != None else sickbeard.SCENE_DEFAULT
307
            self.show.paused = self.paused if self.paused != None else False
echel0n's avatar
echel0n committed
308

309
310
            # set up default new/missing episode status
            logger.log(u"Setting all episodes to the specified default status: " + str(self.show.default_ep_status))
311
            self.show.default_ep_status = self.default_status
312

313
314
315
316
317
318
319
            if self.show.anime:
                self.show.release_groups = BlackAndWhiteList(self.show.indexerid)
                if self.blacklist:
                    self.show.release_groups.set_black_keywords(self.blacklist)
                if self.whitelist:
                    self.show.release_groups.set_white_keywords(self.whitelist)
                    
320
            # be smartish about this
321
            if self.show.genre and "talk show" in self.show.genre.lower():
322
                self.show.air_by_date = 1
323
324
325
            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():
326
                self.show.sports = 1
327

echel0n's avatar
echel0n committed
328
329
330
331
        except sickbeard.indexer_exception, e:
            logger.log(
                u"Unable to add show due to an error with " + sickbeard.indexerApi(self.indexer).name + ": " + ex(e),
                logger.ERROR)
332
            if self.show:
echel0n's avatar
echel0n committed
333
334
335
                ui.notifications.error(
                    "Unable to add " + str(self.show.name) + " due to an error with " + sickbeard.indexerApi(
                        self.indexer).name + "")
336
            else:
echel0n's avatar
echel0n committed
337
338
                ui.notifications.error(
                    "Unable to add show due to an error with " + sickbeard.indexerApi(self.indexer).name + "")
339
340
341
342
            self._finishEarly()
            return

        except exceptions.MultipleShowObjectsException:
343
            logger.log(u"The show in " + self.showDir + " is already in your show list, skipping", logger.WARNING)
344
345
346
347
348
            ui.notifications.error('Show skipped', "The show in " + self.showDir + " is already in your show list")
            self._finishEarly()
            return

        except Exception, e:
echel0n's avatar
echel0n committed
349
            logger.log(u"Error trying to add show: " + ex(e), logger.ERROR)
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
            logger.log(traceback.format_exc(), logger.DEBUG)
            self._finishEarly()
            raise

        logger.log(u"Retrieving show info from IMDb", logger.DEBUG)
        try:
            self.show.loadIMDbInfo()
        except imdb_exceptions.IMDbError, e:
            logger.log(u" Something wrong on IMDb api: " + ex(e), logger.WARNING)
        except Exception, e:
            logger.log(u"Error loading IMDb info: " + ex(e), logger.ERROR)

        try:
            self.show.saveToDB()
        except Exception, e:
            logger.log(u"Error saving the show to the database: " + ex(e), logger.ERROR)
            logger.log(traceback.format_exc(), logger.DEBUG)
            self._finishEarly()
            raise
echel0n's avatar
echel0n committed
369

370
        # add it to the show list
echel0n's avatar
echel0n committed
371
        sickbeard.showList.append(self.show)
372
373
374
375

        try:
            self.show.loadEpisodesFromIndexer()
        except Exception, e:
echel0n's avatar
echel0n committed
376
377
378
            logger.log(
                u"Error with " + sickbeard.indexerApi(self.show.indexer).name + ", not creating episode list: " + ex(e),
                logger.ERROR)
379
380
            logger.log(traceback.format_exc(), logger.DEBUG)

381
382
383
        # update internal name cache
        name_cache.buildNameCache()

384
385
386
387
388
389
        try:
            self.show.loadEpisodesFromDir()
        except Exception, e:
            logger.log(u"Error searching dir for episodes: " + ex(e), logger.ERROR)
            logger.log(traceback.format_exc(), logger.DEBUG)

Giovanni's avatar
Giovanni committed
390
391
        sickbeard.traktRollingScheduler.action.updateWantedList(self.show.indexerid)

392
393
        # if they set default ep status to WANTED then run the backlog to search for episodes
        if self.show.default_ep_status == WANTED:
394
            logger.log(u"Launching backlog for this show since its episodes are WANTED")
395
            sickbeard.backlogSearchScheduler.action.searchBacklog([self.show])
396

397
398
        self.show.writeMetadata()
        self.show.updateMetadata()
399
        self.show.populateCache()
400
401
402

        self.show.flushEpisodes()

echel0n's avatar
echel0n committed
403
404
405
406
407
        if sickbeard.USE_TRAKT:
            # if there are specific episodes that need to be added by trakt
            sickbeard.traktCheckerScheduler.action.manageNewShow(self.show)

            # add show to trakt.tv library
408
409
            if sickbeard.TRAKT_SYNC:
                sickbeard.traktCheckerScheduler.action.addShowToTraktLibrary(self.show)
410

Giovanni's avatar
Giovanni committed
411
412
413
            if sickbeard.TRAKT_SYNC_WATCHLIST:
                logger.log(u"update watchlist")
                notifiers.trakt_notifier.update_watchlist(self.show)
Giovanni's avatar
Giovanni committed
414

415
416
417
        # Load XEM data to DB for show
        sickbeard.scene_numbering.xem_refresh(self.show.indexerid, self.show.indexer, force=True)

418
        # check if show has XEM mapping so we can determin if searches should go by scene numbering or indexer numbering.
419
420
        if not self.scene and sickbeard.scene_numbering.get_xem_numbering_for_show(self.show.indexerid,
                                                                                   self.show.indexer):
421
422
            self.show.scene = 1

423
424
425
        self.finish()

    def _finishEarly(self):
426
        if self.show != None:
427
428
429
430
431
432
            self.show.deleteShow()

        self.finish()


class QueueItemRefresh(ShowQueueItem):
433
    def __init__(self, show=None, force=False):
434
435
436
437
438
        ShowQueueItem.__init__(self, ShowQueueActions.REFRESH, show)

        # do refreshes first because they're quick
        self.priority = generic_queue.QueuePriorities.HIGH

439
440
441
        # force refresh certain items
        self.force = force

442
443
    def run(self):
        ShowQueueItem.run(self)
444
445
446
447

        logger.log(u"Performing refresh on " + self.show.name)

        self.show.refreshDir()
448
        self.show.writeMetadata()
449
450
        #if self.force:
        #    self.show.updateMetadata()
451
452
        self.show.populateCache()

453
        # Load XEM data to DB for show
echel0n's avatar
echel0n committed
454
        sickbeard.scene_numbering.xem_refresh(self.show.indexerid, self.show.indexer)
455

456
457
458
459
460
461
        self.inProgress = False

class QueueItemRename(ShowQueueItem):
    def __init__(self, show=None):
        ShowQueueItem.__init__(self, ShowQueueActions.RENAME, show)

462
    def run(self):
463

464
        ShowQueueItem.run(self)
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

        logger.log(u"Performing rename on " + self.show.name)

        try:
            show_loc = self.show.location
        except exceptions.ShowDirNotFoundException:
            logger.log(u"Can't perform rename on " + self.show.name + " when the show dir is missing.", logger.WARNING)
            return

        ep_obj_rename_list = []

        ep_obj_list = self.show.getAllEpisodes(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.relatedEps:
                    # do we have one of multi-episodes in the rename list already
                    have_already = False
                    for cur_related_ep in cur_ep_obj.relatedEps + [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.inProgress = False

echel0n's avatar
echel0n committed
498

499
500
501
502
class QueueItemSubtitle(ShowQueueItem):
    def __init__(self, show=None):
        ShowQueueItem.__init__(self, ShowQueueActions.SUBTITLE, show)

503
504
    def run(self):
        ShowQueueItem.run(self)
505

echel0n's avatar
echel0n committed
506
        logger.log(u"Downloading subtitles for " + self.show.name)
507
508
509
510
511
512
513
514
515
516
517

        self.show.downloadSubtitles()

        self.inProgress = False


class QueueItemUpdate(ShowQueueItem):
    def __init__(self, show=None):
        ShowQueueItem.__init__(self, ShowQueueActions.UPDATE, show)
        self.force = False

518
    def run(self):
519

520
        ShowQueueItem.run(self)
521

522
        logger.log(u"Beginning update of " + self.show.name, logger.DEBUG)
523

echel0n's avatar
echel0n committed
524
        logger.log(u"Retrieving show info from " + sickbeard.indexerApi(self.show.indexer).name + "", logger.DEBUG)
525
        try:
echel0n's avatar
echel0n committed
526
527
528
529
            self.show.loadFromIndexer(cache=not self.force)
        except sickbeard.indexer_error, e:
            logger.log(u"Unable to contact " + sickbeard.indexerApi(self.show.indexer).name + ", aborting: " + ex(e),
                       logger.WARNING)
530
            return
echel0n's avatar
echel0n committed
531
532
533
        except sickbeard.indexer_attributenotfound, e:
            logger.log(u"Data retrieved from " + sickbeard.indexerApi(
                self.show.indexer).name + " was incomplete, aborting: " + ex(e), logger.ERROR)
534
535
536
537
538
539
540
541
542
543
            return

        logger.log(u"Retrieving show info from IMDb", logger.DEBUG)
        try:
            self.show.loadIMDbInfo()
        except imdb_exceptions.IMDbError, e:
            logger.log(u" Something wrong on IMDb api: " + ex(e), logger.WARNING)
        except Exception, e:
            logger.log(u"Error loading IMDb info: " + ex(e), logger.ERROR)
            logger.log(traceback.format_exc(), logger.DEBUG)
echel0n's avatar
echel0n committed
544

545
546
547
        try:
            self.show.saveToDB()
        except Exception, e:
echel0n's avatar
echel0n committed
548
            logger.log(u"Error saving show info to the database: " + ex(e), logger.ERROR)
549
            logger.log(traceback.format_exc(), logger.DEBUG)
echel0n's avatar
echel0n committed
550

551
552
553
554
555
        # get episode list from DB
        logger.log(u"Loading all episodes from the database", logger.DEBUG)
        DBEpList = self.show.loadEpisodesFromDB()

        # get episode list from TVDB
echel0n's avatar
echel0n committed
556
        logger.log(u"Loading all episodes from " + sickbeard.indexerApi(self.show.indexer).name + "", logger.DEBUG)
557
558
        try:
            IndexerEpList = self.show.loadEpisodesFromIndexer(cache=not self.force)
echel0n's avatar
echel0n committed
559
560
561
        except sickbeard.indexer_exception, e:
            logger.log(u"Unable to get info from " + sickbeard.indexerApi(
                self.show.indexer).name + ", the show info will not be refreshed: " + ex(e), logger.ERROR)
562
563
            IndexerEpList = None

564
        foundMissingEps = False
echel0n's avatar
echel0n committed
565
        if IndexerEpList is None:
echel0n's avatar
echel0n committed
566
567
            logger.log(u"No data returned from " + sickbeard.indexerApi(
                self.show.indexer).name + ", unable to update this show", logger.ERROR)
568
        else:
569
            # for each ep we found on the Indexer delete it from the DB list
570
571
            for curSeason in IndexerEpList:
                for curEpisode in IndexerEpList[curSeason]:
echel0n's avatar
echel0n committed
572
573
                    logger.log(u"Removing " + str(curSeason) + "x" + str(curEpisode) + " from the DB list",
                               logger.DEBUG)
574
575
                    if curSeason in DBEpList and curEpisode in DBEpList[curSeason]:
                        del DBEpList[curSeason][curEpisode]
576
577
578
                    else:
                        # found missing episodes
                        foundMissingEps = True
579
580
581
582

            # for the remaining episodes in the DB list just delete them from the DB
            for curSeason in DBEpList:
                for curEpisode in DBEpList[curSeason]:
echel0n's avatar
echel0n committed
583
                    logger.log(u"Permanently deleting episode " + str(curSeason) + "x" + str(
584
                        curEpisode) + " from the database", logger.INFO)
585
586
587
588
589
590
                    curEp = self.show.getEpisode(curSeason, curEpisode)
                    try:
                        curEp.deleteEpisode()
                    except exceptions.EpisodeDeletedException:
                        pass

591
592
593
594
        # if they set default ep status to WANTED then run the backlog
        if foundMissingEps and self.show.default_ep_status == WANTED:
            logger.log(u"Launching backlog for this show since we found missing episodes")
            sickbeard.backlogSearchScheduler.action.searchBacklog([self.show])
595

596
        sickbeard.showQueueScheduler.action.refreshShow(self.show, self.force)
597

598
599
600
601
class QueueItemForceUpdate(QueueItemUpdate):
    def __init__(self, show=None):
        ShowQueueItem.__init__(self, ShowQueueActions.FORCEUPDATE, show)
        self.force = True