Ejemplo n.º 1
0
    def _sync_uncollected(self):

        if not self.silent:
            self.progress_dialog.update(0, 'Fetching Uncollected Movies')
        trakt_watched_movies = Trakt.TraktAPI().json_response('sync/collected/movies')
        trakt_watched_movies = [i['movie']['ids']['trakt'] for i in trakt_watched_movies]
        local_watched_movies = movie_sync.get_collected_movies()
        local_watched_movies = [i['trakt_id'] for i in local_watched_movies]

        if not self.silent:
            self.progress_dialog.update(0, 'Fetching Uncollected Episodes')
        trakt_watched_episodes = Trakt.TraktAPI().json_response('sync/collected/shows')
        trakt_watched_episodes = ['%s-%s-%s' % (show['show']['ids']['trakt'], season['number'], episode['number'])
                                  for show in trakt_watched_episodes for season in show['seasons'] for episode
                                  in season['episodes']]
        local_watched_episodes = show_sync.get_collected_episodes()
        local_watched_episodes = ['%s-%s-%s' % (i['show_id'], i['season'], i['number']) for i in local_watched_episodes]

        for movie in local_watched_movies:
            if movie not in trakt_watched_movies:
                movie_sync.mark_movie_uncollected(movie)

        for episode in local_watched_episodes:
            if episode not in trakt_watched_episodes:
                show_sync.mark_episode_unwatched(*episode.split('-'))
Ejemplo n.º 2
0
    def get_single_show(self, show_id, list_mode=False, watch_info=True, get_meta=True):
        tools.traktSyncDB_lock.acquire()
        # Get show from Database if it exsits, else create new record
        cursor = self._get_cursor()
        cursor.execute('SELECT * FROM shows WHERE trakt_id=?', (int(show_id),))
        item = cursor.fetchone()
        cursor.close()
        tools.try_release_lock(tools.traktSyncDB_lock)

        if item is None:
            if get_meta:
                show_item = database.get(trakt.TraktAPI().json_response, 24, '/shows/%s?extended=full' % show_id)
            else:
                show_item = None
            item = self._update_show(show_id, show_item, get_meta)

        else:
            if item['kodi_meta'] == '{}' and get_meta:
                show_item = database.get(trakt.TraktAPI().json_response, 24, '/shows/%s?extended=full' % show_id)
                item = self._update_show(show_id, show_item, get_meta)
            else:
                item['kodi_meta'] = ast.literal_eval(item['kodi_meta'])

        if item is None:
            return

        if watch_info and get_meta:
            item['kodi_meta'] = self.get_show_watched_info(item['kodi_meta'])

        if list_mode:
            self.item_list.append(copy.deepcopy(item['kodi_meta']))
        else:
            return item['kodi_meta']
Ejemplo n.º 3
0
    def get_movie(self,
                  trakt_id,
                  list_mode=False,
                  get_meta=True,
                  watched=None,
                  collected=None):
        cursor = self._get_cursor()
        cursor.execute('SELECT * FROM movies WHERE trakt_id=? ', (trakt_id, ))
        item = cursor.fetchone()
        cursor.close()

        if item is None:

            movie_object = trakt.TraktAPI().json_response(
                '/movies/%s?extended=full' % trakt_id)

            if movie_object is None:
                return

            item = self._update_movie(movie_object, get_meta)

        else:
            if item['kodi_meta'] != '{}':
                item['kodi_meta'] = ast.literal_eval(item['kodi_meta'])
            else:
                if get_meta:
                    movie_object = trakt.TraktAPI().json_response(
                        '/movies/%s?extended=full' % trakt_id)
                    if movie_object is None:
                        return

                    item = self._update_movie(movie_object, get_meta)
                else:
                    item['kodi_meta'] = ast.literal_eval(item['kodi_meta'])

        if item is None:
            return

        if item['collected'] == 0 and collected == 1:
            self._mark_movie_record('collected', 1, item['trakt_id'])
            item['collected'] = 1

        if item['watched'] == 0 and watched == 1:
            self._mark_movie_record('watched', 1, item['trakt_id'])
            item['watched'] = 1

        try:
            if item['watched'] == 1:
                item['kodi_meta']['info']['playcount'] = 1
            else:
                item['kodi_meta']['info']['playcount'] = 0
        except:
            pass

        if list_mode:
            self.item_list.append(copy.deepcopy(item['kodi_meta']))
        else:
            return item['kodi_meta']
        pass
