Esempio n. 1
0
    def run(self):
        start_time = time.time()

        sickrage.app.log.info(
            "Started adding show {} from show dir: {}".format(
                self.show_name, self.showDir))

        index_name = IndexerApi(self.indexer).name

        # make sure the Indexer IDs are valid
        lINDEXER_API_PARMS = IndexerApi(self.indexer).api_params.copy()
        lINDEXER_API_PARMS['cache'] = False
        lINDEXER_API_PARMS[
            'language'] = self.lang or sickrage.app.config.indexer_default_language

        sickrage.app.log.info("{}: {}".format(index_name,
                                              repr(lINDEXER_API_PARMS)))

        t = IndexerApi(self.indexer).indexer(**lINDEXER_API_PARMS)

        s = t[self.indexer_id]
        if not s:
            sickrage.app.alerts.error(
                _("Unable to add show"),
                _("Unable to look up the show in {} on {} using ID {}, not using the NFO. Delete .nfo and try adding "
                  "manually again.").format(self.showDir, index_name,
                                            self.indexer_id))

            if sickrage.app.config.use_trakt:
                title = self.showDir.split("/")[-1]

                data = {
                    'shows': [{
                        'title': title,
                        'ids': {
                            IndexerApi(self.indexer).trakt_id: self.indexer_id
                        }
                    }]
                }

                TraktAPI()["sync/watchlist"].remove(data)

            return self._finish_early()

        # 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
        try:
            s.seriesname
        except AttributeError:
            sickrage.app.log.warning(
                "Show in {} has no name on {}, probably the wrong language used to search with"
                .format(self.showDir, index_name))
            sickrage.app.alerts.error(
                _("Unable to add show"),
                _("Show in {} has no name on {}, probably the wrong language. Delete .nfo "
                  "and add manually in the correct language").format(
                      self.showDir, index_name))
            return self._finish_early()

        # if the show has no episodes/seasons
        if not len(s):
            sickrage.app.log.warning("Show " + str(s['seriesname']) +
                                     " is on " +
                                     str(IndexerApi(self.indexer).name) +
                                     "but contains no season/episode "
                                     "data.")
            sickrage.app.alerts.error(
                _("Unable to add show"),
                _("Show ") + str(s['seriesname']) + _(" is on ") +
                str(IndexerApi(self.indexer).name) +
                _(" but contains no season/episode data."))
            return self._finish_early()

        try:
            # add show to database
            show_obj = TVShow(self.indexer_id,
                              self.indexer,
                              lang=self.lang,
                              location=self.showDir)

            # set up initial values
            show_obj.subtitles = self.subtitles if self.subtitles is not None else sickrage.app.config.subtitles_default
            show_obj.sub_use_sr_metadata = self.sub_use_sr_metadata if self.sub_use_sr_metadata is not None else False
            show_obj.quality = self.quality if self.quality is not None else sickrage.app.config.quality_default
            show_obj.flatten_folders = self.flatten_folders if self.flatten_folders is not None else sickrage.app.config.flatten_folders_default
            show_obj.scene = self.scene if self.scene is not None else sickrage.app.config.scene_default
            show_obj.anime = self.anime if self.anime is not None else sickrage.app.config.anime_default
            show_obj.dvdorder = self.dvdorder if self.dvdorder is not None else False
            show_obj.search_format = self.search_format if self.search_format is not None else sickrage.app.config.search_format_default
            show_obj.skip_downloaded = self.skip_downloaded if self.skip_downloaded is not None else sickrage.app.config.skip_downloaded_default
            show_obj.paused = self.paused if self.paused is not None else False

            # set up default new/missing episode status
            sickrage.app.log.info(
                "Setting all current episodes to the specified default status: "
                + str(self.default_status))
            show_obj.default_ep_status = self.default_status

            # save to database
            show_obj.save()

            if show_obj.anime:
                if self.blacklist:
                    show_obj.release_groups.set_black_keywords(self.blacklist)
                if self.whitelist:
                    show_obj.release_groups.set_white_keywords(self.whitelist)
        except indexer_exception as e:
            sickrage.app.log.warning(
                _("Unable to add show due to an error with ") +
                IndexerApi(self.indexer).name + ": {}".format(e))
            sickrage.app.alerts.error(
                _("Unable to add show due to an error with ") +
                IndexerApi(self.indexer).name + "")
            return self._finish_early()
        except MultipleShowObjectsException:
            sickrage.app.log.warning(
                _("The show in ") + self.showDir +
                _(" is already in your show list, skipping"))
            sickrage.app.alerts.error(
                _('Show skipped'),
                _("The show in ") + self.showDir +
                _(" is already in your show list"))
            return self._finish_early()
        except Exception as e:
            sickrage.app.log.error(_("Error trying to add show: {}").format(e))
            sickrage.app.log.debug(traceback.format_exc())
            raise self._finish_early()

        try:
            sickrage.app.log.debug(
                _("Attempting to retrieve show info from IMDb"))
            show_obj.load_imdb_info()
        except Exception as e:
            sickrage.app.log.debug(_("Error loading IMDb info: {}").format(e))
            sickrage.app.log.debug(traceback.format_exc())

        try:
            show_obj.load_episodes_from_indexer()
        except Exception as e:
            sickrage.app.log.debug(
                _("Error with ") + IndexerApi(show_obj.indexer).name +
                _(", not creating episode list: {}").format(e))
            sickrage.app.log.debug(traceback.format_exc())

        try:
            show_obj.load_episodes_from_dir()
        except Exception as e:
            sickrage.app.log.debug(
                "Error searching dir for episodes: {}".format(e))
            sickrage.app.log.debug(traceback.format_exc())

        show_obj.write_metadata(force=True)
        show_obj.populate_cache()

        if sickrage.app.config.use_trakt:
            # if there are specific episodes that need to be added by trakt
            sickrage.app.trakt_searcher.manage_new_show(show_obj)

            # add show to trakt.tv library
            if sickrage.app.config.trakt_sync:
                sickrage.app.trakt_searcher.add_show_to_trakt_library(show_obj)

            if sickrage.app.config.trakt_sync_watchlist:
                sickrage.app.log.info("update watchlist")
                sickrage.app.notifier_providers['trakt'].update_watchlist(
                    show_obj)

        # Retrieve scene exceptions
        show_obj.retrieve_scene_exceptions()

        # Load XEM data to DB for show
        xem_refresh(show_obj.indexer_id, show_obj.indexer, 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 get_xem_numbering_for_show(show_obj.indexer_id, show_obj.indexer):
        #     show_obj.scene = 1

        # if they set default ep status to WANTED then run the backlog to search for episodes
        if show_obj.default_ep_status == WANTED:
            sickrage.app.log.info(
                _("Launching backlog for this show since it has episodes that are WANTED"
                  ))
            sickrage.app.backlog_searcher.search_backlog(show_obj.indexer_id)

        show_obj.default_ep_status = self.default_status_after

        show_obj.save()

        sickrage.app.log.info(
            "Finished adding show {} in {}s from show dir: {}".format(
                self.show_name, round(time.time() - start_time, 2),
                self.showDir))
Esempio n. 2
0
    def run(self):
        super(ShowTaskAdd, self).run()

        start_time = time.time()

        sickrage.app.log.info(
            "Started adding show {} from show dir: {}".format(
                self.show_name, self.show_dir))

        series_provider_language = self.lang or sickrage.app.config.general.series_provider_default_language
        series_info = sickrage.app.series_providers[
            self.series_provider_id].get_series_info(
                self.series_id,
                language=series_provider_language,
                enable_cache=False)
        if not series_info:
            sickrage.app.alerts.error(
                _("Unable to add show"),
                _("Unable to look up the show in {} on {} using ID {}, not using the NFO. Delete .nfo and try adding "
                  "manually again.").format(
                      self.show_dir, sickrage.app.series_providers[
                          self.series_provider_id].name, self.series_id))

            if sickrage.app.config.trakt.enable:
                title = self.show_dir.split("/")[-1]

                data = {
                    'shows': [{
                        'title': title,
                        'ids': {
                            sickrage.app.series_providers[self.series_provider_id].trakt_id:
                            self.series_id
                        }
                    }]
                }

                TraktAPI()["sync/watchlist"].remove(data)

            return self._finish_early()

        # this usually only happens if they have an NFO in their show dir which gave us a series id that has no
        # proper english version of the show
        try:
            series_info.name
        except AttributeError:
            sickrage.app.log.warning(
                f"Show in {self.show_dir} has no name on {sickrage.app.series_providers[self.series_provider_id].name}, "
                f"probably the wrong language used to search with")

            sickrage.app.alerts.error(
                _("Unable to add show"),
                _(f"Show in {self.show_dir} has no name on {sickrage.app.series_providers[self.series_provider_id].name}, "
                  f"probably the wrong language. Delete .nfo and add manually in the correct language"
                  ))

            return self._finish_early()

        # if the show has no episodes/seasons
        if not len(series_info):
            sickrage.app.log.warning("Show " + str(series_info['name']) +
                                     " is on " +
                                     str(sickrage.app.series_providers[
                                         self.series_provider_id].name) +
                                     "but contains no season/episode data.")

            sickrage.app.alerts.error(
                _("Unable to add show"),
                _("Show ") + str(series_info['name']) + _(" is on ") +
                str(sickrage.app.series_providers[
                    self.series_provider_id].name) +
                _(" but contains no season/episode data."))

            return self._finish_early()

        try:
            # add show to database
            show_obj = TVShow(self.series_id,
                              self.series_provider_id,
                              lang=self.lang,
                              location=self.show_dir)

            # set up initial values
            show_obj.subtitles = self.subtitles if self.subtitles is not None else sickrage.app.config.subtitles.default
            show_obj.sub_use_sr_metadata = self.sub_use_sr_metadata if self.sub_use_sr_metadata is not None else False
            show_obj.quality = self.quality if self.quality is not None else sickrage.app.config.general.quality_default
            show_obj.flatten_folders = self.flatten_folders if self.flatten_folders is not None else sickrage.app.config.general.flatten_folders_default
            show_obj.scene = self.scene if self.scene is not None else sickrage.app.config.general.scene_default
            show_obj.anime = self.anime if self.anime is not None else sickrage.app.config.general.anime_default
            show_obj.dvd_order = self.dvd_order if self.dvd_order is not None else False
            show_obj.search_format = self.search_format if self.search_format is not None else sickrage.app.config.general.search_format_default
            show_obj.skip_downloaded = self.skip_downloaded if self.skip_downloaded is not None else sickrage.app.config.general.skip_downloaded_default
            show_obj.paused = self.paused if self.paused is not None else False

            # set up default new/missing episode status
            sickrage.app.log.info(
                "Setting all current episodes to the specified default status: "
                + str(self.default_status))
            show_obj.default_ep_status = self.default_status

            # save to database
            show_obj.save()

            if show_obj.anime:
                if self.blacklist:
                    show_obj.release_groups.set_black_keywords(self.blacklist)
                if self.whitelist:
                    show_obj.release_groups.set_white_keywords(self.whitelist)
        except SeriesProviderException as e:
            sickrage.app.log.warning(
                _("Unable to add show due to an error with ") +
                sickrage.app.series_providers[self.series_provider_id].name +
                ": {}".format(e))
            sickrage.app.alerts.error(
                _("Unable to add show due to an error with ") +
                sickrage.app.series_providers[self.series_provider_id].name +
                "")
            return self._finish_early()
        except MultipleShowObjectsException:
            sickrage.app.log.warning(
                _("The show in ") + self.show_dir +
                _(" is already in your show list, skipping"))
            sickrage.app.alerts.error(
                _('Show skipped'),
                _("The show in ") + self.show_dir +
                _(" is already in your show list"))
            return self._finish_early()
        except Exception as e:
            sickrage.app.log.error(_("Error trying to add show: {}").format(e))
            sickrage.app.log.debug(traceback.format_exc())
            raise self._finish_early()

        try:
            sickrage.app.log.debug(
                _("Attempting to retrieve show info from IMDb"))
            show_obj.load_imdb_info()
        except Exception as e:
            sickrage.app.log.debug(_("Error loading IMDb info: {}").format(e))
            sickrage.app.log.debug(traceback.format_exc())

        try:
            show_obj.load_episodes_from_series_provider()
        except Exception as e:
            sickrage.app.log.debug(
                _("Error with ") + show_obj.series_provider.name +
                _(", not creating episode list: {}").format(e))
            sickrage.app.log.debug(traceback.format_exc())

        try:
            show_obj.load_episodes_from_dir()
        except Exception as e:
            sickrage.app.log.debug(
                "Error searching dir for episodes: {}".format(e))
            sickrage.app.log.debug(traceback.format_exc())

        show_obj.write_metadata(force=True)
        show_obj.populate_cache()

        if sickrage.app.config.trakt.enable:
            # if there are specific episodes that need to be added by trakt
            sickrage.app.trakt_searcher.manage_new_show(show_obj)

            # add show to trakt.tv library
            if sickrage.app.config.trakt.sync:
                sickrage.app.trakt_searcher.add_show_to_trakt_library(show_obj)

            if sickrage.app.config.trakt.sync_watchlist:
                sickrage.app.log.info("update watchlist")
                sickrage.app.notification_providers['trakt'].update_watchlist(
                    show_obj)

        # Retrieve scene exceptions
        show_obj.retrieve_scene_exceptions()

        # Load XEM data to DB for show
        xem_refresh(show_obj.series_id,
                    show_obj.series_provider_id,
                    force=True)

        # check if show has XEM mapping so we can determine if searches should go by scene numbering or series_provider_id
        # numbering.
        # if not self.scene and get_xem_numbering_for_show(show_obj.series_id, show_obj.series_provider_id):
        #     show_obj.scene = 1

        # if they set default ep status to WANTED then run the backlog to search for episodes
        if show_obj.default_ep_status == EpisodeStatus.WANTED:
            sickrage.app.log.info(
                _("Launching backlog for this show since it has episodes that are WANTED"
                  ))
            sickrage.app.backlog_searcher.search_backlog(
                show_obj.series_id, show_obj.series_provider_id)

        show_obj.default_ep_status = self.default_status_after

        show_obj.save()

        WebSocketMessage('SHOW_ADDED', {
            'seriesSlug': show_obj.slug,
            'series': show_obj.to_json(progress=True)
        }).push()

        sickrage.app.log.info(
            "Finished adding show {} in {}s from show dir: {}".format(
                self.show_name, round(time.time() - start_time, 2),
                self.show_dir))