def final_episode_check(self, season, episode): """ Checks to see if the current item is the last episode aired for the show :param season: Season number of item to check :type season: int :param episode: Episode number of item to check :type episode: int :return: True if item is last aired episode else false :rtype: bool """ get = MetadataHandler().get_trakt_info season = int(season) episode = int(episode) last_aired = self.trakt_api.get_json( "shows/{}/last_episode".format(self.show_trakt_id) ) if season > get(last_aired, "season"): return True if season == get(last_aired, "season") and episode == get(last_aired, "number"): return True return False
def get_mixed_episode_list(self, trakt_items, **params): """ Returns a list of mixed episodes from different or same show :param trakt_items: List of show & episodes object pairs :type trakt_items: list :return: List of episode objects with full meta :rtype: list """ g.log("Fetching mixed episode list from sync database", "debug") self._try_update_mixed_episodes(trakt_items) in_predicate = ",".join([ str(i["trakt_id"]) for i in trakt_items if i["trakt_id"] is not None ]) if g.get_bool_setting("general.showRemainingUnwatched"): query = """SELECT e.trakt_id, e.info, e.cast, e.art, e.args, e.watched as play_count, b.resume_time as resume_time, b.percent_played as percent_played, se.watched_episodes, se.unwatched_episodes, se.episode_count FROM episodes as e INNER JOIN seasons se on e.trakt_season_id = se.trakt_id LEFT JOIN bookmarks as b on e.Trakt_id = b.Trakt_id WHERE e.trakt_id in ({})""".format( in_predicate) else: query = """SELECT e.trakt_id, e.info, e.cast, e.art, e.args, e.watched as play_count, b.resume_time as resume_time, b.percent_played as percent_played FROM episodes as e LEFT JOIN bookmarks as b on e.Trakt_id = b.Trakt_id WHERE e.trakt_id in ({})""".format(in_predicate) if params.pop("hide_unaired", self.hide_unaired): query += " AND Datetime(e.air_date) < Datetime('now') " if params.pop("hide_specials", self.hide_specials): query += " AND e.season != 0" if params.pop("hide_watched", self.hide_watched): query += " AND e.watched = 0" return MetadataHandler.sort_list_items( self.execute_sql(query).fetchall(), trakt_items)
def get_show_list(self, trakt_list, **params): """ Takes in a list of shows from a Trakt endpoint, updates meta where required and returns the formatted list :param trakt_list: List of shows to retrieve :type trakt_list: list :return: List of updated shows with full meta :rtype: list """ g.log("Fetching show list from sync database", "debug") trakt_list = [i for i in trakt_list if i.get("trakt_id")] self.insert_trakt_shows( self.filter_trakt_items_that_needs_updating(trakt_list, "shows") ) self._update_mill_format_shows(trakt_list, False) g.log("Show list update and milling compelete", "debug") statement = """SELECT s.trakt_id, s.info, s.cast, s.art, s.args, s.watched_episodes, s.unwatched_episodes, s.episode_count, s.season_count FROM shows as s WHERE s.trakt_id in ({}) """.format( ",".join((str(i.get("trakt_id")) for i in trakt_list)) ) if params.pop("hide_unaired", self.hide_unaired): statement += " AND Datetime(s.air_date) < Datetime('now')" if params.pop("hide_watched", self.hide_watched): statement += " AND s.watched_episodes < s.episode_count" return MetadataHandler.sort_list_items( self.execute_sql(statement).fetchall(), trakt_list )
def __init__(self, ): super(TraktSyncDatabase, self).__init__(g.TRAKT_SYNC_DB_PATH, schema, migrate_db_lock) self.metadataHandler = MetadataHandler() self.trakt_api = TraktAPI() self.activities = {} self.item_list = [] self.base_date = "1970-01-01T00:00:00" self.task_queue = ThreadPool() self.mill_task_queue = ThreadPool() self.parent_task_queue = ThreadPool() self.refresh_activities() # If you make changes to the required meta in any indexer that is cached in this database # You will need to update the below version number to match the new addon version # This will ensure that the metadata required for operations is available self.last_meta_update = "2.0.0" if self.activities is None: self.clear_all_meta(False) self.set_base_activities() if self.activities is not None: self._check_database_version() self.notification_prefix = "{}: Trakt".format(g.ADDON_NAME) self.hide_unaired = g.get_bool_setting("general.hideUnAired") self.hide_specials = g.get_bool_setting("general.hideSpecials") self.hide_watched = g.get_bool_setting("general.hideWatched") self.date_delay = g.get_bool_setting("general.datedelay") self.page_limit = g.get_int_setting("item.limit")
def get_resume_episode(self): """ Fetches playback information for current show and identifies the next episode to be resumed/watched :return: (Season, Episode) tuple :rtype: tuple """ get = MetadataHandler().get_trakt_info info = MetadataHandler().info try: playback_history = self.trakt_api.get_json( "sync/history/shows/{}".format(self.show_trakt_id), extended="full", limit=1)[0] action = playback_history["action"] episode_info = playback_history["episode"] season = get(episode_info, "season") episode = get(episode_info, "episode") except IndexError: # Capture failure to get old playback and resume from first episode action = "watch" season = 1 episode = 1 if action != "watch": episode += 1 all_seasons = self.get_season_info() season_info = [ i for i in all_seasons if info(i).get("season") == season ][0] if episode >= info(season_info).get("episode_count"): season += 1 episode = 1 if self.final_episode_check(season, episode): season = 1 episode = 1 season_id = info([ i for i in all_seasons if info(i).get("season") == season ][0]).get("trakt_id") return season_id, episode
def get_movie_list(self, trakt_list, **params): self._update_movies(trakt_list) query = """SELECT m.trakt_id, m.info, m.art, m.cast, m.args, b.resume_time, b.percent_played, m.watched as play_count FROM movies as m left join bookmarks as b on m.trakt_id = b.trakt_id WHERE m.trakt_id in ({})""".format( ",".join((str(i.get("trakt_id")) for i in trakt_list))) if params.get("hide_unaired", self.hide_unaired): query += " AND Datetime(air_date) < Datetime('now')" if params.get("hide_watched", self.hide_watched): query += " AND watched = 0" return MetadataHandler.sort_list_items(self.fetchall(query), trakt_list)
def save_to_meta_table(self, items, meta_type, provider_type, id_column): if items is None: return sql_statement = "replace into {}_meta (id ,type, meta_hash, value) VALUES (?, ?, ?, ?)".format( meta_type) obj = None meta_hash = None if provider_type == "trakt": obj = MetadataHandler.trakt_object meta_hash = self.trakt_api.meta_hash elif provider_type == "tmdb": obj = MetadataHandler.tmdb_object meta_hash = self.metadataHandler.tmdb_api.meta_hash elif provider_type == "tvdb": obj = MetadataHandler.tvdb_object meta_hash = self.metadataHandler.tvdb_api.meta_hash elif provider_type == "fanart": obj = MetadataHandler.fanart_object meta_hash = self.metadataHandler.fanarttv_api.meta_hash elif provider_type == "omdb": obj = MetadataHandler.omdb_object meta_hash = self.metadataHandler.omdb_api.meta_hash if obj is None or meta_hash is None: raise UnsupportedProviderType(provider_type) self.execute_sql( sql_statement, ((i.get(id_column), provider_type, meta_hash, self.clean_meta(obj(i))) for i in items if i and obj(i) and i.get(id_column) and MetadataHandler.full_meta_up_to_par(meta_type, obj(i))), ) for i in items: if i and obj(i): if obj(i).get("seasons"): self.save_to_meta_table(i.get("seasons"), "season", provider_type, id_column) if obj(i).get("episodes"): self.save_to_meta_table(i.get("episodes"), "episode", provider_type, id_column)