Ejemplo n.º 4
0
    def _sync_uncollected(self):
        show_sync = shows.TraktSyncDatabase()
        movie_sync = movies.TraktSyncDatabase()
        trakt_collected_movies = Trakt.TraktAPI().json_response(
            'sync/collection/movies')

        if trakt_collected_movies is not None:
            trakt_collected_movies = set(
                int(i['movie']['ids']['trakt'])
                for i in trakt_collected_movies)
        else:
            trakt_collected_movies = set()

        local_collected_movies = movie_sync.get_collected_movies()
        local_collected_movies = set(
            int(i['trakt_id']) for i in local_collected_movies)
        trakt_collected_episodes = Trakt.TraktAPI().json_response(
            'sync/collection/shows')

        if trakt_collected_episodes is not None:
            trakt_collected_episodes = set(
                '%s-%s-%s' % (show['show']['ids']['trakt'], season['number'],
                              episode['number'])
                for show in trakt_collected_episodes
                for season in show['seasons']
                for episode in season['episodes'])
        else:
            trakt_collected_episodes = set()

        local_collected_episodes = show_sync.get_collected_episodes()
        local_collected_episodes = set('%s-%s-%s' %
                                       (i['show_id'], i['season'], i['number'])
                                       for i in local_collected_episodes)

        workload = local_collected_movies - trakt_collected_movies
        sql_statement = "UPDATE movies SET collected=0 WHERE trakt_id=?"
        self._execute_batch_sql(sql_statement, ((movie, )
                                                for movie in workload),
                                len(workload))

        workload = local_collected_episodes - trakt_collected_episodes
        sql_statement = "UPDATE episodes SET collected=0 WHERE show_id=? AND season=? AND number=?"
        self._execute_batch_sql(sql_statement, ((tuple(episode.split('-')))
                                                for episode in workload),
                                len(workload))

        if not self.silent:
            self.progress_dialog.update(100, 'Syncing Uncollected items')
def cache_large_info_requests():
    from resources.lib.indexers import trakt
    trakt = trakt.TraktAPI()
    database.get(trakt.json_response,
                 .5,
                 'users/me/watched/shows?extended=full',
                 limit=False)
