def get_profile_list(): from get_settings import get_sonarr_settings url_sonarr = get_sonarr_settings()[6] # url_sonarr_short = get_sonarr_settings()[5] apikey_sonarr = get_sonarr_settings()[4] # Get profiles data from Sonarr error = False url_sonarr_api_series = url_sonarr + "/api/profile?apikey=" + apikey_sonarr try: profiles_json = requests.get(url_sonarr_api_series, timeout=15, verify=False) except requests.exceptions.ConnectionError as errc: error = True logging.exception( "Error trying to get profiles from Sonarr. Connection Error.") except requests.exceptions.Timeout as errt: error = True logging.exception( "Error trying to get profiles from Sonarr. Timeout Error.") except requests.exceptions.RequestException as err: error = True logging.exception("Error trying to get profiles from Sonarr.") url_sonarr_api_series_v3 = url_sonarr + "/api/v3/languageprofile?apikey=" + apikey_sonarr try: profiles_json_v3 = requests.get(url_sonarr_api_series_v3, timeout=15, verify=False) except requests.exceptions.ConnectionError as errc: error = True logging.exception( "Error trying to get profiles from Sonarr. Connection Error.") except requests.exceptions.Timeout as errt: error = True logging.exception( "Error trying to get profiles from Sonarr. Timeout Error.") except requests.exceptions.RequestException as err: error = True logging.exception("Error trying to get profiles from Sonarr.") global profiles_list profiles_list = [] if error is False: # Parsing data returned from Sonarr global sonarr_version if type(profiles_json_v3.json()) != list: sonarr_version = 2 for profile in profiles_json.json(): profiles_list.append( [profile['id'], profile['language'].capitalize()]) else: sonarr_version = 3 for profile in profiles_json_v3.json(): profiles_list.append( [profile['id'], profile['name'].capitalize()])
def sonarr_full_update(): if integration[12] is True: full_update = get_sonarr_settings()[5] if full_update == "Daily": scheduler.add_job(update_all_episodes, CronTrigger(hour=4), max_instances=1, coalesce=True, misfire_grace_time=15, id='update_all_episodes', name='Update all episodes subtitles from disk', replace_existing=True) elif full_update == "Weekly": scheduler.add_job(update_all_episodes, CronTrigger(day_of_week='sun'), hour=4, max_instances=1, coalesce=True, misfire_grace_time=15, id='update_all_episodes', name='Update all episodes subtitles from disk', replace_existing=True) elif full_update == "Manually": scheduler.add_job(update_all_episodes, CronTrigger(year='2100'), hour=4, max_instances=1, coalesce=True, misfire_grace_time=15, id='update_all_episodes', name='Update all episodes subtitles from disk', replace_existing=True)
def sync_episodes(): logging.debug('Starting episode sync from Sonarr.') from get_settings import get_sonarr_settings url_sonarr = get_sonarr_settings()[6] apikey_sonarr = get_sonarr_settings()[4] # Open database connection db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) c = db.cursor() # Get current episodes id in DB current_episodes_db = c.execute( 'SELECT sonarrEpisodeId FROM table_episodes').fetchall() current_episodes_db_list = [x[0] for x in current_episodes_db] current_episodes_sonarr = [] episodes_to_update = [] episodes_to_add = [] # Get sonarrId for each series from database seriesIdList = c.execute( "SELECT sonarrSeriesId FROM table_shows").fetchall() # Close database connection c.close() for seriesId in seriesIdList: # Get episodes data for a series from Sonarr url_sonarr_api_episode = url_sonarr + "/api/episode?seriesId=" + str( seriesId[0]) + "&apikey=" + apikey_sonarr try: r = requests.get(url_sonarr_api_episode, timeout=15, verify=False) r.raise_for_status() except requests.exceptions.HTTPError as errh: logging.exception( "Error trying to get episodes from Sonarr. Http error.") except requests.exceptions.ConnectionError as errc: logging.exception( "Error trying to get episodes from Sonarr. Connection Error.") except requests.exceptions.Timeout as errt: logging.exception( "Error trying to get episodes from Sonarr. Timeout Error.") except requests.exceptions.RequestException as err: logging.exception("Error trying to get episodes from Sonarr.") else: for episode in r.json(): if 'hasFile' in episode: if episode['hasFile'] is True: if 'episodeFile' in episode: if episode['episodeFile']['size'] > 20480: # Add shows in Sonarr to current shows list if 'sceneName' in episode['episodeFile']: sceneName = episode['episodeFile'][ 'sceneName'] else: sceneName = None # Add episodes in sonarr to current episode list current_episodes_sonarr.append(episode['id']) if episode['id'] in current_episodes_db_list: episodes_to_update.append( (episode['title'], episode['episodeFile']['path'], episode['seasonNumber'], episode['episodeNumber'], sceneName, str(bool(episode['monitored'])), episode['id'])) else: episodes_to_add.append( (episode['seriesId'], episode['id'], episode['title'], episode['episodeFile']['path'], episode['seasonNumber'], episode['episodeNumber'], sceneName, str(bool(episode['monitored'])))) removed_episodes = list( set(current_episodes_db_list) - set(current_episodes_sonarr)) # Update or insert movies in DB db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) c = db.cursor() updated_result = c.executemany( '''UPDATE table_episodes SET title = ?, path = ?, season = ?, episode = ?, scene_name = ?, monitored = ? WHERE sonarrEpisodeId = ?''', episodes_to_update) db.commit() added_result = c.executemany( '''INSERT OR IGNORE INTO table_episodes(sonarrSeriesId, sonarrEpisodeId, title, path, season, episode, scene_name, monitored) VALUES (?, ?, ?, ?, ?, ?, ?, ?)''', episodes_to_add) db.commit() for removed_episode in removed_episodes: c.execute('DELETE FROM table_episodes WHERE sonarrEpisodeId = ?', (removed_episode, )) db.commit() # Close database connection c.close() for added_episode in episodes_to_add: store_subtitles(path_replace(added_episode[3])) logging.debug('All episodes synced from Sonarr into database.') list_missing_subtitles() logging.debug('All missing subtitles updated in database.')
def update_series(): from get_settings import get_sonarr_settings url_sonarr = get_sonarr_settings()[6] apikey_sonarr = get_sonarr_settings()[4] serie_default_enabled = get_general_settings()[15] serie_default_language = get_general_settings()[16] serie_default_hi = get_general_settings()[17] if apikey_sonarr == None: pass else: get_profile_list() # Get shows data from Sonarr url_sonarr_api_series = url_sonarr + "/api/series?apikey=" + apikey_sonarr try: r = requests.get(url_sonarr_api_series, timeout=15, verify=False) r.raise_for_status() except requests.exceptions.HTTPError as errh: logging.exception( "Error trying to get series from Sonarr. Http error.") except requests.exceptions.ConnectionError as errc: logging.exception( "Error trying to get series from Sonarr. Connection Error.") except requests.exceptions.Timeout as errt: logging.exception( "Error trying to get series from Sonarr. Timeout Error.") except requests.exceptions.RequestException as err: logging.exception("Error trying to get series from Sonarr.") else: # Open database connection db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) c = db.cursor() # Get current shows in DB current_shows_db = c.execute( 'SELECT tvdbId FROM table_shows').fetchall() # Close database connection db.close() current_shows_db_list = [x[0] for x in current_shows_db] current_shows_sonarr = [] series_to_update = [] series_to_add = [] for show in r.json(): try: overview = unicode(show['overview']) except: overview = "" try: poster_big = show['images'][2]['url'].split('?')[0] poster = os.path.splitext(poster_big)[ 0] + '-250' + os.path.splitext(poster_big)[1] except: poster = "" try: fanart = show['images'][0]['url'].split('?')[0] except: fanart = "" # Add shows in Sonarr to current shows list current_shows_sonarr.append(show['tvdbId']) if show['tvdbId'] in current_shows_db_list: series_to_update.append( (show["title"], show["path"], show["tvdbId"], show["id"], overview, poster, fanart, profile_id_to_language( (show['qualityProfileId'] if sonarr_version == 2 else show['languageProfileId'])), show['sortTitle'], show["tvdbId"])) else: if serie_default_enabled is True: series_to_add.append( (show["title"], show["path"], show["tvdbId"], serie_default_language, serie_default_hi, show["id"], overview, poster, fanart, profile_id_to_language(show['qualityProfileId']), show['sortTitle'])) else: series_to_add.append( (show["title"], show["path"], show["tvdbId"], show["tvdbId"], show["tvdbId"], show["id"], overview, poster, fanart, profile_id_to_language(show['qualityProfileId']), show['sortTitle'])) # Update or insert series in DB db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) c = db.cursor() updated_result = c.executemany( '''UPDATE table_shows SET title = ?, path = ?, tvdbId = ?, sonarrSeriesId = ?, overview = ?, poster = ?, fanart = ?, `audio_language` = ? , sortTitle = ? WHERE tvdbid = ?''', series_to_update) db.commit() if serie_default_enabled is True: added_result = c.executemany( '''INSERT OR IGNORE INTO table_shows(title, path, tvdbId, languages,`hearing_impaired`, sonarrSeriesId, overview, poster, fanart, `audio_language`, sortTitle) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''', series_to_add) db.commit() else: added_result = c.executemany( '''INSERT OR IGNORE INTO table_shows(title, path, tvdbId, languages,`hearing_impaired`, sonarrSeriesId, overview, poster, fanart, `audio_language`, sortTitle) VALUES (?,?,?,(SELECT languages FROM table_shows WHERE tvdbId = ?),(SELECT `hearing_impaired` FROM table_shows WHERE tvdbId = ?), ?, ?, ?, ?, ?, ?)''', series_to_add) db.commit() db.close() for show in series_to_add: list_missing_subtitles(show[5]) # Delete shows not in Sonarr anymore deleted_items = [] for item in current_shows_db_list: if item not in current_shows_sonarr: deleted_items.append(tuple([item])) db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) c = db.cursor() c.executemany('DELETE FROM table_shows WHERE tvdbId = ?', deleted_items) db.commit() db.close()