diff --git a/bazarr.py b/bazarr.py index 13ea808a5..a03f7b2b3 100644 --- a/bazarr.py +++ b/bazarr.py @@ -127,12 +127,16 @@ def image_proxy(url): url_sonarr_short = get_sonarr_settings()[1] apikey = get_sonarr_settings()[2] url_image = url_sonarr_short + '/' + url + '?apikey=' + apikey - img_pil = Image.open(BytesIO(requests.get(url_sonarr_short + '/api' + url_image.split(url_sonarr)[1]).content)) - img_buffer = BytesIO() - img_pil.tobytes() - img_pil.save(img_buffer, img_pil.format) - img_buffer.seek(0) - return send_file(img_buffer, ctype=img_pil.format) + try: + img_pil = Image.open(BytesIO(requests.get(url_sonarr_short + '/api' + url_image.split(url_sonarr)[1]).content)) + except: + return None + else: + img_buffer = BytesIO() + img_pil.tobytes() + img_pil.save(img_buffer, img_pil.format) + img_buffer.seek(0) + return send_file(img_buffer, ctype=img_pil.format) @route(base_url + 'image_proxy_movies/', method='GET') def image_proxy_movies(url): @@ -140,8 +144,13 @@ def image_proxy_movies(url): url_radarr = get_radarr_settings()[0] url_radarr_short = get_radarr_settings()[1] apikey = get_radarr_settings()[2] - url_image = url_radarr_short + '/' + url + '?apikey=' + apikey - img_pil = Image.open(BytesIO(requests.get(url_radarr_short + '/api' + url_image.split(url_radarr)[1]).content)) + try: + url_image = (url_radarr_short + '/' + url + '?apikey=' + apikey).replace('/fanart.jpg', '/banner.jpg') + img_pil = Image.open(BytesIO(requests.get(url_radarr_short + '/api' + url_image.split(url_radarr)[1]).content)) + except: + url_image = url_radarr_short + '/' + url + '?apikey=' + apikey + img_pil = Image.open(BytesIO(requests.get(url_radarr_short + '/api' + url_image.split(url_radarr)[1]).content)) + img_buffer = BytesIO() img_pil.tobytes() img_pil.save(img_buffer, img_pil.format) @@ -198,20 +207,26 @@ def serieseditor(): output = template('serieseditor', __file__=__file__, bazarr_version=bazarr_version, rows=data, languages=languages, missing_count=missing_count, base_url=base_url, single_language=single_language) return output -@route(base_url + 'series_json/', method='GET') -def series_json(query): +@route(base_url + 'search_json/', method='GET') +def search_json(query): db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) c = db.cursor() c.execute("SELECT title, sonarrSeriesId FROM table_shows WHERE title LIKE ? ORDER BY title", ('%'+query+'%',)) - data = c.fetchall() + series = c.fetchall() - series_list = [] - for serie in data: - series_list.append(dict([('name', serie[0]), ('url', base_url + 'episodes/' + str(serie[1]))])) + c.execute("SELECT title, radarrId FROM table_movies WHERE title LIKE ? ORDER BY title", ('%' + query + '%',)) + movies = c.fetchall() + + search_list = [] + for serie in series: + search_list.append(dict([('name', serie[0]), ('url', base_url + 'episodes/' + str(serie[1]))])) + + for movie in movies: + search_list.append(dict([('name', movie[0]), ('url', base_url + 'movie/' + str(movie[1]))])) response.content_type = 'application/json' - return dict(items=series_list) + return dict(items=search_list) @route(base_url + 'edit_series/', method='POST') @@ -335,7 +350,7 @@ def movie(no): c = conn.cursor() movies_details = [] - movies_details = c.execute("SELECT title, overview, poster, fanart, hearing_impaired, tmdbid, audio_language, languages, path_substitution(path) FROM table_movies WHERE radarrId LIKE ?", (str(no),)).fetchone() + movies_details = c.execute("SELECT title, overview, poster, fanart, hearing_impaired, tmdbid, audio_language, languages, path_substitution(path), subtitles FROM table_movies WHERE radarrId LIKE ?", (str(no),)).fetchone() tmdbid = movies_details[5] languages = c.execute("SELECT code2, name FROM table_settings_languages WHERE enabled = 1").fetchall() @@ -343,6 +358,36 @@ def movie(no): return template('movie', __file__=__file__, bazarr_version=bazarr_version, no=no, details=movies_details, languages=languages, url_radarr_short=url_radarr_short, base_url=base_url, tmdbid=tmdbid) +@route(base_url + 'edit_movie/', method='POST') +def edit_movie(no): + ref = request.environ['HTTP_REFERER'] + + lang = request.forms.getall('languages') + if len(lang) > 0: + pass + else: + lang = 'None' + + if str(lang) == "['']": + lang = '[]' + + hi = request.forms.get('hearing_impaired') + + if hi == "on": + hi = "True" + else: + hi = "False" + + conn = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) + c = conn.cursor() + c.execute("UPDATE table_movies SET languages = ?, hearing_impaired = ? WHERE radarrId LIKE ?", (str(lang), hi, no)) + conn.commit() + c.close() + + list_missing_subtitles_movies(no) + + redirect(ref) + @route(base_url + 'scan_disk/', method='GET') def scan_disk(no): ref = request.environ['HTTP_REFERER'] @@ -351,6 +396,14 @@ def scan_disk(no): redirect(ref) +@route(base_url + 'scan_disk_movie/', method='GET') +def scan_disk_movie(no): + ref = request.environ['HTTP_REFERER'] + + movies_scan_subtitles(no) + + redirect(ref) + @route(base_url + 'search_missing_subtitles/', method='GET') def search_missing_subtitles(no): ref = request.environ['HTTP_REFERER'] @@ -359,6 +412,14 @@ def search_missing_subtitles(no): redirect(ref) +@route(base_url + 'search_missing_subtitles_movie/', method='GET') +def search_missing_subtitles_movie(no): + ref = request.environ['HTTP_REFERER'] + + movies_download_subtitles(no) + + redirect(ref) + @route(base_url + 'history') def history(): db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) diff --git a/get_movies.py b/get_movies.py index a881650a5..b368b6a01 100644 --- a/get_movies.py +++ b/get_movies.py @@ -44,14 +44,25 @@ def update_movies(): except: fanart = "" + if 'sceneName' in movie['movieFile']: + sceneName = movie['movieFile']['sceneName'] + else: + sceneName = None + # Add movies in radarr to current movies list current_movies_radarr.append(unicode(movie['tmdbId'])) + # Detect file separator + if movie['path'][0] == "/": + separator = "/" + else: + separator == "\\" + # Update or insert movies list in database table try: - c.execute('''INSERT INTO table_movies(title, path, tmdbId, languages,`hearing_impaired`, radarrId, overview, poster, fanart, `audio_language`) VALUES (?,?,?,(SELECT languages FROM table_movies WHERE tmdbId = ?),(SELECT `hearing_impaired` FROM table_movies WHERE tmdbId = ?), ?, ?, ?, ?, ?)''', (movie["title"], os.path.join(movie["path"], movie['movieFile']['relativePath']), movie["tmdbId"], movie["tmdbId"], movie["tmdbId"], movie["id"], overview, poster, fanart, profile_id_to_language(movie['qualityProfileId']))) + c.execute('''INSERT INTO table_movies(title, path, tmdbId, languages,`hearing_impaired`, radarrId, overview, poster, fanart, `audio_language`, sceneName) VALUES (?,?,?,(SELECT languages FROM table_movies WHERE tmdbId = ?),(SELECT `hearing_impaired` FROM table_movies WHERE tmdbId = ?), ?, ?, ?, ?, ?, ?)''', (movie["title"], movie["path"] + separator + movie['movieFile']['relativePath'], movie["tmdbId"], movie["tmdbId"], movie["tmdbId"], movie["id"], overview, poster, fanart, profile_id_to_language(movie['qualityProfileId']), sceneName)) except: - c.execute('''UPDATE table_movies SET title = ?, path = ?, tmdbId = ?, radarrId = ?, overview = ?, poster = ?, fanart = ?, `audio_language` = ? WHERE tmdbid = ?''', (movie["title"],os.path.join(movie["path"], movie['movieFile']['relativePath']),movie["tmdbId"],movie["id"],overview,poster,fanart,profile_id_to_language(movie['qualityProfileId']),movie["tmdbId"])) + c.execute('''UPDATE table_movies SET title = ?, path = ?, tmdbId = ?, radarrId = ?, overview = ?, poster = ?, fanart = ?, `audio_language` = ?, sceneName = ? WHERE tmdbid = ?''', (movie["title"],movie["path"] + separator + movie['movieFile']['relativePath'],movie["tmdbId"],movie["id"],overview,poster,fanart,profile_id_to_language(movie['qualityProfileId']),sceneName,movie["tmdbId"])) # Delete movies not in radarr anymore deleted_items = [] diff --git a/get_subtitle.py b/get_subtitle.py index eec972df9..b0144229d 100644 --- a/get_subtitle.py +++ b/get_subtitle.py @@ -10,7 +10,7 @@ from bs4 import UnicodeDammit from get_general_settings import * from list_subtitles import * from utils import * -from notifier import send_notifications +from notifier import send_notifications, send_notifications_movie # configure the cache region.configure('dogpile.cache.memory') @@ -129,6 +129,40 @@ def series_download_subtitles(no): send_notifications(no, episode[2], message) list_missing_subtitles(no) + +def movies_download_subtitles(no): + conn_db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) + c_db = conn_db.cursor() + movie = c_db.execute("SELECT path, missing_subtitles, radarrId, sceneName, hearing_impaired FROM table_movies WHERE radarrId = ?", (no,)).fetchone() + enabled_providers = c_db.execute("SELECT * FROM table_settings_providers WHERE enabled = 1").fetchall() + c_db.close() + + providers_list = [] + providers_auth = {} + if len(enabled_providers) > 0: + for provider in enabled_providers: + providers_list.append(provider[0]) + try: + if provider[2] is not '' and provider[3] is not '': + provider_auth = providers_auth.append(provider[0]) + provider_auth.update({'username': providers[2], 'password': providers[3]}) + else: + providers_auth = None + except: + providers_auth = None + else: + providers_list = None + providers_auth = None + + for language in ast.literal_eval(movie[1]): + message = download_subtitle(path_replace(movie[0]), str(pycountry.languages.lookup(language).alpha_3), movie[4], providers_list, providers_auth, movie[3]) + if message is not None: + store_subtitles_movie(path_replace(movie[0])) + history_log_movie(1, no, message) + send_notifications_movie(no, message) + list_missing_subtitles_movies(no) + + def wanted_download_subtitles(path): conn_db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) c_db = conn_db.cursor() diff --git a/list_subtitles.py b/list_subtitles.py index a934c3888..bed4855cd 100644 --- a/list_subtitles.py +++ b/list_subtitles.py @@ -145,6 +145,43 @@ def list_missing_subtitles(*no): 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(os.path.dirname(__file__), 'data/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 = [] + + for movie_subtitles in movies_subtitles: + actual_subtitles = [] + desired_subtitles = [] + missing_subtitles = [] + if movie_subtitles[1] != None: + actual_subtitles = ast.literal_eval(movie_subtitles[1]) + 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: + 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(os.path.dirname(__file__), 'data/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 full_scan_subtitles(): conn_db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) c_db = conn_db.cursor() @@ -174,6 +211,19 @@ def series_scan_subtitles(no): list_missing_subtitles(no) + +def movies_scan_subtitles(no): + conn_db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/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[0])) + + list_missing_subtitles_movies(no) + + def new_scan_subtitles(): conn_db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) c_db = conn_db.cursor() @@ -182,3 +232,6 @@ def new_scan_subtitles(): for episode in episodes: store_subtitles(path_replace(episode[0])) + +if __name__ == '__main__': + full_scan_subtitles() \ No newline at end of file diff --git a/notifier.py b/notifier.py index 206dc79d8..8a5a0fe97 100644 --- a/notifier.py +++ b/notifier.py @@ -26,6 +26,14 @@ def get_episode_name(sonarrEpisodeId): return data[0] +def get_movies_name(radarrId): + conn_db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) + c_db = conn_db.cursor() + data = c_db.execute('SELECT title FROM table_movies WHERE radarrId = ?', (radarrId,)).fetchone() + c_db.close() + + return data[0] + def send_notifications(sonarrSeriesId, sonarrEpisodeId, message): providers = get_notifier_providers() series = get_series_name(sonarrSeriesId) @@ -40,4 +48,20 @@ def send_notifications(sonarrSeriesId, sonarrEpisodeId, message): apobj.notify( title='Bazarr notification', body=series + ' - ' + episode + ' : ' + message, + ) + + +def send_notifications_movie(radarrId, message): + providers = get_notifier_providers() + movie = get_movies_name(radarrId) + + apobj = apprise.Apprise() + + for provider in providers: + if provider[1] is not None: + apobj.add(provider[1]) + + apobj.notify( + title='Bazarr notification', + body=movie + ' : ' + message, ) \ No newline at end of file diff --git a/update_db.py b/update_db.py index 2e3c1a0da..49e472483 100644 --- a/update_db.py +++ b/update_db.py @@ -88,7 +88,12 @@ if os.path.exists(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db')) pass try: - c.execute('CREATE TABLE "table_movies" ( `tmdbId` TEXT NOT NULL UNIQUE, `title` TEXT NOT NULL, `path` TEXT NOT NULL UNIQUE, `languages` TEXT, `subtitles` TEXT, `missing_subtitles` TEXT, `hearing_impaired` TEXT, `radarrId` INTEGER NOT NULL UNIQUE, `overview` TEXT, `poster` TEXT, `fanart` TEXT, "audio_language" "text", `sceceName` TEXT, PRIMARY KEY(`tmdbId`) )') + c.execute('CREATE TABLE "table_movies" ( `tmdbId` TEXT NOT NULL UNIQUE, `title` TEXT NOT NULL, `path` TEXT NOT NULL UNIQUE, `languages` TEXT, `subtitles` TEXT, `missing_subtitles` TEXT, `hearing_impaired` TEXT, `radarrId` INTEGER NOT NULL UNIQUE, `overview` TEXT, `poster` TEXT, `fanart` TEXT, "audio_language" "text", `sceneName` TEXT, PRIMARY KEY(`tmdbId`) )') + except: + pass + + try: + c.execute('CREATE TABLE "table_history_movie" ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, `action` INTEGER NOT NULL, `radarrId` INTEGER NOT NULL, `timestamp` INTEGER NOT NULL, `description` TEXT NOT NULL )') except: pass diff --git a/utils.py b/utils.py index 21884bd2f..fe4a9a0e0 100644 --- a/utils.py +++ b/utils.py @@ -15,3 +15,17 @@ def history_log(action, sonarrSeriesId, sonarrEpisodeId, description): # Close database connection db.close() + + +def history_log_movie(action, radarrId, description): + # Open database connection + db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/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() diff --git a/views/menu.tpl b/views/menu.tpl index f8394eb3f..370096f9e 100644 --- a/views/menu.tpl +++ b/views/menu.tpl @@ -114,7 +114,7 @@ $('.ui.search') .search({ apiSettings: { - url: '{{base_url}}series_json/{query}', + url: '{{base_url}}search_json/{query}', onResponse: function(results) { var response = { results : [] diff --git a/views/movie.tpl b/views/movie.tpl index c145b48d3..40028e3a7 100644 --- a/views/movie.tpl +++ b/views/movie.tpl @@ -19,7 +19,7 @@