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
def clear_assist_torrents(): try: tools.activeTorrentsDBFile_lock.acquire() cursor = _get_connection_cursor(tools.activeTorrentsDBFile) for t in [cache_table, 'rel_list', 'rel_lib']: try: cursor.execute("DROP TABLE IF EXISTS torrents") cursor.execute("VACUUM") cursor.connection.commit() cursor.close() except: pass except: try: cursor.close() except: pass import traceback traceback.print_exc() finally: tools.try_release_lock(tools.activeTorrentsDBFile_lock) tools.showDialog.notification('{}: {}'.format(tools.addonName, tools.lang(40306)), tools.lang(32080), time=5000)
def clear_providers(): try: tools.providersDB_lock.acquire() cursor = _get_connection_cursor(tools.providersDB) for t in [cache_table, 'rel_list', 'rel_lib']: try: cursor.execute("DROP TABLE IF EXISTS providers") cursor.execute("VACCUM") except: pass try: cursor.execute("DROP TABLE IF EXISTS packages") cursor.execute("VACUUM") except: pass cursor.connection.commit() cursor.close() except: try: cursor.close() except: pass import traceback traceback.print_exc() finally: tools.try_release_lock(tools.providersDB_lock)
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']
def torrent_cache_clear(): confirmation = tools.showDialog.yesno(tools.addonName, tools.lang(32043)) if not confirmation: return try: tools.torrentScrapeCacheFile_lock.acquire() cursor = _get_connection_cursor(tools.torrentScrapeCacheFile) for t in [cache_table, 'rel_list', 'rel_lib']: try: cursor.execute("DROP TABLE IF EXISTS %s" % t) cursor.execute("VACUUM") cursor.connection.commit() except: pass except: try: cursor.close() except: pass import traceback traceback.print_exc() finally: tools.try_release_lock(tools.torrentScrapeCacheFile_lock) tools.showDialog.notification('{}: {}'.format(tools.addonName, tools.lang(40306)), tools.lang(32079), time=5000)
def addSearchHistory(search_string, media_type): try: tools.searchHistoryDB_lock.acquire() cursor = _get_connection_cursor(tools.searchHistoryDB) cursor.execute('CREATE TABLE IF NOT EXISTS show (value TEXT)') cursor.execute('CREATE TABLE IF NOT EXISTS movie (value TEXT)') cursor.execute( "CREATE UNIQUE INDEX IF NOT EXISTS ix_history ON movie (value)") cursor.execute( "CREATE UNIQUE INDEX IF NOT EXISTS ix_history ON show (value)") cursor.execute("REPLACE INTO %s Values (?)" % media_type, (search_string, )) cursor.connection.commit() cursor.close() except: try: cursor.close() except: pass import traceback traceback.print_exc() return [] finally: tools.try_release_lock(tools.searchHistoryDB_lock)
def clearSearchHistory(): try: tools.searchHistoryDB_lock.acquire() confirmation = tools.showDialog.yesno(tools.addonName, tools.lang(40169)) if not confirmation: return cursor = _get_connection_cursor(tools.searchHistoryDB) cursor.execute("DROP TABLE IF EXISTS movie") cursor.execute("DROP TABLE IF EXISTS show") try: cursor.execute("VACCUM") except: pass cursor.connection.commit() cursor.close() tools.showDialog.notification(tools.addonName, tools.lang(40259), time=5000) except: try: cursor.close() except: pass import traceback traceback.print_exc() return [] finally: tools.try_release_lock(tools.searchHistoryDB_lock)
def cache_insert(key, value): tools.cacheFile_lock.acquire() try: cursor = _get_connection_cursor(tools.cacheFile) now = int(time.time()) cursor.execute( "CREATE TABLE IF NOT EXISTS %s (key TEXT, value TEXT, date INTEGER, UNIQUE(key))" % cache_table) cursor.execute("CREATE UNIQUE INDEX IF NOT EXISTS ix_%s ON %s (key)" % (cache_table, cache_table)) cursor.execute( "REPLACE INTO %s (key, value, date) VALUES (?, ?, ?)" % cache_table, (key, value, now)) cursor.connection.commit() cursor.close() except: try: cursor.close() except: pass import traceback traceback.print_exc() pass finally: tools.try_release_lock(tools.cacheFile_lock)
def getSearchHistory(media_type): try: tools.searchHistoryDB_lock.acquire() cursor = _get_connection_cursor(tools.searchHistoryDB) cursor.execute('CREATE TABLE IF NOT EXISTS show (value TEXT)') cursor.execute('CREATE TABLE IF NOT EXISTS movie (value TEXT)') cursor.execute( "CREATE UNIQUE INDEX IF NOT EXISTS ix_history ON movie (value)") cursor.execute( "CREATE UNIQUE INDEX IF NOT EXISTS ix_history ON show (value)") cursor.execute("SELECT * FROM %s" % media_type) history = cursor.fetchall() cursor.close() history.reverse() history = history[:50] filter = [] for i in history: if i['value'] not in filter: filter.append(i['value']) return filter except: try: cursor.close() except: pass import traceback traceback.print_exc() return [] finally: tools.try_release_lock(tools.searchHistoryDB_lock)
def mark_episode_unwatched_by_id(self, trakt_id): tools.traktSyncDB_lock.acquire() cursor = self._get_cursor() cursor.execute('UPDATE episodes SET watched=0 WHERE trakt_id=?', (trakt_id,)) cursor.connection.commit() cursor.close() tools.try_release_lock(tools.traktSyncDB_lock)
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']
def _sync_insert_episode(self, show_id, episode_id, season, episode, 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) if item is None: episode_object = { 'ids': { 'trakt': episode_id }, 'season': season, 'number': episode } self._update_episode(show_id, episode_object, False, watched, collected) else: if watched is not None: self.mark_episode_watched_by_id(episode_id) if collected is None: self.mark_episode_collected(show_id, season, episode)
def _mark_show_record(self, column, value, show_id): tools.traktSyncDB_lock.acquire() cursor = self._get_cursor() cursor.execute('UPDATE shows SET %s=? WHERE trakt_id=?' % column, (value, show_id)) cursor.connection.commit() cursor.close() tools.try_release_lock(tools.traktSyncDB_lock)
def remove_list(self, trakt_id, media_type): tools.traktSyncDB_lock.acquire() cursor = self._get_cursor() cursor.execute('DELETE FROM lists WHERE trakt_id=? AND media_type =?', (trakt_id, media_type)) cursor.connection.commit() cursor.close() tools.try_release_lock(tools.traktSyncDB_lock)
def _update_activity_record(self, record, time): tools.traktSyncDB_lock.acquire() cursor = self._get_cursor() cursor.execute('UPDATE activities SET %s=? WHERE sync_id=1' % record, (time, )) cursor.connection.commit() cursor.close() tools.try_release_lock(tools.traktSyncDB_lock)
def get_collected_movies(self): tools.traktSyncDB_lock.acquire() cursor = self._get_cursor() cursor.execute('SELECT * FROM movies WHERE collected =1') movies = cursor.fetchall() cursor.close() tools.try_release_lock(tools.traktSyncDB_lock) return movies
def remove_item(self, section, trakt_id): tools.traktSyncDB_lock.acquire() cursor = self._get_cursor() cursor.execute('DELETE FROM hidden WHERE section=? AND trakt_id=?', (str(section), int(trakt_id))) cursor.connection.commit() cursor.close() tools.try_release_lock(tools.traktSyncDB_lock)
def flush_activities(self, clear_meta=False): if clear_meta: self.clear_all_meta() tools.traktSyncDB_lock.acquire() cursor = self._get_cursor() cursor.execute('DROP TABLE activities') cursor.connection.commit() cursor.close() tools.try_release_lock(tools.traktSyncDB_lock)
def set_trakt_user(self, trakt_username): tools.traktSyncDB_lock.acquire() cursor = self._get_cursor() tools.log('Setting Trakt Username: %s' % trakt_username) cursor.execute('UPDATE activities SET trakt_username=?', (trakt_username, )) cursor.connection.commit() cursor.close() tools.try_release_lock(tools.traktSyncDB_lock)
def get_all_movies(self): tools.traktSyncDB_lock.acquire() cursor = self._get_cursor() cursor.execute('SELECT * FROM movies') movies = cursor.fetchall() cursor.close() tools.try_release_lock(tools.traktSyncDB_lock) movies = [i['trakt_id'] for i in movies] return movies
def mark_season_watched(self, show_id, season, watched): tools.traktSyncDB_lock.acquire() cursor = self._get_cursor() cursor.execute('UPDATE episodes SET watched=? WHERE show_id=? AND season=?', (watched, show_id, season)) cursor.connection.commit() cursor.close() tools.try_release_lock(tools.traktSyncDB_lock) if watched: self._update_watched(show_id)
def get_watched_episodes(self): tools.traktSyncDB_lock.acquire() cursor = self._get_cursor() cursor.execute('SELECT * FROM episodes WHERE watched=1') episodes = cursor.fetchall() cursor.close() tools.try_release_lock(tools.traktSyncDB_lock) return episodes
def _mark_episode_record(self, column, value, show_id, season, number): tools.traktSyncDB_lock.acquire() cursor = self._get_cursor() cursor.execute('UPDATE episodes SET %s=? WHERE show_id=? AND season=? AND number=?' % column, (value, show_id, season, number)) cursor.connection.commit() cursor.close() tools.try_release_lock(tools.traktSyncDB_lock)
def get_show_list(self, show_list, watch_info=True): # Ease of use to get full list from DB # We first attempt to pull the entire list in one DB hit to minimise transactions # Failing ability to get all items, we send the required items to be updated and then return if type(show_list[0]) is dict: if 'show' in show_list[0]: show_list = [i['show'] for i in show_list] if 'ids' in show_list[0]: show_list = [i['ids']['trakt'] for i in show_list] show_list = set(show_list) self.item_list = [] tools.traktSyncDB_lock.acquire() cursor = self._get_cursor() db_query = 'SELECT * FROM shows WHERE trakt_id IN (%s)' % ','.join( (str(i) for i in show_list)) cursor.execute(db_query) show_db_list = cursor.fetchall() cursor.close() tools.try_release_lock(tools.traktSyncDB_lock) requires_update = [] for show_id in show_list: db_item = [i for i in show_db_list if i['trakt_id'] == show_id] if len(db_item) == 1: db_item = db_item[0] if db_item['kodi_meta'] == '{}': requires_update.append(show_id) else: requires_update.append(show_id) if len(requires_update) == 0: meta_list = [ ast.literal_eval(i['kodi_meta']) for i in show_db_list if i['kodi_meta'] != '{}' ] meta_list = [self.get_show_watched_info(i) for i in meta_list] return meta_list else: self.update_show_list(requires_update, watch_info) tools.traktSyncDB_lock.acquire() cursor = self._get_cursor() cursor.execute(db_query) show_db_list = cursor.fetchall() cursor.close() tools.try_release_lock(tools.traktSyncDB_lock) meta_list = [ ast.literal_eval(i['kodi_meta']) for i in show_db_list if i['kodi_meta'] != '{}' ] meta_list = [self.get_show_watched_info(i) for i in meta_list] return meta_list
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
def get_lists(self, media_type, list_type): tools.traktSyncDB_lock.acquire() cursor = self._get_cursor() cursor.execute( 'SELECT * FROM lists WHERE list_type=? and media_type=?', (list_type, media_type)) list = cursor.fetchall() cursor.close() tools.try_release_lock(tools.traktSyncDB_lock) return list
def mark_season_collected(self, show_id, season, collected): tools.traktSyncDB_lock.acquire() cursor = self._get_cursor() cursor.execute('UPDATE episodes SET collected=? WHERE show_id=? AND season=?', (collected, show_id, season)) cursor.connection.commit() cursor.close() tools.try_release_lock(tools.traktSyncDB_lock) from activities import TraktSyncDatabase as activities_database sync_thread = threading.Thread(target=activities_database()._sync_collection_shows()) sync_thread.run()
def _get_show_episodes(self, show_id, meta=False): 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) if meta: episodes = [episode['kodi_meta'] for episode in episodes] return episodes
def _update_show(self, trakt_id, show_item, get_meta=True): tools.traktSyncDB_lock.acquire() cursor = self._get_cursor() cursor.execute('SELECT * FROM shows WHERE trakt_id=?', (trakt_id, )) old_entry = cursor.fetchone() cursor.close() tools.try_release_lock(tools.traktSyncDB_lock) if get_meta: try: kodi_meta = tvdb.TVDBAPI().seriesIDToListItem(show_item) if kodi_meta is None or kodi_meta == '{}': kodi_meta = tmdb.TMDBAPI().showToListItem(show_item) if kodi_meta is None or kodi_meta == '{}': kodi_meta = imdb.IMDBScraper().showToListItem(show_item) if kodi_meta is None or kodi_meta == '{}': return update_time = str( datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S')) except: return None else: if old_entry is None: kodi_meta = {} update_time = self.base_date else: update_time = old_entry['last_updated'] kodi_meta = old_entry['kodi_meta'] tools.traktSyncDB_lock.acquire() cursor = self._get_cursor() try: cursor.execute('PRAGMA foreign_keys=OFF') cursor.execute( "REPLACE INTO shows (" "trakt_id, kodi_meta, last_updated, air_date)" "VALUES " "(?, ?, ?, ?)", (int(trakt_id), str(kodi_meta), update_time, kodi_meta['info']['premiered'])) cursor.execute('PRAGMA foreign_keys=ON') cursor.connection.commit() cursor.close() return { 'trakt_id': trakt_id, 'kodi_meta': kodi_meta, 'last_updated': update_time } except: cursor.close() import traceback traceback.print_exc() pass finally: tools.try_release_lock(tools.traktSyncDB_lock)
def addTorrent(item_meta, torrent_objects): try: if tools.getSetting('general.torrentCache') == 'false': return if 'showInfo' in item_meta: season = item_meta['info']['season'] episode = item_meta['info']['episode'] trakt_id = item_meta['showInfo']['ids']['trakt'] else: season = 0 episode = 0 trakt_id = item_meta['ids']['trakt'] tools.torrentScrapeCacheFile_lock.acquire() cursor = _get_connection_cursor(tools.torrentScrapeCacheFile) try: # Confirm we are on the newer version of the torrent cache database columns = [ i['name'] for i in cursor.execute( "PRAGMA table_info(cache);").fetchall() ] if 'trakt_id' not in columns: raise Exception except: cursor.execute("DROP TABLE IF EXISTS cache") _try_create_torrent_cache(cursor) for torrent_object in torrent_objects: try: hash = torrent_object['hash'] pack = torrent_object['package'] cursor.execute( "REPLACE INTO %s (trakt_id, meta, hash, season, episode, package) " "VALUES (?, ?, ?, ?, ?, ?)" % cache_table, (trakt_id, str(torrent_object), hash, season, episode, pack)) except: import traceback traceback.print_exc() cursor.connection.commit() cursor.close() except: try: cursor.close() except: pass import traceback traceback.print_exc() return [] finally: tools.try_release_lock(tools.torrentScrapeCacheFile_lock)