From 41fd0e153ebea507280c323effe830d77af7983e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20V=C3=A9zina?= <5130500+morpheus65535@users.noreply.github.com> Date: Mon, 11 Mar 2019 17:44:49 -0400 Subject: [PATCH 1/5] Improvement to history in preparation for subtitles upgrading. --- bazarr/get_subtitle.py | 51 +++++++++++++++++++++++++++++++----------- bazarr/helper.py | 45 +++++++++++++++++++++++++++++++++++++ bazarr/main.py | 48 ++++++++++++++++++++++++++------------- bazarr/update_db.py | 21 +++++++++++++++++ bazarr/utils.py | 12 +++++----- 5 files changed, 143 insertions(+), 34 deletions(-) diff --git a/bazarr/get_subtitle.py b/bazarr/get_subtitle.py index 89187447f..eeea3242c 100644 --- a/bazarr/get_subtitle.py +++ b/bazarr/get_subtitle.py @@ -226,7 +226,12 @@ def download_subtitle(path, language, hi, providers, providers_auth, sceneName, logging.info('BAZARR Post-processing result for file ' + path + ' : ' + out) # fixme: support multiple languages at once - return message + if media_type == 'series': + reversed_path = path_replace_reverse(path) + else: + reversed_path = path_replace_reverse_movie(path) + + return message, reversed_path, downloaded_language_code2, downloaded_provider, subtitle.score if not saved_any: logging.debug('BAZARR No subtitles were found for this file: ' + path) @@ -431,12 +436,17 @@ def series_download_subtitles(no): for language in ast.literal_eval(episode[1]): if language is not None: notifications.write(msg='Searching for ' + str(language_from_alpha2(language)) + ' subtitles for this episode: ' + path_replace(episode[0]), queue='get_subtitle') - message = download_subtitle(path_replace(episode[0]), str(alpha3_from_alpha2(language)), + result = download_subtitle(path_replace(episode[0]), str(alpha3_from_alpha2(language)), series_details[0], providers_list, providers_auth, str(episode[3]), series_details[1], 'series') - if message is not None: + if result is not None: + message = result[0] + path = result[1] + language_code = result[2] + provider = result[3] + score = result[4] store_subtitles(path_replace(episode[0])) - history_log(1, no, episode[2], message) + history_log(1, no, episode[2], message, path, language_code, provider, score) send_notifications(no, episode[2], message) list_missing_subtitles(no) @@ -457,11 +467,16 @@ def movies_download_subtitles(no): for language in ast.literal_eval(movie[1]): if language is not None: notifications.write(msg='Searching for ' + str(language_from_alpha2(language)) + ' subtitles for this movie: ' + path_replace_movie(movie[0]), queue='get_subtitle') - message = download_subtitle(path_replace_movie(movie[0]), str(alpha3_from_alpha2(language)), movie[4], + result = download_subtitle(path_replace_movie(movie[0]), str(alpha3_from_alpha2(language)), movie[4], providers_list, providers_auth, str(movie[3]), movie[5], 'movie') - if message is not None: + if result is not None: + message = result[0] + path = result[1] + language_code = result[2] + provider = result[3] + score = result[4] store_subtitles_movie(path_replace_movie(movie[0])) - history_log_movie(1, no, message) + history_log_movie(1, no, message, path, language_code, provider, score) send_notifications_movie(no, message) list_missing_subtitles_movies(no) @@ -504,13 +519,18 @@ def wanted_download_subtitles(path): if search_active(attempt[i][1]): notifications.write( msg='Searching ' + str(language_from_alpha2(language)) + ' subtitles for this episode: ' + path, queue='get_subtitle') - message = download_subtitle(path_replace(episode[0]), str(alpha3_from_alpha2(language)), + result = download_subtitle(path_replace(episode[0]), str(alpha3_from_alpha2(language)), episode[4], providers_list, providers_auth, str(episode[5]), episode[7], 'series') - if message is not None: + if result is not None: + message = result[0] + path = result[1] + language_code = result[2] + provider = result[3] + score = result[4] store_subtitles(path_replace(episode[0])) list_missing_subtitles(episode[3]) - history_log(1, episode[3], episode[2], message) + history_log(1, episode[3], episode[2], message, path, language_code, provider, score) send_notifications(episode[3], episode[2], message) else: logging.debug( @@ -552,13 +572,18 @@ def wanted_download_subtitles_movie(path): if search_active(attempt[i][1]) is True: notifications.write( msg='Searching ' + str(language_from_alpha2(language)) + ' subtitles for this movie: ' + path, queue='get_subtitle') - message = download_subtitle(path_replace_movie(movie[0]), str(alpha3_from_alpha2(language)), + result = download_subtitle(path_replace_movie(movie[0]), str(alpha3_from_alpha2(language)), movie[4], providers_list, providers_auth, str(movie[5]), movie[7], 'movie') - if message is not None: + if result is not None: + message = result[0] + path = result[1] + language_code = result[2] + provider = result[3] + score = result[4] store_subtitles_movie(path_replace_movie(movie[0])) list_missing_subtitles_movies(movie[3]) - history_log_movie(1, movie[3], message) + history_log_movie(1, movie[3], message, path, language_code, provider, score) send_notifications_movie(movie[3], message) else: logging.info( diff --git a/bazarr/helper.py b/bazarr/helper.py index 903ea1029..258cfa6bc 100644 --- a/bazarr/helper.py +++ b/bazarr/helper.py @@ -4,8 +4,11 @@ import os import re import types import logging +import sqlite3 from config import settings +from get_args import args +from get_languages import alpha2_from_language def path_replace(path): @@ -121,3 +124,45 @@ def force_unicode(s): except UnicodeDecodeError: s = UnicodeDammit(s).unicode_markup return s + + +def upgrade_history(): + # Open database connection + db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) + c = db.cursor() + + data = c.execute('SELECT sonarrEpisodeId, video_path, description, ROWID FROM table_history WHERE action = 1').fetchall() + + for row in data: + if row[1] is None: + path = c.execute('SELECT path FROM table_episodes WHERE sonarrEpisodeId = ?', (row[0],)).fetchone() + c.execute('UPDATE table_history SET video_path = ? WHERE sonarrEpisodeId = ?', (path[0], row[0])) + + values = re.split(r' subtitles downloaded from | with a score of | using', row[2])[:-1] + language = alpha2_from_language(values[0]) + provider = values[1] + score = int(round((float(values[2][:-1])/100 * 360) - 0.5)) + c.execute('UPDATE table_history SET language = ?, provider = ?, score = ? WHERE ROWID = ?', (language, provider, score, row[3])) + db.commit() + db.close() + +def upgrade_history_movies(): + # Open database connection + db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) + c = db.cursor() + + data = c.execute( + 'SELECT radarrId, video_path, description, ROWID FROM table_history_movie WHERE action = 1').fetchall() + + for row in data: + if row[1] is None: + path = c.execute('SELECT path FROM table_movies WHERE radarrId = ?', (row[0],)).fetchone() + c.execute('UPDATE table_history_movie SET video_path = ? WHERE radarrId = ?', (path[0], row[0])) + + values = re.split(r' subtitles downloaded from | with a score of | using', row[2])[:-1] + language = alpha2_from_language(values[0]) + provider = values[1] + score = int(round((float(values[2][:-1]) / 100 * 120) - 0.5)) + c.execute('UPDATE table_history_movie SET language = ?, provider = ?, score = ? WHERE ROWID = ?', (language, provider, score, row[3])) + db.commit() + db.close() diff --git a/bazarr/main.py b/bazarr/main.py index ac827d1e0..9b1524cf0 100644 --- a/bazarr/main.py +++ b/bazarr/main.py @@ -1635,8 +1635,8 @@ def remove_subtitles(): os.remove(subtitlesPath) result = language_from_alpha3(language) + " subtitles deleted from disk." history_log(0, sonarrSeriesId, sonarrEpisodeId, result) - except OSError: - pass + except OSError as e: + logging.exception('BAZARR cannot delete subtitles file: ' + subtitlesPath) store_subtitles(unicode(episodePath)) list_missing_subtitles(sonarrSeriesId) @@ -1649,15 +1649,13 @@ def remove_subtitles_movie(): language = request.forms.get('language') subtitlesPath = request.forms.get('subtitlesPath') radarrId = request.forms.get('radarrId') - subfolder = ('/' + get_subtitle_destination_folder() + '/') if get_subtitle_destination_folder() else '/' - subtitlesPath = os.path.split(subtitlesPath) try: - os.remove(subtitlesPath[0] + subfolder + subtitlesPath[1]) + os.remove(subtitlesPath) result = language_from_alpha3(language) + " subtitles deleted from disk." history_log_movie(0, radarrId, result) - except OSError: - pass + except OSError as e: + logging.exception('BAZARR cannot delete subtitles file: ' + subtitlesPath) store_subtitles_movie(unicode(moviePath)) list_missing_subtitles_movies(radarrId) @@ -1684,8 +1682,13 @@ def get_subtitle(): result = download_subtitle(episodePath, language, hi, providers_list, providers_auth, sceneName, title, 'series') if result is not None: - history_log(1, sonarrSeriesId, sonarrEpisodeId, result) - send_notifications(sonarrSeriesId, sonarrEpisodeId, result) + message = result[0] + path = result[1] + language_code = result[2] + provider = result[3] + score = result[4] + history_log(1, sonarrSeriesId, sonarrEpisodeId, message, path, language_code, provider, score) + send_notifications(sonarrSeriesId, sonarrEpisodeId, message) store_subtitles(unicode(episodePath)) list_missing_subtitles(sonarrSeriesId) redirect(ref) @@ -1735,8 +1738,13 @@ def manual_get_subtitle(): result = manual_download_subtitle(episodePath, language, hi, subtitle, selected_provider, providers_auth, sceneName, title, 'series') if result is not None: - history_log(1, sonarrSeriesId, sonarrEpisodeId, result) - send_notifications(sonarrSeriesId, sonarrEpisodeId, result) + message = result[0] + path = result[1] + language_code = result[2] + provider = result[3] + score = result[4] + history_log(1, sonarrSeriesId, sonarrEpisodeId, message, path, language_code, provider, score) + send_notifications(sonarrSeriesId, sonarrEpisodeId, message) store_subtitles(unicode(episodePath)) list_missing_subtitles(sonarrSeriesId) redirect(ref) @@ -1764,8 +1772,13 @@ def get_subtitle_movie(): try: result = download_subtitle(moviePath, language, hi, providers_list, providers_auth, sceneName, title, 'movie') if result is not None: - history_log_movie(1, radarrId, result) - send_notifications_movie(radarrId, result) + message = result[0] + path = result[1] + language_code = result[2] + provider = result[3] + score = result[4] + history_log_movie(1, radarrId, message, path, language_code, provider, score) + send_notifications_movie(radarrId, message) store_subtitles_movie(unicode(moviePath)) list_missing_subtitles_movies(radarrId) redirect(ref) @@ -1814,8 +1827,13 @@ def manual_get_subtitle_movie(): result = manual_download_subtitle(moviePath, language, hi, subtitle, selected_provider, providers_auth, sceneName, title, 'movie') if result is not None: - history_log_movie(1, radarrId, result) - send_notifications_movie(radarrId, result) + message = result[0] + path = result[1] + language_code = result[2] + provider = result[3] + score = result[4] + history_log_movie(1, radarrId, message, path, language_code, provider, score) + send_notifications_movie(radarrId, message) store_subtitles_movie(unicode(moviePath)) list_missing_subtitles_movies(radarrId) redirect(ref) diff --git a/bazarr/update_db.py b/bazarr/update_db.py index 98a1b55c0..1f4c30976 100644 --- a/bazarr/update_db.py +++ b/bazarr/update_db.py @@ -6,6 +6,7 @@ import sqlite3 from get_args import args from scheduler import execute_now from config import settings +from helper import upgrade_history, upgrade_history_movies # Check if database exist if os.path.exists(os.path.join(args.config_dir, 'db', 'bazarr.db')): @@ -121,5 +122,25 @@ if os.path.exists(os.path.join(args.config_dir, 'db', 'bazarr.db')): execute_now('sync_episodes') if settings.general.getboolean('use_radarr'): execute_now('update_movies') + + try: + c.execute('alter table table_history add column "video_path" "text"') + c.execute('alter table table_history add column "language" "text"') + c.execute('alter table table_history add column "provider" "text"') + c.execute('alter table table_history add column "score" "text"') + + c.execute('alter table table_history_movie add column "video_path" "text"') + c.execute('alter table table_history_movie add column "language" "text"') + c.execute('alter table table_history_movie add column "provider" "text"') + c.execute('alter table table_history_movie add column "score" "text"') + + db.commit() + except: + pass + else: + if settings.general.getboolean('use_sonarr'): + upgrade_history() + if settings.general.getboolean('use_radarr'): + upgrade_history_movies() db.close() diff --git a/bazarr/utils.py b/bazarr/utils.py index 8d20bc1f6..724d7144d 100644 --- a/bazarr/utils.py +++ b/bazarr/utils.py @@ -7,15 +7,15 @@ import time from get_args import args -def history_log(action, sonarrSeriesId, sonarrEpisodeId, description): +def history_log(action, sonarrSeriesId, sonarrEpisodeId, description, video_path=None, language=None, provider=None, score=None): # Open database connection db = sqlite3.connect(os.path.join(args.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)) + '''INSERT INTO table_history(action, sonarrSeriesId, sonarrEpisodeId, timestamp, description, video_path, language, provider, score) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)''', + (action, sonarrSeriesId, sonarrEpisodeId, time.time(), description, video_path, language, provider, score)) # Commit changes to DB db.commit() @@ -24,14 +24,14 @@ def history_log(action, sonarrSeriesId, sonarrEpisodeId, description): db.close() -def history_log_movie(action, radarrId, description): +def history_log_movie(action, radarrId, description, video_path=None, language=None, provider=None, score=None): # Open database connection db = sqlite3.connect(os.path.join(args.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)) + '''INSERT INTO table_history_movie(action, radarrId, timestamp, description, video_path, language, provider, score) VALUES (?, ?, ?, ?, ?, ?, ?, ?)''', + (action, radarrId, time.time(), description, video_path, language, provider, score)) # Commit changes to DB db.commit() From 81d1015e7e34ca0c58f079e890ad5fb2e75e67ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20V=C3=A9zina?= <5130500+morpheus65535@users.noreply.github.com> Date: Mon, 11 Mar 2019 21:03:38 -0400 Subject: [PATCH 2/5] Fix for deleted elements in history. --- bazarr/helper.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/bazarr/helper.py b/bazarr/helper.py index 258cfa6bc..e4dfcbc2f 100644 --- a/bazarr/helper.py +++ b/bazarr/helper.py @@ -136,13 +136,14 @@ def upgrade_history(): for row in data: if row[1] is None: path = c.execute('SELECT path FROM table_episodes WHERE sonarrEpisodeId = ?', (row[0],)).fetchone() - c.execute('UPDATE table_history SET video_path = ? WHERE sonarrEpisodeId = ?', (path[0], row[0])) - - values = re.split(r' subtitles downloaded from | with a score of | using', row[2])[:-1] - language = alpha2_from_language(values[0]) - provider = values[1] - score = int(round((float(values[2][:-1])/100 * 360) - 0.5)) - c.execute('UPDATE table_history SET language = ?, provider = ?, score = ? WHERE ROWID = ?', (language, provider, score, row[3])) + if path is not None: + c.execute('UPDATE table_history SET video_path = ? WHERE sonarrEpisodeId = ?', (path[0], row[0])) + + values = re.split(r' subtitles downloaded from | with a score of | using', row[2])[:-1] + language = alpha2_from_language(values[0]) + provider = values[1] + score = int(round((float(values[2][:-1])/100 * 360) - 0.5)) + c.execute('UPDATE table_history SET language = ?, provider = ?, score = ? WHERE ROWID = ?', (language, provider, score, row[3])) db.commit() db.close() @@ -151,18 +152,18 @@ def upgrade_history_movies(): db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() - data = c.execute( - 'SELECT radarrId, video_path, description, ROWID FROM table_history_movie WHERE action = 1').fetchall() + data = c.execute('SELECT radarrId, video_path, description, ROWID FROM table_history_movie WHERE action = 1').fetchall() for row in data: if row[1] is None: path = c.execute('SELECT path FROM table_movies WHERE radarrId = ?', (row[0],)).fetchone() - c.execute('UPDATE table_history_movie SET video_path = ? WHERE radarrId = ?', (path[0], row[0])) - - values = re.split(r' subtitles downloaded from | with a score of | using', row[2])[:-1] - language = alpha2_from_language(values[0]) - provider = values[1] - score = int(round((float(values[2][:-1]) / 100 * 120) - 0.5)) - c.execute('UPDATE table_history_movie SET language = ?, provider = ?, score = ? WHERE ROWID = ?', (language, provider, score, row[3])) + if path is not None: + c.execute('UPDATE table_history_movie SET video_path = ? WHERE radarrId = ?', (path[0], row[0])) + + values = re.split(r' subtitles downloaded from | with a score of | using', row[2])[:-1] + language = alpha2_from_language(values[0]) + provider = values[1] + score = int(round((float(values[2][:-1]) / 100 * 120) - 0.5)) + c.execute('UPDATE table_history_movie SET language = ?, provider = ?, score = ? WHERE ROWID = ?', (language, provider, score, row[3])) db.commit() db.close() From 53c6d9e32912a90e9f810d2eaf54b3fee8f991c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20V=C3=A9zina?= <5130500+morpheus65535@users.noreply.github.com> Date: Mon, 11 Mar 2019 22:37:28 -0400 Subject: [PATCH 3/5] Removal of history reverse engineering. --- bazarr/helper.py | 43 ------------------------------------------- bazarr/update_db.py | 5 ----- 2 files changed, 48 deletions(-) diff --git a/bazarr/helper.py b/bazarr/helper.py index e4dfcbc2f..434e0dd1c 100644 --- a/bazarr/helper.py +++ b/bazarr/helper.py @@ -124,46 +124,3 @@ def force_unicode(s): except UnicodeDecodeError: s = UnicodeDammit(s).unicode_markup return s - - -def upgrade_history(): - # Open database connection - db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) - c = db.cursor() - - data = c.execute('SELECT sonarrEpisodeId, video_path, description, ROWID FROM table_history WHERE action = 1').fetchall() - - for row in data: - if row[1] is None: - path = c.execute('SELECT path FROM table_episodes WHERE sonarrEpisodeId = ?', (row[0],)).fetchone() - if path is not None: - c.execute('UPDATE table_history SET video_path = ? WHERE sonarrEpisodeId = ?', (path[0], row[0])) - - values = re.split(r' subtitles downloaded from | with a score of | using', row[2])[:-1] - language = alpha2_from_language(values[0]) - provider = values[1] - score = int(round((float(values[2][:-1])/100 * 360) - 0.5)) - c.execute('UPDATE table_history SET language = ?, provider = ?, score = ? WHERE ROWID = ?', (language, provider, score, row[3])) - db.commit() - db.close() - -def upgrade_history_movies(): - # Open database connection - db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) - c = db.cursor() - - data = c.execute('SELECT radarrId, video_path, description, ROWID FROM table_history_movie WHERE action = 1').fetchall() - - for row in data: - if row[1] is None: - path = c.execute('SELECT path FROM table_movies WHERE radarrId = ?', (row[0],)).fetchone() - if path is not None: - c.execute('UPDATE table_history_movie SET video_path = ? WHERE radarrId = ?', (path[0], row[0])) - - values = re.split(r' subtitles downloaded from | with a score of | using', row[2])[:-1] - language = alpha2_from_language(values[0]) - provider = values[1] - score = int(round((float(values[2][:-1]) / 100 * 120) - 0.5)) - c.execute('UPDATE table_history_movie SET language = ?, provider = ?, score = ? WHERE ROWID = ?', (language, provider, score, row[3])) - db.commit() - db.close() diff --git a/bazarr/update_db.py b/bazarr/update_db.py index 1f4c30976..6e1b3c519 100644 --- a/bazarr/update_db.py +++ b/bazarr/update_db.py @@ -137,10 +137,5 @@ if os.path.exists(os.path.join(args.config_dir, 'db', 'bazarr.db')): db.commit() except: pass - else: - if settings.general.getboolean('use_sonarr'): - upgrade_history() - if settings.general.getboolean('use_radarr'): - upgrade_history_movies() db.close() From 1fcc6323e8fe64b884f8b1c50d8fab7e17ecbb90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20V=C3=A9zina?= <5130500+morpheus65535@users.noreply.github.com> Date: Mon, 11 Mar 2019 23:30:08 -0400 Subject: [PATCH 4/5] Removal of history reverse engineering. --- bazarr/update_db.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bazarr/update_db.py b/bazarr/update_db.py index 6e1b3c519..3ca6e1e65 100644 --- a/bazarr/update_db.py +++ b/bazarr/update_db.py @@ -6,7 +6,6 @@ import sqlite3 from get_args import args from scheduler import execute_now from config import settings -from helper import upgrade_history, upgrade_history_movies # Check if database exist if os.path.exists(os.path.join(args.config_dir, 'db', 'bazarr.db')): From 76eca81afc0cc5994f1ab6bbd2f21a3c9a265cc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20V=C3=A9zina?= <5130500+morpheus65535@users.noreply.github.com> Date: Tue, 12 Mar 2019 07:07:29 -0400 Subject: [PATCH 5/5] Version bump. --- bazarr/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bazarr/main.py b/bazarr/main.py index 9b1524cf0..37b1446f9 100644 --- a/bazarr/main.py +++ b/bazarr/main.py @@ -1,6 +1,6 @@ # coding=utf-8 -bazarr_version = '0.7.2' +bazarr_version = '0.7.3' import gc import sys