Esempio n. 1
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()
Esempio n. 2
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
Esempio n. 3
0
    def sync_activities(self, silent=False):
        sync_errors = False

        update_time = str(datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S'))

        tools.log('STARTING SYNC')

        self._refresh_activites()

        if not silent and \
                str(self.activites['all_activities']) == self.base_date and \
                tools.getSetting('trakt.auth') != '':
            tools.showDialog.notification(tools.addonName, tools.lang(40133))

            # Give the people time to read the damn notification
            tools.kodi.sleep(500)
        try:

            if str(self.activites['all_activities']) == self.base_date:
                self.silent = False

            if not self.silent:
                self.progress_dialog = tools.bgProgressDialog()
                self.progress_dialog.create(tools.addonName + 'Sync',
                                            'Seren: Trakt Sync')

            ############################################################################################################
            # CHECK FOR META REFRESH
            ############################################################################################################

            # Meta removal should only run every 12 hours, otherwise we repeatedly dump information every run

            try:
                if self.activites['shows_meta_update'] == self.base_date:
                    self._update_activity_record('shows_meta_update',
                                                 update_time)
                else:
                    local_date = trakt_sync._parse_local_date_format(
                        self.activites['shows_meta_update'])
                    local_date = local_date + timedelta(hours=2)
                    now = trakt_sync._utc_now_as_trakt_string()
                    local_date = trakt_sync._strf_local_date(local_date)
                    if trakt_sync._requires_update(now, local_date):
                        success = self._remove_old_meta_items('shows')
                        if success:
                            self._update_activity_record(
                                'shows_meta_update', update_time)
            except:
                sync_errors = True
                import traceback
                traceback.print_exc()
                pass

            try:
                if self.activites['movies_meta_update'] == self.base_date:
                    self._update_activity_record('movies_meta_update',
                                                 update_time)
                else:
                    local_date = trakt_sync._parse_local_date_format(
                        self.activites['movies_meta_update'])
                    local_date = local_date + timedelta(hours=2)
                    now = trakt_sync._utc_now_as_trakt_string()
                    local_date = trakt_sync._strf_local_date(local_date)
                    if trakt_sync._requires_update(now, local_date):
                        success = self._remove_old_meta_items('movies')
                        if success:
                            self._update_activity_record(
                                'movies_meta_update', update_time)
            except:
                sync_errors = True
                import traceback
                traceback.print_exc()
                pass

            if tools.getSetting('trakt.auth') == '':
                if self.progress_dialog is not None:
                    self.progress_dialog.close()
                    self.progress_dialog = None
                self._update_activity_record('all_activities', update_time)
                return

            ########################################################################################################
            # SYNC LISTS
            ########################################################################################################

            lists_to_update = []

            try:
                lists_db = lists.TraktSyncDatabase()
                trakt_api = Trakt.TraktAPI()
                my_lists = trakt_api.json_response('users/me/lists',
                                                   limit=True,
                                                   limitOverride=500)
                if my_lists is None:
                    my_lists = []
                my_lists.extend([
                    i['list'] for i in trakt_api.json_response(
                        'users/likes/lists', limit=True, limitOverride=500)
                ])
                for item in my_lists:
                    sync_dates = [
                        lists_db.get_list(item['ids']['trakt'], 'movie',
                                          item['user']['ids']['slug']),
                        lists_db.get_list(item['ids']['trakt'], 'show',
                                          item['user']['ids']['slug'])
                    ]
                    sync_dates = [i for i in sync_dates if i]
                    sync_dates = [i['updated_at'][:19] for i in sync_dates]
                    if len(sync_dates) == 0:
                        lists_to_update.append(item)
                        continue
                    for date in sync_dates:
                        if trakt_sync._requires_update(item['updated_at'],
                                                       date):
                            lists_to_update.append(item)
                            break

                self._sync_lists(lists_to_update)
            except:
                sync_errors = True
                import traceback
                traceback.print_exc()
                pass

            trakt_activities = Trakt.TraktAPI().json_response(
                'sync/last_activities')

            if trakt_activities is None:
                tools.log('Unable to connect to Trakt', 'error')
                if self.progress_dialog is not None:
                    self.progress_dialog.close()
                    self.progress_dialog = None
                return True

            if trakt_sync._requires_update(trakt_activities['all'],
                                           self.activites['all_activities']):

                ########################################################################################################
                # SYNC HIDDEN ITEMS
                ########################################################################################################
                try:
                    if not self.silent:
                        self.progress_dialog.update(0, 'Syncing Hidden Items')
                    if trakt_sync._requires_update(trakt_activities['movies']['hidden_at'],
                                                   self.activites['hidden_sync']) and \
                            trakt_sync._requires_update(trakt_activities['shows']['hidden_at'],
                                                        self.activites['hidden_sync']):
                        self._sync_hidden()

                except:
                    sync_errors = True
                    import traceback
                    traceback.print_exc()
                    pass
                ########################################################################################################
                # SYNC WATCHED ITEMS
                ########################################################################################################
                sync_triggered = False

                try:
                    if trakt_sync._requires_update(
                            trakt_activities['episodes']['watched_at'],
                            self.activites['shows_watched']):
                        sync_triggered = True
                        self._sync_watched_episodes()
                except:
                    sync_errors = True
                    import traceback
                    traceback.print_exc()
                    pass
                try:
                    if trakt_sync._requires_update(
                            trakt_activities['movies']['watched_at'],
                            self.activites['movies_watched']):
                        sync_triggered = True
                        self._sync_watched_movies()
                except:
                    sync_errors = True
                    import traceback
                    traceback.print_exc()
                    pass
                try:
                    if sync_triggered:
                        if not self.silent:
                            self.progress_dialog.update(
                                0, 'Syncing Unwatched items')
                        self._sync_unwatched()
                except:
                    sync_errors = True
                    import traceback
                    traceback.print_exc()
                    pass

                ########################################################################################################
                # SYNC COLLECTION
                ########################################################################################################

                sync_triggered = False

                try:
                    if trakt_sync._requires_update(
                            trakt_activities['episodes']['collected_at'],
                            self.activites['shows_collected']):
                        self._sync_collection_shows()
                        sync_triggered = True
                except:
                    sync_errors = True
                    import traceback
                    traceback.print_exc()
                    pass

                try:
                    if trakt_sync._requires_update(
                            trakt_activities['movies']['collected_at'],
                            self.activites['movies_collected']):
                        self._sync_collection_movies()
                        sync_triggered = True
                except:
                    sync_errors = True
                    import traceback
                    traceback.print_exc()
                    pass

                try:
                    if sync_triggered:
                        if not self.silent:
                            self.progress_dialog.update(
                                0, 'Syncing Uncollected items')
                        self._sync_uncollected()
                except:
                    sync_errors = True
                    import traceback
                    traceback.print_exc()
                    pass

                ########################################################################################################
                # SYNC BOOKMARK
                ########################################################################################################

                sync_triggered = False

                try:
                    if trakt_sync._requires_update(
                            trakt_activities['episodes']['paused_at'],
                            self.activites['episodes_bookmarked']):
                        cursor = self._get_cursor()
                        cursor.execute('DELETE FROM bookmark WHERE 1=1')
                        cursor.connection.commit()
                        cursor.close()
                        self._sync_bookmarks('episodes')
                        sync_triggered = True
                except:
                    sync_errors = True
                    import traceback
                    traceback.print_exc()
                    pass

                try:
                    if trakt_sync._requires_update(
                            trakt_activities['movies']['paused_at'],
                            self.activites['movies_bookmarked']):
                        self._sync_bookmarks('movies')
                        sync_triggered = True
                except:
                    sync_errors = True
                    import traceback
                    traceback.print_exc()
                    pass

                try:
                    if sync_triggered:
                        if not self.silent:
                            self.progress_dialog.update(
                                100, 'Syncing bookmarked items')
                except:
                    sync_errors = True
                    import traceback
                    traceback.print_exc()
                    pass

                self._update_activity_record('all_activities', update_time)

            if self.progress_dialog is not None:
                self.progress_dialog.close()
                self.progress_dialog = None
        except:
            try:
                if self.progress_dialog is not None:
                    self.progress_dialog.close()
                    self.progress_dialog = None
            except:
                pass
            import traceback
            traceback.print_exc()
            pass

        return sync_errors
Esempio n. 4
0
    def sync_activities(self):

        update_time = str(datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S'))
        trakt_activities = Trakt.TraktAPI().json_response('sync/last_activities')

        if str(self.activites['all_activities']) == self.base_date and tools.getSetting('trakt.auth') != '':
            # Increase the amount of concurrent tasks running during initial and Force Sync processes to speed up task
            tools.showDialog.textviewer(tools.addonName, tools.lang(40133))
            confirmation = tools.showDialog.yesno(tools.addonName, tools.lang(40134))

            if not confirmation:
                return

        if trakt_activities is None:
            tools.log('Unable to connect to Trakt', 'error')
            return
        try:

            if str(self.activites['all_activities']) != self.base_date:
                self.silent = True

            if not self.silent:
                self.progress_dialog = tools.bgProgressDialog()
                self.progress_dialog.create(tools.addonName + 'Sync', 'Seren: Trakt Sync')

            ############################################################################################################
            # CHECK FOR META REFRESH
            ############################################################################################################

            # Meta removal should only run every 12 hours, otherwise we repeatedly dump information every run

            try:
                if self.activites['shows_meta_update'] == self.base_date:
                    self._update_activity_record('shows_meta_update', update_time)
                else:
                    local_date = trakt_sync._parse_local_date_format(self.activites['shows_meta_update'])
                    local_date = local_date + timedelta(hours=2)
                    now = trakt_sync._utc_now_as_trakt_string()
                    local_date = trakt_sync._strf_local_date(local_date)
                    if trakt_sync._requires_update(now, local_date):
                        self._remove_old_meta_items('shows')
                        self._update_activity_record('shows_meta_update', update_time)
            except:
                import traceback
                traceback.print_exc()
                pass

            try:
                if self.activites['movies_meta_update'] == self.base_date:
                    self._update_activity_record('movies_meta_update', update_time)
                else:
                    local_date = trakt_sync._parse_local_date_format(self.activites['movies_meta_update'])
                    local_date = local_date + timedelta(hours=2)
                    now = trakt_sync._utc_now_as_trakt_string()
                    local_date = trakt_sync._strf_local_date(local_date)
                    if trakt_sync._requires_update(now, local_date):
                        self._remove_old_meta_items('movies')
                        self._update_activity_record('movies_meta_update', update_time)
            except:
                import traceback
                traceback.print_exc()
                pass

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

            if trakt_sync._requires_update(trakt_activities['all'], self.activites['all_activities']):


                ########################################################################################################
                # SYNC HIDDEN ITEMS
                ########################################################################################################
                try:
                    if not self.silent:
                        self.progress_dialog.update(0, 'Syncing Hidden Items')
                    if trakt_sync._requires_update(trakt_activities['movies']['hidden_at'],
                                                   self.activites['hidden_sync']) and \
                            trakt_sync._requires_update(trakt_activities['shows']['hidden_at'],
                                                        self.activites['hidden_sync']):
                        self._sync_hidden()

                except:
                    import traceback
                    traceback.print_exc()
                    pass
                ########################################################################################################
                # SYNC WATCHED ITEMS
                ########################################################################################################
                sync_triggered = False

                try:
                    if trakt_sync._requires_update(trakt_activities['episodes']['watched_at'],
                                                   self.activites['shows_watched']):
                        sync_triggered = True
                        self._sync_watched_episodes()
                except:
                    import traceback
                    traceback.print_exc()
                    pass
                try:
                    if trakt_sync._requires_update(trakt_activities['movies']['watched_at'],
                                                   self.activites['movies_watched']):
                        sync_triggered = True
                        self._sync_watched_movies()
                except:
                    import traceback
                    traceback.print_exc()
                    pass
                try:
                    if sync_triggered:
                        if not self.silent:
                            self.progress_dialog.update(100, 'Syncing Unwatched items')
                        self._sync_unwatched()
                except:
                    import traceback
                    traceback.print_exc()
                    pass

                ########################################################################################################
                # SYNC COLLECTION
                ########################################################################################################

                sync_triggered = False

                try:
                    if trakt_sync._requires_update(trakt_activities['episodes']['collected_at'],
                                                   self.activites['shows_collected']):
                        self._sync_collection_shows()
                        sync_triggered = True
                except:
                    pass

                try:
                    if trakt_sync._requires_update(trakt_activities['movies']['collected_at'],
                                                   self.activites['movies_collected']):
                        self._sync_collection_movies()
                        sync_triggered = True
                except:
                    pass

                try:
                    if sync_triggered:
                        if not self.silent:
                            self.progress_dialog.update(100, 'Syncing Uncollected items')
                        self._sync_uncollected()
                except:
                    pass

                self._update_activity_record('all_activities', update_time)

            if self.progress_dialog is not None:
                self.progress_dialog.close()
                self.progress_dialog = None
        except:
            try:
                if self.progress_dialog is not None:
                    self.progress_dialog.close()
                    self.progress_dialog = None
            except:
                pass
            import traceback
            traceback.print_exc()
            pass