def getListItems(self, arguments, page): lists_database = lists.TraktSyncDatabase() paginate_lists = ( tools.getSetting('general.paginatetraktlists') == 'true') arguments = json.loads(tools.unquote(arguments)) media_type = arguments['type'] list_items = ast.literal_eval( lists_database.get_list(arguments['trakt_id'], media_type, arguments['username'])['kodi_meta']) max_items = len(list_items) if paginate_lists: list_items = tools.paginate_list( list_items, int(page), int(tools.getSetting('item.limit'))) if media_type in ['show', 'shows']: from resources.lib.gui import tvshowMenus tvshowMenus.Menus().showListBuilder(list_items) if media_type in ['movie', 'movies']: from resources.lib.gui import movieMenus movieMenus.Menus().commonListBuilder(list_items) content_type = 'tvshows' if media_type in ['movie', 'movies']: content_type = 'movies' if paginate_lists: limit = int(tools.getSetting('item.limit')) if int(page) * limit < max_items: tools.addDirectoryItem( tools.lang(32019), 'traktList&page=%s&actionArgs=%s' % (int(page) + 1, tools.quote(json.dumps(arguments)))) tools.closeDirectory(content_type) return
def myTraktLists(self, media_type): lists_database = lists.TraktSyncDatabase() for user_list in lists_database.get_lists(self._remove_pluralization(media_type), 'myLists'): arguments = {'trakt_id': user_list['trakt_id'], 'username': user_list['username'], 'type': user_list['media_type'], 'sort_how': user_list['sort_how'], 'sort_by': user_list['sort_by']} tools.addDirectoryItem('{} - [COLOR {}]{}[/COLOR]'.format(user_list['name'].encode('utf-8'), tools.get_user_text_color(), user_list['username'].encode('utf-8')), 'traktList&page=1&actionArgs=%s' % tools.quote(json.dumps(arguments))) tools.closeDirectory('addons')
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)
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