From 113709073581563dfda0761b3573caf818b9329a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20V=C3=A9zina?= <5130500+morpheus65535@users.noreply.github.com> Date: Thu, 4 Oct 2018 14:16:49 -0400 Subject: [PATCH] Moving everything else bazarr.py to a bazarr subdirectory. --- ___init__.py | 1 + bazarr.py | 56 +- check_update.py => bazarr/check_update.py | 0 create_db.sql => bazarr/create_db.sql | 0 get_argv.py => bazarr/get_argv.py | 2 +- get_episodes.py => bazarr/get_episodes.py | 210 +++---- get_languages.py => bazarr/get_languages.py | 182 +++--- get_movies.py => bazarr/get_movies.py | 0 get_providers.py => bazarr/get_providers.py | 0 get_series.py => bazarr/get_series.py | 322 +++++------ get_settings.py => bazarr/get_settings.py | 0 get_subtitle.py => bazarr/get_subtitle.py | 0 init.py => bazarr/init.py | 0 list_subtitles.py => bazarr/list_subtitles.py | 526 +++++++++--------- main.py => bazarr/main.py | 8 +- notifier.py => bazarr/notifier.py | 0 scheduler.py => bazarr/scheduler.py | 0 update_db.py => bazarr/update_db.py | 0 utils.py => bazarr/utils.py | 66 +-- 19 files changed, 713 insertions(+), 660 deletions(-) create mode 100644 ___init__.py rename check_update.py => bazarr/check_update.py (100%) rename create_db.sql => bazarr/create_db.sql (100%) rename get_argv.py => bazarr/get_argv.py (83%) rename get_episodes.py => bazarr/get_episodes.py (96%) rename get_languages.py => bazarr/get_languages.py (96%) rename get_movies.py => bazarr/get_movies.py (100%) rename get_providers.py => bazarr/get_providers.py (100%) rename get_series.py => bazarr/get_series.py (97%) rename get_settings.py => bazarr/get_settings.py (100%) rename get_subtitle.py => bazarr/get_subtitle.py (100%) rename init.py => bazarr/init.py (100%) rename list_subtitles.py => bazarr/list_subtitles.py (97%) rename main.py => bazarr/main.py (99%) rename notifier.py => bazarr/notifier.py (100%) rename scheduler.py => bazarr/scheduler.py (100%) rename update_db.py => bazarr/update_db.py (100%) rename utils.py => bazarr/utils.py (97%) diff --git a/___init__.py b/___init__.py new file mode 100644 index 000000000..cd4683012 --- /dev/null +++ b/___init__.py @@ -0,0 +1 @@ +from bazarr import * \ No newline at end of file diff --git a/bazarr.py b/bazarr.py index eff49c7c6..9e22a2ef9 100644 --- a/bazarr.py +++ b/bazarr.py @@ -3,6 +3,7 @@ import threading import time import os import signal +import platform import logging import sys import getopt @@ -17,7 +18,7 @@ arguments = [] try: opts, args = getopt.getopt(sys.argv[1:],"h:",["no-update", "config="]) except getopt.GetoptError: - print 'daemon.py -h --no-update --config ' + print 'bazarr.py -h --no-update --config ' sys.exit(2) for opt, arg in opts: arguments.append(opt) @@ -26,7 +27,7 @@ for opt, arg in opts: def start_bazarr(): - script = ['python','main.py'] + globals()['arguments'] + script = ['python','bazarr/main.py'] + globals()['arguments'] pidfile = os.path.normcase(os.path.join(os.path.dirname(__file__), 'bazarr.pid')) if os.path.exists(pidfile): @@ -78,6 +79,44 @@ def restart_bazarr(): file.close() +# GetExitCodeProcess uses a special exit code to indicate that the process is +# still running. +_STILL_ACTIVE = 259 + + +def is_pid_running(pid): + return (_is_pid_running_on_windows(pid) if platform.system() == "Windows" + else _is_pid_running_on_unix(pid)) + + +def _is_pid_running_on_unix(pid): + try: + os.kill(pid, 0) + except OSError: + return False + return True + + +def _is_pid_running_on_windows(pid): + import ctypes.wintypes + + kernel32 = ctypes.windll.kernel32 + handle = kernel32.OpenProcess(1, 0, pid) + if handle == 0: + return False + + # If the process exited recently, a pid may still exist for the handle. + # So, check if we can get the exit code. + exit_code = ctypes.wintypes.DWORD() + is_running = ( + kernel32.GetExitCodeProcess(handle, ctypes.byref(exit_code)) == 0) + kernel32.CloseHandle(handle) + + # See if we couldn't get the exit code or the exit code indicates that the + # process is still running. + return is_running or exit_code.value == _STILL_ACTIVE + + if __name__ == '__main__': pidfile = os.path.normcase(os.path.join(os.path.dirname(__file__), 'bazarr.pid')) restartfile = os.path.normcase(os.path.join(os.path.dirname(__file__), 'bazarr.restart')) @@ -110,6 +149,19 @@ if __name__ == '__main__': shutdown_bazarr(True) start_bazarr() + if os.path.exists(pidfile): + try: + file = open(pidfile, 'r') + except: + logging.error("Error trying to read pid file.") + else: + pid = int(file.read()) + file.close() + + if is_pid_running(pid) is False: + logging.warn('Bazarr unexpectedly exited. Starting it back.') + start_bazarr() + daemon() diff --git a/check_update.py b/bazarr/check_update.py similarity index 100% rename from check_update.py rename to bazarr/check_update.py diff --git a/create_db.sql b/bazarr/create_db.sql similarity index 100% rename from create_db.sql rename to bazarr/create_db.sql diff --git a/get_argv.py b/bazarr/get_argv.py similarity index 83% rename from get_argv.py rename to bazarr/get_argv.py index b844db207..5a2b27587 100644 --- a/get_argv.py +++ b/bazarr/get_argv.py @@ -2,7 +2,7 @@ import os import sys import getopt -config_dir = os.path.join(os.path.dirname(__file__), 'data/') +config_dir = os.path.join(os.path.dirname(__file__), '../data/') no_update = False try: diff --git a/get_episodes.py b/bazarr/get_episodes.py similarity index 96% rename from get_episodes.py rename to bazarr/get_episodes.py index c0a1f12e6..03477c039 100644 --- a/get_episodes.py +++ b/bazarr/get_episodes.py @@ -1,106 +1,106 @@ -from get_argv import config_dir - -import os -import sqlite3 -import requests -import logging - -from get_settings import path_replace -from list_subtitles import list_missing_subtitles, store_subtitles, series_full_scan_subtitles, movies_full_scan_subtitles - -def update_all_episodes(): - series_full_scan_subtitles() - logging.info('All existing episode subtitles indexed from disk.') - list_missing_subtitles() - logging.info('All missing episode subtitles updated in database.') - -def update_all_movies(): - movies_full_scan_subtitles() - logging.info('All existing movie subtitles indexed from disk.') - list_missing_subtitles() - logging.info('All missing movie subtitles updated in database.') - -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() +from get_argv import config_dir + +import os +import sqlite3 +import requests +import logging + +from get_settings import path_replace +from list_subtitles import list_missing_subtitles, store_subtitles, series_full_scan_subtitles, movies_full_scan_subtitles + +def update_all_episodes(): + series_full_scan_subtitles() + logging.info('All existing episode subtitles indexed from disk.') + list_missing_subtitles() + logging.info('All missing episode subtitles updated in database.') + +def update_all_movies(): + movies_full_scan_subtitles() + logging.info('All existing movie subtitles indexed from disk.') + list_missing_subtitles() + logging.info('All missing movie subtitles updated in database.') + +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.') \ No newline at end of file diff --git a/get_languages.py b/bazarr/get_languages.py similarity index 96% rename from get_languages.py rename to bazarr/get_languages.py index 74c95e1d8..a9f1c0cad 100644 --- a/get_languages.py +++ b/bazarr/get_languages.py @@ -1,92 +1,92 @@ -from get_argv import config_dir - -import sqlite3 -import pycountry -import os - -def load_language_in_db(): - # Get languages list in langs tuple - langs = [[lang.alpha_3,lang.alpha_2,lang.name] - for lang in pycountry.languages - if hasattr(lang, 'alpha_2')] - - # Open database connection - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) - c = db.cursor() - - # Insert languages in database table - c.executemany('''INSERT OR IGNORE INTO table_settings_languages(code3, code2, name) VALUES(?, ?, ?)''', langs) - c.execute('''INSERT OR IGNORE INTO table_settings_languages(code3, code2, name) VALUES(?, ?, ?)''', ('pob','pb','Brazilian Portuguese')) - - # Commit changes to database table - db.commit() - - # Close database connection - db.close() - -def language_from_alpha2(lang): - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) - c = db.cursor() - try: - result = c.execute('''SELECT name FROM table_settings_languages WHERE code2 = ?''', (lang,)).fetchone()[0] - except: - result = None - db.close - return result - -def language_from_alpha3(lang): - if lang == 'fre': - lang = 'fra' - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) - c = db.cursor() - try: - result = c.execute('''SELECT name FROM table_settings_languages WHERE code3 = ?''', (lang,)).fetchone()[0] - except: - result = None - db.close - return result - -def alpha2_from_alpha3(lang): - if lang == 'fre': - lang = 'fra' - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) - c = db.cursor() - try: - result = c.execute('''SELECT code2 FROM table_settings_languages WHERE code3 = ?''', (lang,)).fetchone()[0] - except: - result = None - db.close - return result - -def alpha2_from_language(lang): - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) - c = db.cursor() - try: - result = c.execute('''SELECT code2 FROM table_settings_languages WHERE name = ?''', (lang,)).fetchone()[0] - except: - result = None - db.close - return result - -def alpha3_from_alpha2(lang): - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) - c = db.cursor() - try: - result = c.execute('''SELECT code3 FROM table_settings_languages WHERE code2 = ?''', (lang,)).fetchone()[0] - except: - result = None - db.close - return result - -def alpha3_from_language(lang): - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) - c = db.cursor() - try: - result = c.execute('''SELECT code3 FROM table_settings_languages WHERE name = ?''', (lang,)).fetchone()[0] - except: - result = None - db.close - return result - -if __name__ == '__main__': +from get_argv import config_dir + +import sqlite3 +import pycountry +import os + +def load_language_in_db(): + # Get languages list in langs tuple + langs = [[lang.alpha_3,lang.alpha_2,lang.name] + for lang in pycountry.languages + if hasattr(lang, 'alpha_2')] + + # Open database connection + db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + c = db.cursor() + + # Insert languages in database table + c.executemany('''INSERT OR IGNORE INTO table_settings_languages(code3, code2, name) VALUES(?, ?, ?)''', langs) + c.execute('''INSERT OR IGNORE INTO table_settings_languages(code3, code2, name) VALUES(?, ?, ?)''', ('pob','pb','Brazilian Portuguese')) + + # Commit changes to database table + db.commit() + + # Close database connection + db.close() + +def language_from_alpha2(lang): + db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + c = db.cursor() + try: + result = c.execute('''SELECT name FROM table_settings_languages WHERE code2 = ?''', (lang,)).fetchone()[0] + except: + result = None + db.close + return result + +def language_from_alpha3(lang): + if lang == 'fre': + lang = 'fra' + db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + c = db.cursor() + try: + result = c.execute('''SELECT name FROM table_settings_languages WHERE code3 = ?''', (lang,)).fetchone()[0] + except: + result = None + db.close + return result + +def alpha2_from_alpha3(lang): + if lang == 'fre': + lang = 'fra' + db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + c = db.cursor() + try: + result = c.execute('''SELECT code2 FROM table_settings_languages WHERE code3 = ?''', (lang,)).fetchone()[0] + except: + result = None + db.close + return result + +def alpha2_from_language(lang): + db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + c = db.cursor() + try: + result = c.execute('''SELECT code2 FROM table_settings_languages WHERE name = ?''', (lang,)).fetchone()[0] + except: + result = None + db.close + return result + +def alpha3_from_alpha2(lang): + db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + c = db.cursor() + try: + result = c.execute('''SELECT code3 FROM table_settings_languages WHERE code2 = ?''', (lang,)).fetchone()[0] + except: + result = None + db.close + return result + +def alpha3_from_language(lang): + db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + c = db.cursor() + try: + result = c.execute('''SELECT code3 FROM table_settings_languages WHERE name = ?''', (lang,)).fetchone()[0] + except: + result = None + db.close + return result + +if __name__ == '__main__': load_language_in_db() \ No newline at end of file diff --git a/get_movies.py b/bazarr/get_movies.py similarity index 100% rename from get_movies.py rename to bazarr/get_movies.py diff --git a/get_providers.py b/bazarr/get_providers.py similarity index 100% rename from get_providers.py rename to bazarr/get_providers.py diff --git a/get_series.py b/bazarr/get_series.py similarity index 97% rename from get_series.py rename to bazarr/get_series.py index 616b5e183..0b9490ff4 100644 --- a/get_series.py +++ b/bazarr/get_series.py @@ -1,161 +1,161 @@ -from get_argv import config_dir - -import os -import sqlite3 -import requests -import logging - -from get_settings import get_general_settings -from list_subtitles import list_missing_subtitles - -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() - -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 profile_id_to_language(id): - for profile in profiles_list: - if id == profile[0]: - return profile[1] +from get_argv import config_dir + +import os +import sqlite3 +import requests +import logging + +from get_settings import get_general_settings +from list_subtitles import list_missing_subtitles + +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() + +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 profile_id_to_language(id): + for profile in profiles_list: + if id == profile[0]: + return profile[1] diff --git a/get_settings.py b/bazarr/get_settings.py similarity index 100% rename from get_settings.py rename to bazarr/get_settings.py diff --git a/get_subtitle.py b/bazarr/get_subtitle.py similarity index 100% rename from get_subtitle.py rename to bazarr/get_subtitle.py diff --git a/init.py b/bazarr/init.py similarity index 100% rename from init.py rename to bazarr/init.py diff --git a/list_subtitles.py b/bazarr/list_subtitles.py similarity index 97% rename from list_subtitles.py rename to bazarr/list_subtitles.py index 8d809fb82..566d15c94 100644 --- a/list_subtitles.py +++ b/bazarr/list_subtitles.py @@ -1,263 +1,263 @@ -from get_argv import config_dir - -import gc -import os -import enzyme -import babelfish -import logging -from subliminal import core -import sqlite3 -import ast -import langdetect -from bs4 import UnicodeDammit -from itertools import islice - -from get_settings import path_replace_reverse, path_replace, path_replace_reverse_movie, path_replace_movie, get_general_settings -from get_languages import alpha2_from_alpha3 - -gc.enable() - -def store_subtitles(file): - # languages = [] - actual_subtitles = [] - if os.path.exists(file): - if os.path.splitext(file)[1] == '.mkv': - try: - with open(file, 'rb') as f: - mkv = enzyme.MKV(f) - - for subtitle_track in mkv.subtitle_tracks: - try: - if alpha2_from_alpha3(subtitle_track.language) != None: - actual_subtitles.append([str(alpha2_from_alpha3(subtitle_track.language)),None]) - except: - pass - except: - pass - - - brazilian_portuguese = [".pt-br", ".pob", "pb"] - try: - subtitles = core.search_external_subtitles(file) - except: - pass - else: - for subtitle, language in subtitles.iteritems(): - if str(os.path.splitext(subtitle)[0]).lower().endswith(tuple(brazilian_portuguese)) is True: - actual_subtitles.append([str("pb"), path_replace_reverse(os.path.join(os.path.dirname(file), subtitle))]) - elif str(language) != 'und': - actual_subtitles.append([str(language), path_replace_reverse(os.path.join(os.path.dirname(file), subtitle))]) - else: - with open(path_replace(os.path.join(os.path.dirname(file), subtitle)), 'r') as f: - text = list(islice(f, 100)) - text = ' '.join(text) - encoding = UnicodeDammit(text) - try: - text = text.decode(encoding.original_encoding) - except Exception as e: - logging.exception('Error trying to detect character encoding for this subtitles file: ' + path_replace(os.path.join(os.path.dirname(file), subtitle)) + ' You should try to delete this subtitles file manually and ask Bazarr to download it again.') - else: - detected_language = langdetect.detect(text) - if len(detected_language) > 0: - actual_subtitles.append([str(detected_language), path_replace_reverse(os.path.join(os.path.dirname(file), subtitle))]) - - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) - c_db = conn_db.cursor() - - c_db.execute("UPDATE table_episodes SET subtitles = ? WHERE path = ?", (str(actual_subtitles), path_replace_reverse(file))) - conn_db.commit() - - c_db.close() - - return actual_subtitles - - -def store_subtitles_movie(file): - # languages = [] - actual_subtitles = [] - if os.path.exists(file): - if os.path.splitext(file)[1] == '.mkv': - try: - with open(file, 'rb') as f: - mkv = enzyme.MKV(f) - - for subtitle_track in mkv.subtitle_tracks: - try: - if alpha2_from_alpha3(subtitle_track.language) != None: - actual_subtitles.append([str(alpha2_from_alpha3(subtitle_track.language)), None]) - except: - pass - except: - pass - - subtitles = core.search_external_subtitles(file) - brazilian_portuguese = [".pt-br", ".pob", "pb"] - - for subtitle, language in subtitles.iteritems(): - if str(os.path.splitext(subtitle)[0]).lower().endswith(tuple(brazilian_portuguese)) is True: - actual_subtitles.append([str("pb"), path_replace_reverse_movie(os.path.join(os.path.dirname(file), subtitle))]) - elif str(language) != 'und': - actual_subtitles.append([str(language), path_replace_reverse_movie(os.path.join(os.path.dirname(file), subtitle))]) - else: - if os.path.splitext(subtitle)[1] != ".sub": - with open(path_replace_movie(os.path.join(os.path.dirname(file), subtitle)), 'r') as f: - text = list(islice(f, 100)) - text = ' '.join(text) - encoding = UnicodeDammit(text) - try: - text = text.decode(encoding.original_encoding) - except Exception as e: - logging.exception('Error trying to detect character encoding for this subtitles file: ' + path_replace_movie(os.path.join(os.path.dirname(file), subtitle)) + ' You should try to delete this subtitles file manually and ask Bazarr to download it again.') - else: - detected_language = langdetect.detect(text) - if len(detected_language) > 0: - actual_subtitles.append([str(detected_language), path_replace_reverse_movie(os.path.join(os.path.dirname(file), subtitle))]) - - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) - c_db = conn_db.cursor() - - c_db.execute("UPDATE table_movies SET subtitles = ? WHERE path = ?", (str(actual_subtitles), path_replace_reverse_movie(file))) - conn_db.commit() - - c_db.close() - - return actual_subtitles - - -def list_missing_subtitles(*no): - query_string = '' - try: - query_string = " WHERE table_shows.sonarrSeriesId = " + str(no[0]) - except: - pass - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) - c_db = conn_db.cursor() - episodes_subtitles = c_db.execute("SELECT table_episodes.sonarrEpisodeId, table_episodes.subtitles, table_shows.languages FROM table_episodes INNER JOIN table_shows on table_episodes.sonarrSeriesId = table_shows.sonarrSeriesId" + query_string).fetchall() - c_db.close() - - missing_subtitles_global = [] - use_embedded_subs = get_general_settings()[23] - for episode_subtitles in episodes_subtitles: - actual_subtitles_temp = [] - actual_subtitles = [] - desired_subtitles = [] - missing_subtitles = [] - if episode_subtitles[1] != None: - if use_embedded_subs is True: - actual_subtitles = ast.literal_eval(episode_subtitles[1]) - else: - actual_subtitles_temp = ast.literal_eval(episode_subtitles[1]) - for subtitle in actual_subtitles_temp: - if subtitle[1] != None: - actual_subtitles.append(subtitle) - if episode_subtitles[2] != None: - desired_subtitles = ast.literal_eval(episode_subtitles[2]) - actual_subtitles_list = [] - if desired_subtitles == None: - missing_subtitles_global.append(tuple(['[]', episode_subtitles[0]])) - else: - for item in actual_subtitles: - if item[0] == "pt-BR": - actual_subtitles_list.append("pb") - else: - actual_subtitles_list.append(item[0]) - missing_subtitles = list(set(desired_subtitles) - set(actual_subtitles_list)) - missing_subtitles_global.append(tuple([str(missing_subtitles), episode_subtitles[0]])) - - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) - c_db = conn_db.cursor() - c_db.executemany("UPDATE table_episodes SET missing_subtitles = ? WHERE sonarrEpisodeId = ?", (missing_subtitles_global)) - conn_db.commit() - c_db.close() - - -def list_missing_subtitles_movies(*no): - query_string = '' - try: - query_string = " WHERE table_movies.radarrId = " + str(no[0]) - except: - pass - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) - c_db = conn_db.cursor() - movies_subtitles = c_db.execute("SELECT radarrId, subtitles, languages FROM table_movies" + query_string).fetchall() - c_db.close() - - missing_subtitles_global = [] - use_embedded_subs = get_general_settings()[23] - for movie_subtitles in movies_subtitles: - actual_subtitles_temp = [] - actual_subtitles = [] - desired_subtitles = [] - missing_subtitles = [] - if movie_subtitles[1] != None: - if use_embedded_subs is True: - actual_subtitles = ast.literal_eval(movie_subtitles[1]) - else: - actual_subtitles_temp = ast.literal_eval(movie_subtitles[1]) - for subtitle in actual_subtitles_temp: - if subtitle[1] != None: - actual_subtitles.append(subtitle) - if movie_subtitles[2] != None: - desired_subtitles = ast.literal_eval(movie_subtitles[2]) - actual_subtitles_list = [] - if desired_subtitles == None: - missing_subtitles_global.append(tuple(['[]', movie_subtitles[0]])) - else: - for item in actual_subtitles: - if item[0] == "pt-BR": - actual_subtitles_list.append("pb") - else: - actual_subtitles_list.append(item[0]) - missing_subtitles = list(set(desired_subtitles) - set(actual_subtitles_list)) - missing_subtitles_global.append(tuple([str(missing_subtitles), movie_subtitles[0]])) - - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) - c_db = conn_db.cursor() - c_db.executemany("UPDATE table_movies SET missing_subtitles = ? WHERE radarrId = ?", (missing_subtitles_global)) - conn_db.commit() - c_db.close() - -def series_full_scan_subtitles(): - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) - c_db = conn_db.cursor() - episodes = c_db.execute("SELECT path FROM table_episodes").fetchall() - c_db.close() - - for episode in episodes: - store_subtitles(path_replace(episode[0])) - - gc.collect() - -def movies_full_scan_subtitles(): - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) - c_db = conn_db.cursor() - movies = c_db.execute("SELECT path FROM table_movies").fetchall() - c_db.close() - - for movie in movies: - store_subtitles_movie(path_replace_movie(movie[0])) - - gc.collect() - -def series_scan_subtitles(no): - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) - c_db = conn_db.cursor() - episodes = c_db.execute("SELECT path FROM table_episodes WHERE sonarrSeriesId = ?", (no,)).fetchall() - c_db.close() - - for episode in episodes: - store_subtitles(path_replace(episode[0])) - - list_missing_subtitles(no) - - -def movies_scan_subtitles(no): - conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) - c_db = conn_db.cursor() - movies = c_db.execute("SELECT path FROM table_movies WHERE radarrId = ?", (no,)).fetchall() - c_db.close() - - for movie in movies: - store_subtitles_movie(path_replace_movie(movie[0])) - - list_missing_subtitles_movies(no) +from get_argv import config_dir + +import gc +import os +import enzyme +import babelfish +import logging +from subliminal import core +import sqlite3 +import ast +import langdetect +from bs4 import UnicodeDammit +from itertools import islice + +from get_settings import path_replace_reverse, path_replace, path_replace_reverse_movie, path_replace_movie, get_general_settings +from get_languages import alpha2_from_alpha3 + +gc.enable() + +def store_subtitles(file): + # languages = [] + actual_subtitles = [] + if os.path.exists(file): + if os.path.splitext(file)[1] == '.mkv': + try: + with open(file, 'rb') as f: + mkv = enzyme.MKV(f) + + for subtitle_track in mkv.subtitle_tracks: + try: + if alpha2_from_alpha3(subtitle_track.language) != None: + actual_subtitles.append([str(alpha2_from_alpha3(subtitle_track.language)),None]) + except: + pass + except: + pass + + + brazilian_portuguese = [".pt-br", ".pob", "pb"] + try: + subtitles = core.search_external_subtitles(file) + except: + pass + else: + for subtitle, language in subtitles.iteritems(): + if str(os.path.splitext(subtitle)[0]).lower().endswith(tuple(brazilian_portuguese)) is True: + actual_subtitles.append([str("pb"), path_replace_reverse(os.path.join(os.path.dirname(file), subtitle))]) + elif str(language) != 'und': + actual_subtitles.append([str(language), path_replace_reverse(os.path.join(os.path.dirname(file), subtitle))]) + else: + with open(path_replace(os.path.join(os.path.dirname(file), subtitle)), 'r') as f: + text = list(islice(f, 100)) + text = ' '.join(text) + encoding = UnicodeDammit(text) + try: + text = text.decode(encoding.original_encoding) + except Exception as e: + logging.exception('Error trying to detect character encoding for this subtitles file: ' + path_replace(os.path.join(os.path.dirname(file), subtitle)) + ' You should try to delete this subtitles file manually and ask Bazarr to download it again.') + else: + detected_language = langdetect.detect(text) + if len(detected_language) > 0: + actual_subtitles.append([str(detected_language), path_replace_reverse(os.path.join(os.path.dirname(file), subtitle))]) + + conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + c_db = conn_db.cursor() + + c_db.execute("UPDATE table_episodes SET subtitles = ? WHERE path = ?", (str(actual_subtitles), path_replace_reverse(file))) + conn_db.commit() + + c_db.close() + + return actual_subtitles + + +def store_subtitles_movie(file): + # languages = [] + actual_subtitles = [] + if os.path.exists(file): + if os.path.splitext(file)[1] == '.mkv': + try: + with open(file, 'rb') as f: + mkv = enzyme.MKV(f) + + for subtitle_track in mkv.subtitle_tracks: + try: + if alpha2_from_alpha3(subtitle_track.language) != None: + actual_subtitles.append([str(alpha2_from_alpha3(subtitle_track.language)), None]) + except: + pass + except: + pass + + subtitles = core.search_external_subtitles(file) + brazilian_portuguese = [".pt-br", ".pob", "pb"] + + for subtitle, language in subtitles.iteritems(): + if str(os.path.splitext(subtitle)[0]).lower().endswith(tuple(brazilian_portuguese)) is True: + actual_subtitles.append([str("pb"), path_replace_reverse_movie(os.path.join(os.path.dirname(file), subtitle))]) + elif str(language) != 'und': + actual_subtitles.append([str(language), path_replace_reverse_movie(os.path.join(os.path.dirname(file), subtitle))]) + else: + if os.path.splitext(subtitle)[1] != ".sub": + with open(path_replace_movie(os.path.join(os.path.dirname(file), subtitle)), 'r') as f: + text = list(islice(f, 100)) + text = ' '.join(text) + encoding = UnicodeDammit(text) + try: + text = text.decode(encoding.original_encoding) + except Exception as e: + logging.exception('Error trying to detect character encoding for this subtitles file: ' + path_replace_movie(os.path.join(os.path.dirname(file), subtitle)) + ' You should try to delete this subtitles file manually and ask Bazarr to download it again.') + else: + detected_language = langdetect.detect(text) + if len(detected_language) > 0: + actual_subtitles.append([str(detected_language), path_replace_reverse_movie(os.path.join(os.path.dirname(file), subtitle))]) + + conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + c_db = conn_db.cursor() + + c_db.execute("UPDATE table_movies SET subtitles = ? WHERE path = ?", (str(actual_subtitles), path_replace_reverse_movie(file))) + conn_db.commit() + + c_db.close() + + return actual_subtitles + + +def list_missing_subtitles(*no): + query_string = '' + try: + query_string = " WHERE table_shows.sonarrSeriesId = " + str(no[0]) + except: + pass + conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + c_db = conn_db.cursor() + episodes_subtitles = c_db.execute("SELECT table_episodes.sonarrEpisodeId, table_episodes.subtitles, table_shows.languages FROM table_episodes INNER JOIN table_shows on table_episodes.sonarrSeriesId = table_shows.sonarrSeriesId" + query_string).fetchall() + c_db.close() + + missing_subtitles_global = [] + use_embedded_subs = get_general_settings()[23] + for episode_subtitles in episodes_subtitles: + actual_subtitles_temp = [] + actual_subtitles = [] + desired_subtitles = [] + missing_subtitles = [] + if episode_subtitles[1] != None: + if use_embedded_subs is True: + actual_subtitles = ast.literal_eval(episode_subtitles[1]) + else: + actual_subtitles_temp = ast.literal_eval(episode_subtitles[1]) + for subtitle in actual_subtitles_temp: + if subtitle[1] != None: + actual_subtitles.append(subtitle) + if episode_subtitles[2] != None: + desired_subtitles = ast.literal_eval(episode_subtitles[2]) + actual_subtitles_list = [] + if desired_subtitles == None: + missing_subtitles_global.append(tuple(['[]', episode_subtitles[0]])) + else: + for item in actual_subtitles: + if item[0] == "pt-BR": + actual_subtitles_list.append("pb") + else: + actual_subtitles_list.append(item[0]) + missing_subtitles = list(set(desired_subtitles) - set(actual_subtitles_list)) + missing_subtitles_global.append(tuple([str(missing_subtitles), episode_subtitles[0]])) + + conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + c_db = conn_db.cursor() + c_db.executemany("UPDATE table_episodes SET missing_subtitles = ? WHERE sonarrEpisodeId = ?", (missing_subtitles_global)) + conn_db.commit() + c_db.close() + + +def list_missing_subtitles_movies(*no): + query_string = '' + try: + query_string = " WHERE table_movies.radarrId = " + str(no[0]) + except: + pass + conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + c_db = conn_db.cursor() + movies_subtitles = c_db.execute("SELECT radarrId, subtitles, languages FROM table_movies" + query_string).fetchall() + c_db.close() + + missing_subtitles_global = [] + use_embedded_subs = get_general_settings()[23] + for movie_subtitles in movies_subtitles: + actual_subtitles_temp = [] + actual_subtitles = [] + desired_subtitles = [] + missing_subtitles = [] + if movie_subtitles[1] != None: + if use_embedded_subs is True: + actual_subtitles = ast.literal_eval(movie_subtitles[1]) + else: + actual_subtitles_temp = ast.literal_eval(movie_subtitles[1]) + for subtitle in actual_subtitles_temp: + if subtitle[1] != None: + actual_subtitles.append(subtitle) + if movie_subtitles[2] != None: + desired_subtitles = ast.literal_eval(movie_subtitles[2]) + actual_subtitles_list = [] + if desired_subtitles == None: + missing_subtitles_global.append(tuple(['[]', movie_subtitles[0]])) + else: + for item in actual_subtitles: + if item[0] == "pt-BR": + actual_subtitles_list.append("pb") + else: + actual_subtitles_list.append(item[0]) + missing_subtitles = list(set(desired_subtitles) - set(actual_subtitles_list)) + missing_subtitles_global.append(tuple([str(missing_subtitles), movie_subtitles[0]])) + + conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + c_db = conn_db.cursor() + c_db.executemany("UPDATE table_movies SET missing_subtitles = ? WHERE radarrId = ?", (missing_subtitles_global)) + conn_db.commit() + c_db.close() + +def series_full_scan_subtitles(): + conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + c_db = conn_db.cursor() + episodes = c_db.execute("SELECT path FROM table_episodes").fetchall() + c_db.close() + + for episode in episodes: + store_subtitles(path_replace(episode[0])) + + gc.collect() + +def movies_full_scan_subtitles(): + conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + c_db = conn_db.cursor() + movies = c_db.execute("SELECT path FROM table_movies").fetchall() + c_db.close() + + for movie in movies: + store_subtitles_movie(path_replace_movie(movie[0])) + + gc.collect() + +def series_scan_subtitles(no): + conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + c_db = conn_db.cursor() + episodes = c_db.execute("SELECT path FROM table_episodes WHERE sonarrSeriesId = ?", (no,)).fetchall() + c_db.close() + + for episode in episodes: + store_subtitles(path_replace(episode[0])) + + list_missing_subtitles(no) + + +def movies_scan_subtitles(no): + conn_db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + c_db = conn_db.cursor() + movies = c_db.execute("SELECT path FROM table_movies WHERE radarrId = ?", (no,)).fetchall() + c_db.close() + + for movie in movies: + store_subtitles_movie(path_replace_movie(movie[0])) + + list_missing_subtitles_movies(no) diff --git a/main.py b/bazarr/main.py similarity index 99% rename from main.py rename to bazarr/main.py index 8d88433b9..3f092b18e 100644 --- a/main.py +++ b/bazarr/main.py @@ -1,4 +1,4 @@ -bazarr_version = '0.6.5' +bazarr_version = '0.6.6' import gc gc.enable() @@ -11,7 +11,7 @@ import os import sys reload(sys) sys.setdefaultencoding('utf8') -sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'libs/')) +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../libs/')) import sqlite3 from init import * @@ -69,7 +69,7 @@ if get_proxy_settings()[0] != 'None': from bottle import route, run, template, static_file, request, redirect, response, HTTPError, app import bottle -bottle.TEMPLATE_PATH.insert(0, os.path.join(os.path.dirname(__file__), 'views/')) +bottle.TEMPLATE_PATH.insert(0, os.path.join(os.path.dirname(__file__), '../views/')) bottle.debug(True) bottle.TEMPLATES.clear() @@ -200,7 +200,7 @@ def restart(): @route(base_url + 'static/:path#.+#', name='static') @custom_auth_basic(check_credentials) def static(path): - return static_file(path, root=os.path.join(os.path.dirname(__file__), 'static')) + return static_file(path, root=os.path.join(os.path.dirname(__file__), '../static')) @route(base_url + 'emptylog') @custom_auth_basic(check_credentials) diff --git a/notifier.py b/bazarr/notifier.py similarity index 100% rename from notifier.py rename to bazarr/notifier.py diff --git a/scheduler.py b/bazarr/scheduler.py similarity index 100% rename from scheduler.py rename to bazarr/scheduler.py diff --git a/update_db.py b/bazarr/update_db.py similarity index 100% rename from update_db.py rename to bazarr/update_db.py diff --git a/utils.py b/bazarr/utils.py similarity index 97% rename from utils.py rename to bazarr/utils.py index dc8929072..10acfebdc 100644 --- a/utils.py +++ b/bazarr/utils.py @@ -1,33 +1,33 @@ -from get_argv import config_dir - -import os -import sqlite3 -import time - -def history_log(action, sonarrSeriesId, sonarrEpisodeId, description): - # Open database connection - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) - c = db.cursor() - - # Get Sonarr API URL from database config table - history = c.execute('''INSERT INTO table_history(action, sonarrSeriesId, sonarrEpisodeId, timestamp, description) VALUES (?, ?, ?, ?, ?)''', (action, sonarrSeriesId, sonarrEpisodeId, time.time(), description)) - - # Commit changes to DB - db.commit() - - # Close database connection - db.close() - - -def history_log_movie(action, radarrId, description): - # Open database connection - db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) - c = db.cursor() - - history = c.execute('''INSERT INTO table_history_movie(action, radarrId, timestamp, description) VALUES (?, ?, ?, ?)''', (action, radarrId, time.time(), description)) - - # Commit changes to DB - db.commit() - - # Close database connection - db.close() +from get_argv import config_dir + +import os +import sqlite3 +import time + +def history_log(action, sonarrSeriesId, sonarrEpisodeId, description): + # Open database connection + db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + c = db.cursor() + + # Get Sonarr API URL from database config table + history = c.execute('''INSERT INTO table_history(action, sonarrSeriesId, sonarrEpisodeId, timestamp, description) VALUES (?, ?, ?, ?, ?)''', (action, sonarrSeriesId, sonarrEpisodeId, time.time(), description)) + + # Commit changes to DB + db.commit() + + # Close database connection + db.close() + + +def history_log_movie(action, radarrId, description): + # Open database connection + db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30) + c = db.cursor() + + history = c.execute('''INSERT INTO table_history_movie(action, radarrId, timestamp, description) VALUES (?, ?, ?, ?)''', (action, radarrId, time.time(), description)) + + # Commit changes to DB + db.commit() + + # Close database connection + db.close()