Ejemplo n.º 6
0
    def _sync_collection_movies(self):
        movie_sync = movies.TraktSyncDatabase()

        insert_list = []
        if not self.silent:
            self.progress_dialog.update(0, 'Fetching Collected Movies')
        local_collection = set(i['trakt_id']
                               for i in movie_sync.get_collected_movies())

        update_time = str(datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S'))
        trakt_collecton = Trakt.TraktAPI().json_response(
            'sync/collection/movies?extended=full')

        for item in trakt_collecton:
            if item['movie']['ids']['trakt'] not in local_collection:
                insert_list.append(item)

        if not self.silent:
            self.progress_dialog.update(0, 'Inserting Collected Movies')

        sql_statement = "INSERT OR IGNORE INTO movies (trakt_id, kodi_meta, collected, watched, last_updated, " \
                        "air_date) VALUES (?, '{}', ?, ?, ?, ?) "
        self._execute_batch_sql(sql_statement,
                                ((i['movie']['ids']['trakt'], 1, 0,
                                  self.base_date, i['movie'].get('released'))
                                 for i in insert_list), len(insert_list))

        sql_statement = "UPDATE movies SET collected=1 WHERE trakt_id=?"

        self._execute_batch_sql(sql_statement, [(i['movie']['ids']['trakt'], )
                                                for i in trakt_collecton],
                                len(trakt_collecton))

        self._update_activity_record('movies_collected', update_time)
Ejemplo n.º 7
0
    def get_single_season(self, show_id, season, list_mode=False, get_meta=True):

        show_meta = self.get_single_show(show_id)

        if show_meta is None:
            return
        tools.traktSyncDB_lock.acquire()
        cursor = self._get_cursor()
        cursor.execute('SELECT * FROM seasons WHERE show_id=? AND season=?', (show_id, season))
        item = cursor.fetchone()
        cursor.close()
        tools.try_release_lock(tools.traktSyncDB_lock)

        if item is None or (item['kodi_meta'] == '{}' and get_meta):
            try:
                season_meta = trakt.TraktAPI().json_response('shows/%s/seasons/?extended=full' % show_id)
                season_meta = [i for i in season_meta if i['number'] == int(season)][0]
                item = self._update_season(show_meta, season_meta, get_meta)
                if item is None:
                    return
            except:
                return None
        else:
            item['kodi_meta'] = ast.literal_eval(item['kodi_meta'])

        item['kodi_meta'] = self.get_season_watch_info(item['kodi_meta'])

        if list_mode:
            self.item_list.append(copy.deepcopy(item['kodi_meta']))
        else:
            return item['kodi_meta']
Ejemplo n.º 8
0
 def _pull_show_episodes(self, show_id):
     self.results_mill.update({
         str(show_id):
         database.get(
             Trakt.TraktAPI().json_response, 24,
             '/shows/{}/seasons?extended=episodes%2Cfull'.format(show_id))
     })
Ejemplo n.º 9
0
    def _sync_collection_movies(self):

        insert_list = []
        if not self.silent:
            self.progress_dialog.update(0, 'Fetching Collected Movies')
        local_collection = [
            i['trakt_id'] for i in movie_sync.get_collected_movies()
        ]
        update_time = str(datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S'))
        trakt_collecton = [
            i['movie']['ids']['trakt']
            for i in Trakt.TraktAPI().json_response('sync/collection/movies')
        ]

        for item in trakt_collecton:
            if item not in local_collection:
                insert_list.append(item)

        inserted_tasks = 0
        cursor = self._get_cursor()

        if not self.silent:
            self.progress_dialog.update(0, 'Inserting Collected Movies')

        sql_statements = 0

        for i in insert_list:
            inserted_tasks += 1
            progress_perc = (float(inserted_tasks) /
                             float(len(insert_list))) * 100
            if not self.silent:
                self.progress_dialog.update(int(progress_perc))
            cursor.execute(
                "INSERT OR IGNORE INTO movies ("
                "trakt_id, kodi_meta, collected, watched, last_updated)"
                "VALUES "
                "(?, ?, ?, ?, ?)", (i, str({}), 0, 0, self.base_date))

            # Batch the entries as to not reach SQL expression limit
            sql_statements += 1
            if sql_statements > 999:
                cursor.connection.commit()
                sql_statements = 0

        cursor.connection.commit()
        cursor.close()

        cursor = self._get_cursor()

        if not self.silent:
            self.progress_dialog.update(0, 'Marking Movies Collected')

        for item in insert_list:
            cursor.execute('UPDATE movies SET collected=1 WHERE trakt_id=?',
                           (item, ))
            # movie_sync.mark_movie_collected(item[1][0])
        cursor.connection.commit()
        cursor.close()

        self._update_activity_record('movies_collected', update_time)
Ejemplo n.º 10
0
    def __init__(self):
        self.trakt_api = trakt.TraktAPI()
        self.pre_cache_initiated = False
        self.play_next_triggered = False
        self.trakt_id = None
        self.media_type = None
        self.offset = None
        self.media_length = 0
        self.current_time = 0
        self.args = {}
        self.playback_started = False
        self.playing_file = None
        self.AVStarted = False
        self.scrobbling_enabled = tools.getSetting(
            'trakt.scrobbling') == 'true'
        self.scrobbled = False
        self.original_action_args = ''
        self.smart_playlists = tools.getSetting('smartplay.playlistcreate')
        self.smart_module = None
        self.ignoreSecondsAtStart = int(
            tools.get_advanced_setting('video', 'ignoresecondsatstart'))
        self.ignorePercentAtEnd = int(
            tools.get_advanced_setting('video', 'ignorepercentatend'))
        self.playCountMinimumPercent = int(
            tools.get_advanced_setting('video', 'playcountminimumpercent'))

        self.min_time_before_scrape = int(tools.getSetting('playingnext.time')) + \
                                      int(int(tools.getSetting('general.timeout'))) + 20
        self.marked_watched = False
        tools.player.__init__(self)
Ejemplo n.º 11
0
    def get_flat_episode_list(self, show_id):

        show_meta = self.get_single_show(show_id)
        tools.traktSyncDB_lock.acquire()
        cursor = self._get_cursor()

        cursor.execute('SELECT * FROM episodes WHERE show_id=?', (show_id,))
        episodes = cursor.fetchall()
        cursor.close()
        tools.try_release_lock(tools.traktSyncDB_lock)
        try:
            if len(episodes) != int(show_meta['info']['episode_count']):
                raise Exception

            if len([i for i in episodes if i['kodi_meta'] == '{}']) > 0:
                raise Exception

            return self.get_meta_episode_list(episodes, [show_meta])

        except:

            seasons = trakt.TraktAPI().json_response('shows/%s/seasons?extended=episodes' % show_id)
            episodes = [episode for season in seasons for episode in season['episodes']]

            for i in episodes:
                self.task_queue.put(self.get_single_episode, show_id, i['season'], i['number'], True)

            self.task_queue.wait_completion()

            return self.item_list
Ejemplo n.º 12
0
 def _update_watched(self, show_id):
     from activities import TraktSyncDatabase as activities_database
     progress = trakt.TraktAPI().json_response('shows/{}/progress/watched?'
                                               'specials=true&extended=full'.format(show_id))
     progress = [(show_id, season['number'], episode['number']) for season in progress['seasons']
                 for episode in season['episodes'] if episode['completed']]
     sync_thread = threading.Thread(target=activities_database()._mill_episodes, args=(progress, 1))
     sync_thread.run()
Ejemplo n.º 13
0
    def get_season_list(self, show_id):
        self.threads = []
        show_meta = self.get_single_show(show_id)
        tools.traktSyncDB_lock.acquire()
        cursor = self._get_cursor()
        cursor.execute('SELECT* FROM seasons WHERE show_id = ?', (show_id, ))
        seasons = cursor.fetchall()
        cursor.close()
        tools.try_release_lock(tools.traktSyncDB_lock)
        season_count = int(show_meta['info']['season_count'])
        try:
            if len([i for i in seasons if i['kodi_meta'] == '{}']) > 0:
                raise Exception
            if len([i for i in seasons
                    if int(i['season']) != 0]) == season_count:
                seasons = [i for i in seasons if i['kodi_meta'] != '{}']
                if len(seasons) == 0 or len(seasons) != season_count:
                    raise Exception
                seasons = [
                    ast.literal_eval(season['kodi_meta']) for season in seasons
                ]
                seasons = [
                    self.get_season_watch_info(season) for season in seasons
                ]
                return seasons
        except:
            # We likely haven't built the meta information yet
            pass

        seasons = trakt.TraktAPI().json_response('shows/%s/seasons' %
                                                 show_meta['ids']['trakt'])

        # Maybe we can add here other providers to get some more information out
        # if seasons is None:
        #    return self.item_list

        for season in seasons:
            self.threads.append(
                threading.Thread(target=self.get_single_season,
                                 args=(show_meta['ids']['trakt'],
                                       season['number'], True)))

        for i in self.threads:
            i.start()

        for i in self.threads:
            # What the actual f**k? There are a stack of threads being created somehow after the first start is called
            # If someone can spot the issue here, I'd love to know what the f**k I've done wrong lol
            try:
                i.join()
            except:
                break

        self.threads = []

        return self.item_list
Ejemplo n.º 14
0
    def get_season_episodes(self, show_id, season):

        try:

            cursor = self._get_cursor()
            cursor.execute('SELECT * FROM shows WHERE trakt_id=?', (show_id,))
            show_object = cursor.fetchone()
            cursor.execute('SELECT * FROM seasons WHERE show_id=? AND season=?', (show_id, season))
            season_object = cursor.fetchone()
            cursor.execute('SELECT * FROM episodes WHERE show_id=? AND season=?', (show_id, season))
            season_episodes = cursor.fetchall()
            cursor.close()

            show_object = ast.literal_eval(show_object['kodi_meta'])
            season_meta = ast.literal_eval(season_object['kodi_meta'])
            season_episode_count = season_meta['info']['episode_count']
            season_aired_count = season_meta['info']['aired_episodes']

            if int(season_episode_count) == 0:
                raise Exception

            if int(season_episode_count) > int(season_aired_count):
                # Because of trakt caching, we can not trust the information gathered on the last call if the season
                # is not completely aired. We can limit the amount this slow down is occured by limiting it to
                # only unfinished seasons
                raise Exception

            if len(season_episodes) < int(season_aired_count):
                raise Exception

            if len([i for i in season_episodes if i['kodi_meta'] == '{}']) > 0:
                raise Exception

            for episode in season_episodes:
                episode['kodi_meta'] = ast.literal_eval(episode['kodi_meta'])

            for episode in season_episodes:
                episode = self.update_episode_playcount(episode)
                episode['kodi_meta'].update({'showInfo': show_object})

            return [episode['kodi_meta'] for episode in season_episodes]

        except:
            import traceback
            traceback.print_exc()
            trakt_list = database.get(trakt.TraktAPI().json_response, 24, 'shows/%s/seasons/%s' % (show_id, season))

            self._start_queue_workers()

            for i in trakt_list:
                self.task_queue.put((self.get_single_episode, (show_id, season, i['number'], True)), True)

            self._finish_queue_workers()

            return self.item_list
Ejemplo n.º 15
0
    def __init__(self):
        super(SerenPlayer, self).__init__()

        self._trakt_api = trakt.TraktAPI()
        self.trakt_id = None
        self.mediatype = None
        self.offset = None
        self.playing_file = None
        self.scrobbling_enabled = g.get_bool_setting("trakt.scrobbling")
        self.item_information = None
        self.smart_playlists = g.get_bool_setting("smartplay.playlistcreate")
        self.default_action = g.get_int_setting("smartplay.defaultaction")
        self.smart_module = None
        self.current_time = 0
        self.total_time = 0
        self.watched_percentage = 0
        self.ignoreSecondsAtStart = g.get_int_setting(
            "trakt.ignoreSecondsAtStart")
        self.min_time_before_scrape = max(self.total_time * 0.2, 600)
        self.playCountMinimumPercent = g.get_int_setting(
            "trakt.playCountMinimumPercent")
        self.intro_dialog_enabled = g.get_bool_setting("skip.intro.dialog")
        self.intro_dialog_delay = g.get_int_setting("skip.intro.dialog.delay")
        self.playing_next_dialog_enabled = g.get_bool_setting(
            "smartplay.playingnextdialog")
        self.still_watching_dialog_enabled = g.get_bool_setting(
            "smartplay.stillwatching")
        self.intro_dialog_open_time = g.get_int_setting("skip.intro.open.time")
        self.pre_scrape_enabled = g.get_bool_setting("smartPlay.preScrape")
        self.playing_next_time = g.get_int_setting("playingnext.time")
        self.skip_intro = g.get_bool_setting("skip.intro")
        self.skip_intro_from = g.get_int_setting("skip.intro.from")
        self.skip_intro_to = g.get_int_setting("skip.intro.to")
        self.force_skip = g.get_bool_setting("skip.intro.force")
        self.bookmark_sync = bookmark.TraktSyncDatabase()
        self.trakt_enabled = True if g.get_setting("trakt.auth", "") else False
        self._running_path = None

        # Flags
        self.default_pause = False
        self.default_exit = False
        self.resumed = False
        self.playback_started = False
        self.playback_error = False
        self.playback_ended = False
        self.playback_stopped = False
        self.scrobbled = False
        self.scrobble_started = False
        self._force_marked_watched = False
        self.intro_dialog_triggered = False
        self.playing_next_dialog_triggered = False
        self.still_watching_dialog_triggered = False
        self.pre_scrape_initiated = False
        self.playback_timestamp = 0
        self.intro_skipped = False
Ejemplo n.º 16
0
 def __init__(self):
     tools.player.__init__(self)
     self.trakt_api = trakt.TraktAPI()
     self.pre_cache_initiated = False
     self.play_next_triggered = False
     self.trakt_id = None
     self.media_type = None
     self.offset = None
     self.media_length = 0
     self.current_time = 0
     self.stopped = False
Ejemplo n.º 17
0
    def _sync_hidden(self):
        progress_perc = 0
        trakt_api = Trakt.TraktAPI()
        sections = [
            'calendar', 'progress_watched', 'progress_watched_reset',
            'progress_collected', 'recommendations'
        ]
        update_time = str(datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S'))

        for section in sections:
            progress_perc += 20
            if not self.silent:
                self.progress_dialog.update(
                    progress_perc, 'Syncing %s Hidden Items' %
                    section.title().replace('_', ' '))
            to_remove = hidden.TraktSyncDatabase().get_hidden_items(section)
            to_remove = set(i['trakt_id'] for i in to_remove)
            page = 1
            total_pages = 1000
            while page < (total_pages + 1):
                hidden_items = trakt_api.json_response(
                    'users/hidden/%s?page=%s' % (section, page))
                if hidden_items is None:
                    return
                page = int(trakt_api.response_headers['X-Pagination-Page']) + 1
                total_pages = int(
                    trakt_api.response_headers['X-Pagination-Page-Count'])

                for item in hidden_items:
                    if 'show' in item:
                        item_id = item['show']['ids']['trakt']
                        item_type = 'show'
                    elif 'movie' in item:
                        item_type = 'movie'
                        item_id = item['movie']['ids']['trakt']
                    else:
                        continue
                    if item_id in to_remove:
                        to_remove.remove(item_id)
                    else:
                        hidden.TraktSyncDatabase().add_hidden_item(
                            item_id, item_type, section)

            if not self.silent:
                self.progress_dialog.update(100, 'Syncing Unhidden items')
            for item in to_remove:
                try:
                    hidden.TraktSyncDatabase().remove_item(section, item)
                except:
                    import traceback
                    traceback.print_exc()
                    pass

        self._update_activity_record('hidden_sync', update_time)
Ejemplo n.º 18
0
 def __init__(self):
     tools.player.__init__(self)
     self.trakt_api = trakt.TraktAPI()
     self.pre_cache_initiated = False
     self.play_next_triggered = False
     self.trakt_id = None
     self.media_type = None
     self.offset = None
     self.media_length = 0
     self.current_time = 0
     self.args = {}
     self.playback_started = False
     self.scrobbled = False
     self.playback_resumed = False
Ejemplo n.º 19
0
    def _remove_old_meta_items(self, type):

        last_update = self.activites['%s_meta_update' % type]
        update_time = str(datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S'))

        trakt_api = Trakt.TraktAPI()
        updated_item = []
        url = '%s/updates/%s?page=%s'
        if not self.silent:
            self.progress_dialog.update(0, 'Clearing Outdated %s Metadata' % type[:-1].title())
        updates = trakt_api.json_response(url % (type, last_update[:10], 1), limitOverride=500, limit=True)

        for item in updates:
            if not trakt_sync._requires_update(item['updated_at'], last_update):
                continue
            item_id = item[type[:-1]]['ids']['trakt']
            updated_item.append(item_id)

        for i in range(2, int(trakt_api.response_headers['X-Pagination-Page-Count']) + 1):
            progress = (i / (int(trakt_api.response_headers['X-Pagination-Page-Count']) + 1)) * 100
            if not self.silent:
                self.progress_dialog.update(progress)
            updates = trakt_api.json_response(url % (type, last_update[:10], i), limitOverride=500, limit=True)
            for item in updates:
                if not trakt_sync._requires_update(item['updated_at'], last_update):
                    continue
                item_id = item[type[:-1]]['ids']['trakt']
                updated_item.append(item_id)

        cursor = self._get_cursor()

        updated_item = sorted(list(set(updated_item)))


        if type == 'shows':
            for i in updated_item:
                cursor.execute('UPDATE shows SET kodi_meta=?, last_updated=? WHERE trakt_id=?',
                               (str({}), update_time, i))
                cursor.execute('UPDATE episodes SET kodi_meta=?, last_updated=? WHERE show_id=?',
                               (str({}), update_time, i))
                cursor.execute('UPDATE seasons SET kodi_meta=? WHERE show_id=?', (str({}), i))
        elif type == 'movies':
            for i in updated_item:
                cursor.execute('UPDATE movies SET kodi_meta=?, last_updated=? WHERE trakt_id=?',
                               (str({}), update_time, i))
        else:
            raise Exception
        cursor.connection.commit()
        cursor.close()
Ejemplo n.º 20
0
    def _sync_watched_episodes(self):

        update_time = str(datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S'))
        if not self.silent:
            self.progress_dialog.update(-1, 'Fetching Watched Episodes')
        trakt_watched = Trakt.TraktAPI().json_response('/sync/watched/shows')
        trakt_watched = ['%s-%s-%s' % (show['show']['ids']['trakt'], season['number'], episode['number'])
                         for show in trakt_watched for season in show['seasons'] for episode
                         in season['episodes']]

        local_watched = shows.TraktSyncDatabase().get_watched_episodes()
        local_watched = ['%s-%s-%s' % (i['show_id'], i['season'], i['number']) for i in local_watched]

        self._mill_episodes(trakt_watched, local_watched, 1, None)

        self._update_activity_record('shows_watched', update_time)
Ejemplo n.º 21
0
    def _sync_collection_shows(self):

        local_collection = ['%s-%s-%s' % (i['show_id'], i['season'], i['number'])
                            for i in show_sync.get_collected_episodes()]

        update_time = str(datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S'))
        if not self.silent:
            self.progress_dialog.update(0, 'Fetching Collected Episdoes')
        trakt_collection = Trakt.TraktAPI().json_response('sync/collection/shows')
        trakt_collection = ['%s-%s-%s' % (show['show']['ids']['trakt'], season['number'], episode['number'])
                            for show in trakt_collection for season in show['seasons'] for episode
                            in season['episodes']]

        self._mill_episodes(trakt_collection, local_collection, None, 1)

        self._update_activity_record('shows_collected', update_time)
Ejemplo n.º 22
0
    def _sync_watched_movies(self):
        self.threads = []

        if tools.getSetting('trakt.auth') == '':
            return

        insert_list = []

        if not self.silent:
            self.progress_dialog.update(-1, 'Fetching Watched Movies')
        update_time = str(datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S'))
        trakt_watched = Trakt.TraktAPI().json_response('/sync/watched/movies')

        trakt_watched = [i['movie']['ids']['trakt'] for i in trakt_watched]

        local_watched = movies.TraktSyncDatabase().get_watched_movies()
        local_watched = [i['trakt_id'] for i in local_watched]

        for movie in trakt_watched:
            if movie not in local_watched:
                insert_list.append((movie_sync.get_movie, (movie, False, False)))

        movie_tasks = len(insert_list)

        if movie_tasks == 0:
            self._update_activity_record('movies_watched', update_time)
            return

        inserted_tasks = 0
        if not self.silent:
            self.progress_dialog.update(0, 'Syncing Watched Movies')

        self._start_queue_workers()

        for i in insert_list:
            inserted_tasks += 1
            progress_perc = (float(inserted_tasks) / float(movie_tasks)) * 100
            if not self.silent:
                self.progress_dialog.update(int(progress_perc))
            self.task_queue.put(i, block=True)

        self._finish_queue_workers()

        for item in insert_list:
            movie_sync.mark_movie_watched(item[1][0])

        self._update_activity_record('movies_watched', update_time)
Ejemplo n.º 23
0
    def _sync_lists(self, lists_to_sync):
        trakt_api = Trakt.TraktAPI()
        media_types = ['movie', 'show']
        update_time = str(datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S'))

        total_lists = len(lists_to_sync) * len(media_types)

        processed_lists = 0
        list_sync = lists.TraktSyncDatabase()

        for media_type in media_types:
            for trakt_list in lists_to_sync:
                if not self.silent:
                    processed_lists += 1
                    self.progress_dialog.update(
                        int(float(processed_lists) / float(total_lists) * 100),
                        'Syncing lists')

                url = 'users/%s/lists/%s/items/%s?extended=full' % (
                    trakt_list['user']['ids']['slug'],
                    trakt_list['ids']['trakt'], media_type)
                list_items = trakt_api.json_response(url, limit=False)

                if list_items is None or len(list_items) == 0:
                    list_sync.remove_list(trakt_list['ids']['trakt'],
                                          media_type)
                    continue

                list_items = trakt_api.sort_list(trakt_list['sort_by'],
                                                 trakt_list['sort_how'],
                                                 list_items, media_type)
                list_items = [
                    i[media_type] for i in list_items
                    if i['type'] == media_type and i is not None
                ]
                list_sync.add_list(
                    trakt_list['ids']['trakt'], list_items, trakt_list['name'],
                    tools.quote_plus(trakt_list['user']['ids']['slug']),
                    'myLists', media_type, trakt_list['updated_at'],
                    len(list_items), trakt_list['sort_by'],
                    trakt_list['sort_how'], trakt_list['ids']['slug'])

        if not self.silent:
            self.progress_dialog.update(100, 'Syncing lists')

        self._update_activity_record('lists_sync', update_time)
Ejemplo n.º 24
0
    def _sync_collection_shows(self):
        show_sync = shows.TraktSyncDatabase()
        local_collection = [(i['show_id'], i['season'], i['number'])
                            for i in show_sync.get_collected_episodes()]

        update_time = str(datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S'))
        if not self.silent:
            self.progress_dialog.update(0, 'Fetching Collected Episodes')
        trakt_collection = Trakt.TraktAPI().json_response(
            'sync/collection/shows?extended=full')
        trakt_collection = [(show['show']['ids']['trakt'], season['number'],
                             episode['number']) for show in trakt_collection
                            for season in show['seasons']
                            for episode in season['episodes']]

        filtered = [i for i in trakt_collection if i not in local_collection]

        self._mill_episodes(filtered, False)
        self._update_activity_record('shows_collected', update_time)
Ejemplo n.º 25
0
    def _sync_bookmarks(self, type):
        update_time = str(datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S'))
        if not self.silent:
            self.progress_dialog.update(
                -1, 'Fetching {} bookmark status'.format(type))

        trakt_api = Trakt.TraktAPI()
        progress = trakt_api.json_response(
            'sync/playback/{}/?extended=full&limit=300'.format(type))

        base_sql_statement = "REPLACE INTO bookmark Values (%s, %s)"
        tools.traktSyncDB_lock.acquire()
        cursor = self._get_cursor()
        sql_statements = 0

        for i in progress:
            if i['progress'] == 0 or i['progress'] == 100:
                continue
            if i[i['type']]['runtime'] is None:
                continue
            if 'episode' in i:
                offset = int((float(i['progress'] / 100) *
                              int(i['episode']['runtime']) * 60))
                cursor.execute(base_sql_statement %
                               (i['episode']['ids']['trakt'], offset))
            if 'movie' in i:
                offset = int((float(i['progress'] / 100) *
                              int(i['movie']['runtime']) * 60))
                cursor.execute(base_sql_statement %
                               (i['movie']['ids']['trakt'], offset))

            # Batch the entries as to not reach SQL expression limit
            sql_statements += 1
            if sql_statements > 999:
                cursor.connection.commit()
                sql_statements = 0

        cursor.connection.commit()
        cursor.close()
        tools.try_release_lock(tools.traktSyncDB_lock)
        self._update_activity_record('{}_bookmarked'.format(type), update_time)
Ejemplo n.º 26
0
    def _sync_watched_movies(self):
        self.threads = []

        if tools.getSetting('trakt.auth') == '':
            return

        insert_list = []

        if not self.silent:
            self.progress_dialog.update(-1, 'Fetching Watched Movies')
        update_time = str(datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S'))
        trakt_watched = Trakt.TraktAPI().json_response(
            '/sync/watched/movies?extended=full')

        local_watched = movies.TraktSyncDatabase().get_watched_movies()
        local_watched = {i['trakt_id']: i for i in local_watched}

        for movie in trakt_watched:
            if movie['movie']['ids']['trakt'] not in local_watched:
                insert_list.append(movie)

        movie_tasks = len(insert_list)

        if movie_tasks == 0:
            self._update_activity_record('movies_watched', update_time)
            return

        if not self.silent:
            self.progress_dialog.update(0, 'Inserting Watched Movies')

        sql_statement = "INSERT OR IGNORE INTO movies (trakt_id, kodi_meta, collected, watched, last_updated, air_date)" \
                        "VALUES " \
                        "(?, '{}', ?, ?, ?, ?)"

        self._execute_batch_sql(sql_statement,
                                ((i['movie']['ids']['trakt'], 0, 1,
                                  self.base_date, i['movie'].get('released'))
                                 for i in insert_list), movie_tasks)

        self._update_activity_record('movies_watched', update_time)
Ejemplo n.º 27
0
    def _sync_collection_movies(self):

        insert_list = []
        if not self.silent:
            self.progress_dialog.update(01, 'Fetching Collected Movies')
        local_collection = [i['trakt_id'] for i in movie_sync.get_collected_movies()]
        update_time = str(datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S'))
        trakt_collecton = [i['movie']['ids']['trakt'] for i in Trakt.TraktAPI().json_response('sync/collection/movies')]

        for item in trakt_collecton:
            if item not in local_collection:
                insert_list.append((movie_sync.get_movie, (item, False, False)))

        self._start_queue_workers()

        for i in insert_list:
            self.task_queue.put(i, block=True)

        self._finish_queue_workers()

        for item in insert_list:
            movie_sync.mark_movie_collected(item[1][0])

        self._update_activity_record('movies_collected', update_time)
Ejemplo n.º 28
0
    def get_single_episode(self, show_id, season, episode, list_mode=False, get_meta=True,
                           watched=None, collected=None):
        tools.traktSyncDB_lock.acquire()
        cursor = self._get_cursor()

        cursor.execute('SELECT * FROM episodes WHERE show_id=? AND season=? AND number=?',
                       (show_id, season, episode))
        item = cursor.fetchone()
        cursor.close()
        tools.try_release_lock(tools.traktSyncDB_lock)

        show_meta = self.get_single_show(show_id, get_meta=get_meta, watch_info=False)

        if show_meta is None:
            return

        if item is None:
            episode_object = database.get(trakt.TraktAPI().json_response, 24,
                                          '/shows/%s/seasons/%s/episodes/%s?extended=full' % (show_id, season, episode))
            if episode_object is None:
                return

            item = self._update_episode(show_id, episode_object, get_meta, watched, collected)
        else:
            if get_meta and item['kodi_meta'] == '{}':
                episode_object = database.get(trakt.TraktAPI().json_response, 24,
                                              '/shows/%s/seasons/%s/episodes/%s?extended=full' %
                                              (show_id, season, episode))
                if episode_object is None:
                    return

                item = self._update_episode(show_id, episode_object, get_meta, watched, collected)
            else:
                item['kodi_meta'] = ast.literal_eval(item['kodi_meta'])

        if item is None:
            return

        item['kodi_meta'].update({'showInfo': show_meta})

        if item['collected'] == 0 and collected == 1:
            self._mark_episode_record('collected', 1, item['show_id'], item['season'], item['number'])

        if item['watched'] == 0 and watched == 1:
            self._mark_episode_record('watched', 1, item['show_id'], item['season'], item['number'])

        try:
            if get_meta:
                item['kodi_meta'] = self.clean_episode_showinfo(item['kodi_meta'])
                item = self.update_episode_playcount(item)
        except:
            import traceback
            traceback.print_exc()
            pass

        if list_mode:
            try:
                self.item_list.append(copy.deepcopy(item['kodi_meta']))
            except:
                import traceback
                traceback.print_exc()
                pass
        else:
            return item['kodi_meta']
Ejemplo n.º 29
0
    def _remove_old_meta_items(self, type):
        last_update = self.activites['%s_meta_update' % type]
        update_time = str(datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S'))
        success = True

        trakt_api = Trakt.TraktAPI()
        updated_item = []
        url = '%s/updates/%s?page=%s'

        if not self.silent:
            self.progress_dialog.update(
                0, 'Clearing Outdated %s Metadata' % type[:-1].title())

        updates = trakt_api.json_response(url % (type, last_update[:10], 1),
                                          limitOverride=500,
                                          limit=True)

        for item in updates:
            if not trakt_sync._requires_update(item['updated_at'],
                                               last_update):
                continue
            item_id = item[type[:-1]]['ids']['trakt']
            updated_item.append(item_id)

        for i in range(
                2,
                int(trakt_api.response_headers['X-Pagination-Page-Count']) +
                1):
            progress = (
                i /
                (int(trakt_api.response_headers['X-Pagination-Page-Count']) +
                 1)) * 100
            if not self.silent:
                self.progress_dialog.update(progress)
            updates = trakt_api.json_response(url %
                                              (type, last_update[:10], i),
                                              limitOverride=500,
                                              limit=True)
            for item in updates:
                if not trakt_sync._requires_update(item['updated_at'],
                                                   last_update):
                    continue
                item_id = item[type[:-1]]['ids']['trakt']
                updated_item.append(item_id)

        tools.traktSyncDB_lock.acquire()
        cursor = self._get_cursor()

        try:
            updated_item = sorted(list(set(updated_item)))

            sql_statements = 0

            if type == 'shows':
                for i in updated_item:
                    cursor.execute(
                        'UPDATE shows SET kodi_meta=?, last_updated=? WHERE trakt_id=?',
                        ('{}', update_time, i))
                    cursor.execute(
                        'UPDATE episodes SET kodi_meta=?, last_updated=? WHERE show_id=?',
                        ('{}', update_time, i))
                    cursor.execute(
                        'UPDATE seasons SET kodi_meta=? WHERE show_id=?',
                        ('{}', i))

                    sql_statements += 3

                    # Batch the entries as to not reach SQL expression limit
                    if sql_statements > 500:
                        cursor.connection.commit()
                        sql_statements = 0

            elif type == 'movies':
                for i in updated_item:
                    cursor.execute(
                        'UPDATE movies SET kodi_meta=?, last_updated=? WHERE trakt_id=?',
                        (str({}), update_time, i))
                    # Batch the entries as to not reach SQL expression limit
                    sql_statements += 1
                    if sql_statements > 999:
                        cursor.connection.commit()
                        sql_statements = 0

        except database.OperationalError:
            tools.log('Failed to update some meta items')
            success = False
        finally:
            cursor.connection.commit()
            cursor.close()

        tools.try_release_lock(tools.traktSyncDB_lock)
        return success
Ejemplo n.º 30
0
    from resources.lib.gui import movieMenus

    movieMenus.Menus().myMovieWatchlist()

if action == 'moviesRelated':
    from resources.lib.gui import movieMenus

    movieMenus.Menus().moviesRelated(actionArgs)

if action == 'colorPicker':
    tools.colorPicker()

if action == 'authTrakt':
    from resources.lib.indexers import trakt

    trakt.TraktAPI().auth()

if action == 'revokeTrakt':
    from resources.lib.indexers import trakt

    trakt.TraktAPI().revokeAuth()

if action == 'getSources':

    try:
        if tools.playList.getposition() == 0:
            display_background = True
        else:
            display_background = False

        background = windows.persistant_background()