2018-10-31 19:34:40 +00:00
|
|
|
# coding=utf-8
|
2018-08-16 02:01:49 +00:00
|
|
|
|
2018-04-19 23:45:10 +00:00
|
|
|
import os
|
|
|
|
import sqlite3
|
|
|
|
import requests
|
2018-07-11 02:31:58 +00:00
|
|
|
import logging
|
2019-01-01 20:15:12 +00:00
|
|
|
from queueconfig import q4ws
|
2018-04-19 23:45:10 +00:00
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
from get_args import args
|
2018-12-15 00:36:28 +00:00
|
|
|
from config import settings, url_radarr
|
|
|
|
from helper import path_replace_movie
|
2018-08-15 20:51:46 +00:00
|
|
|
from list_subtitles import store_subtitles_movie, list_missing_subtitles_movies
|
2018-04-19 23:45:10 +00:00
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
|
2018-04-19 23:45:10 +00:00
|
|
|
def update_movies():
|
2018-12-17 04:18:53 +00:00
|
|
|
q4ws.append("Update movies list from Radarr is running...")
|
2018-10-01 14:44:27 +00:00
|
|
|
logging.debug('BAZARR Starting movie sync from Radarr.')
|
2018-12-15 00:36:28 +00:00
|
|
|
apikey_radarr = settings.radarr.apikey
|
2018-12-27 19:19:59 +00:00
|
|
|
movie_default_enabled = settings.general.getboolean('movie_default_enabled')
|
2018-12-15 00:36:28 +00:00
|
|
|
movie_default_language = settings.general.movie_default_language
|
|
|
|
movie_default_hi = settings.general.movie_default_hi
|
2019-01-15 16:25:13 +00:00
|
|
|
|
2018-12-15 00:36:28 +00:00
|
|
|
if apikey_radarr is None:
|
2018-04-19 23:45:10 +00:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
get_profile_list()
|
2019-01-15 16:25:13 +00:00
|
|
|
|
2018-04-19 23:45:10 +00:00
|
|
|
# Get movies data from radarr
|
|
|
|
url_radarr_api_movies = url_radarr + "/api/movie?apikey=" + apikey_radarr
|
2018-07-11 02:31:58 +00:00
|
|
|
try:
|
2018-09-02 13:48:50 +00:00
|
|
|
r = requests.get(url_radarr_api_movies, timeout=15, verify=False)
|
2018-07-11 02:31:58 +00:00
|
|
|
r.raise_for_status()
|
|
|
|
except requests.exceptions.HTTPError as errh:
|
2018-10-19 03:33:01 +00:00
|
|
|
logging.exception("BAZARR Error trying to get movies from Radarr. Http error.")
|
2018-07-11 02:31:58 +00:00
|
|
|
except requests.exceptions.ConnectionError as errc:
|
2018-10-19 03:33:01 +00:00
|
|
|
logging.exception("BAZARR Error trying to get movies from Radarr. Connection Error.")
|
2018-07-11 02:31:58 +00:00
|
|
|
except requests.exceptions.Timeout as errt:
|
2018-10-19 03:33:01 +00:00
|
|
|
logging.exception("BAZARR Error trying to get movies from Radarr. Timeout Error.")
|
2018-07-11 02:31:58 +00:00
|
|
|
except requests.exceptions.RequestException as err:
|
2018-10-19 03:33:01 +00:00
|
|
|
logging.exception("BAZARR Error trying to get movies from Radarr.")
|
2018-07-11 02:31:58 +00:00
|
|
|
else:
|
|
|
|
# Get current movies in DB
|
2018-10-31 19:34:40 +00:00
|
|
|
db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30)
|
2018-09-25 02:23:47 +00:00
|
|
|
c = db.cursor()
|
2019-01-06 18:50:36 +00:00
|
|
|
current_movies_db = c.execute('SELECT tmdbId, path FROM table_movies').fetchall()
|
2018-09-25 02:23:47 +00:00
|
|
|
db.close()
|
2019-01-15 16:25:13 +00:00
|
|
|
|
2018-07-11 02:31:58 +00:00
|
|
|
current_movies_db_list = [x[0] for x in current_movies_db]
|
|
|
|
current_movies_radarr = []
|
2018-09-25 02:23:47 +00:00
|
|
|
movies_to_update = []
|
|
|
|
movies_to_add = []
|
2019-01-15 16:25:13 +00:00
|
|
|
|
2018-07-11 02:31:58 +00:00
|
|
|
for movie in r.json():
|
2018-12-17 04:18:53 +00:00
|
|
|
q4ws.append("Getting data for this movie: " + movie['title'])
|
2018-07-11 02:31:58 +00:00
|
|
|
if movie['hasFile'] is True:
|
|
|
|
if 'movieFile' in movie:
|
2018-11-05 11:30:50 +00:00
|
|
|
if movie["path"] != None and movie['movieFile']['relativePath'] != None:
|
2018-11-05 03:17:21 +00:00
|
|
|
try:
|
|
|
|
overview = unicode(movie['overview'])
|
|
|
|
except:
|
|
|
|
overview = ""
|
|
|
|
try:
|
|
|
|
poster_big = movie['images'][0]['url']
|
|
|
|
poster = os.path.splitext(poster_big)[0] + '-500' + os.path.splitext(poster_big)[1]
|
|
|
|
except:
|
|
|
|
poster = ""
|
|
|
|
try:
|
|
|
|
fanart = movie['images'][1]['url']
|
|
|
|
except:
|
|
|
|
fanart = ""
|
2019-01-15 16:25:13 +00:00
|
|
|
|
2018-11-05 03:17:21 +00:00
|
|
|
if 'sceneName' in movie['movieFile']:
|
|
|
|
sceneName = movie['movieFile']['sceneName']
|
|
|
|
else:
|
|
|
|
sceneName = None
|
2019-02-05 03:57:15 +00:00
|
|
|
|
|
|
|
if movie['alternativeTitles'] != None:
|
|
|
|
alternativeTitles = str([item['title'] for item in movie['alternativeTitles']])
|
2019-02-06 03:49:58 +00:00
|
|
|
|
2019-02-06 11:51:19 +00:00
|
|
|
try:
|
|
|
|
format, resolution = movie['movieFile']['quality']['quality']['name'].split('-')
|
|
|
|
except:
|
|
|
|
format = movie['movieFile']['quality']['quality']['name']
|
|
|
|
resolution = movie['movieFile']['quality']['quality']['resolution'].lstrip('r').lower()
|
|
|
|
|
2018-11-05 03:17:21 +00:00
|
|
|
# Add movies in radarr to current movies list
|
|
|
|
current_movies_radarr.append(unicode(movie['tmdbId']))
|
2019-01-15 16:25:13 +00:00
|
|
|
|
2018-11-05 03:17:21 +00:00
|
|
|
# Detect file separator
|
|
|
|
if movie['path'][0] == "/":
|
|
|
|
separator = "/"
|
2018-09-23 14:47:55 +00:00
|
|
|
else:
|
2018-11-05 03:17:21 +00:00
|
|
|
separator = "\\"
|
2019-01-15 16:25:13 +00:00
|
|
|
|
2018-11-05 03:17:21 +00:00
|
|
|
if unicode(movie['tmdbId']) in current_movies_db_list:
|
2019-01-15 16:25:13 +00:00
|
|
|
movies_to_update.append((movie["title"],
|
|
|
|
movie["path"] + separator + movie['movieFile']['relativePath'],
|
|
|
|
movie["tmdbId"], movie["id"], overview, poster, fanart,
|
|
|
|
profile_id_to_language(movie['qualityProfileId']), sceneName,
|
|
|
|
unicode(bool(movie['monitored'])), movie['sortTitle'],
|
2019-02-06 11:51:19 +00:00
|
|
|
movie['year'], alternativeTitles, format, resolution,
|
2019-02-06 03:49:58 +00:00
|
|
|
movie['movieFile']['mediaInfo']['videoCodecLibrary'],
|
|
|
|
movie['movieFile']['mediaInfo']['audioFormat'],
|
|
|
|
movie["tmdbId"]))
|
2018-09-23 14:47:55 +00:00
|
|
|
else:
|
2018-11-05 03:17:21 +00:00
|
|
|
if movie_default_enabled is True:
|
2019-01-15 16:25:13 +00:00
|
|
|
movies_to_add.append((movie["title"],
|
2019-02-05 03:57:15 +00:00
|
|
|
movie["path"] + separator + movie['movieFile']['relativePath'],
|
|
|
|
movie["tmdbId"], movie_default_language, '[]', movie_default_hi,
|
|
|
|
movie["id"], overview, poster, fanart,
|
2019-01-15 16:25:13 +00:00
|
|
|
profile_id_to_language(movie['qualityProfileId']), sceneName,
|
2019-02-05 03:57:15 +00:00
|
|
|
unicode(bool(movie['monitored'])), movie['sortTitle'],
|
2019-02-06 11:51:19 +00:00
|
|
|
movie['year'], alternativeTitles, format, resolution,
|
2019-02-06 03:49:58 +00:00
|
|
|
movie['movieFile']['mediaInfo']['videoCodecLibrary'],
|
|
|
|
movie['movieFile']['mediaInfo']['audioFormat']))
|
2018-11-05 03:17:21 +00:00
|
|
|
else:
|
2019-01-15 16:25:13 +00:00
|
|
|
movies_to_add.append((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,
|
2019-02-05 03:57:15 +00:00
|
|
|
unicode(bool(movie['monitored'])), movie['sortTitle'],
|
2019-02-06 11:51:19 +00:00
|
|
|
movie['year'], alternativeTitles, format, resolution,
|
2019-02-06 03:49:58 +00:00
|
|
|
movie['moviefile']['mediaInfo']['videoCodecLibrary'],
|
|
|
|
movie['moviefile']['mediaInfo']['audioFormat']))
|
2018-11-05 03:17:21 +00:00
|
|
|
else:
|
2019-01-15 16:25:13 +00:00
|
|
|
logging.error(
|
|
|
|
'BAZARR Radarr returned a movie without a file path: ' + movie["path"] + separator +
|
|
|
|
movie['movieFile']['relativePath'])
|
|
|
|
|
2018-09-25 02:23:47 +00:00
|
|
|
# Update or insert movies in DB
|
2018-10-31 19:34:40 +00:00
|
|
|
db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30)
|
2018-09-25 02:23:47 +00:00
|
|
|
c = db.cursor()
|
2019-01-15 16:25:13 +00:00
|
|
|
|
|
|
|
updated_result = c.executemany(
|
2019-02-06 11:51:19 +00:00
|
|
|
'''UPDATE table_movies SET title = ?, path = ?, tmdbId = ?, radarrId = ?, overview = ?, poster = ?, fanart = ?, `audio_language` = ?, sceneName = ?, monitored = ?, sortTitle = ?, year = ?, alternativeTitles = ?, format = ?, resolution = ?, video_codec = ?, audio_codec = ? WHERE tmdbid = ?''',
|
2019-01-15 16:25:13 +00:00
|
|
|
movies_to_update)
|
2018-09-25 02:23:47 +00:00
|
|
|
db.commit()
|
2019-01-15 16:25:13 +00:00
|
|
|
|
2018-09-25 02:23:47 +00:00
|
|
|
if movie_default_enabled is True:
|
2019-01-15 16:25:13 +00:00
|
|
|
added_result = c.executemany(
|
2019-02-06 11:51:19 +00:00
|
|
|
'''INSERT OR IGNORE INTO table_movies(title, path, tmdbId, languages, subtitles,`hearing_impaired`, radarrId, overview, poster, fanart, `audio_language`, sceneName, monitored, sortTitle, year, alternativeTitles, format, resolution, video_codec, audio_codec) VALUES (?,?,?,?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''',
|
2019-01-15 16:25:13 +00:00
|
|
|
movies_to_add)
|
2018-09-25 02:23:47 +00:00
|
|
|
db.commit()
|
|
|
|
else:
|
2019-01-15 16:25:13 +00:00
|
|
|
added_result = c.executemany(
|
2019-02-06 11:51:19 +00:00
|
|
|
'''INSERT OR IGNORE INTO table_movies(title, path, tmdbId, languages, subtitles,`hearing_impaired`, radarrId, overview, poster, fanart, `audio_language`, sceneName, monitored, sortTitle, year, alternativeTitles, format, resolution, video_codec, audio_codec) VALUES (?,?,?,(SELECT languages FROM table_movies WHERE tmdbId = ?), '[]',(SELECT `hearing_impaired` FROM table_movies WHERE tmdbId = ?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''',
|
2019-01-15 16:25:13 +00:00
|
|
|
movies_to_add)
|
2018-09-25 02:23:47 +00:00
|
|
|
db.commit()
|
2018-07-11 02:31:58 +00:00
|
|
|
|
|
|
|
removed_movies = list(set(current_movies_db_list) - set(current_movies_radarr))
|
|
|
|
|
2018-10-02 18:38:37 +00:00
|
|
|
for removed_movie in removed_movies:
|
|
|
|
c.execute('DELETE FROM table_movies WHERE tmdbId = ?', (removed_movie,))
|
2019-01-06 18:50:36 +00:00
|
|
|
db.commit()
|
2018-05-29 16:18:59 +00:00
|
|
|
|
2019-01-06 18:50:36 +00:00
|
|
|
# Get movies list after INSERT and UPDATE
|
|
|
|
movies_now_in_db = c.execute('SELECT tmdbId, path FROM table_movies').fetchall()
|
|
|
|
|
|
|
|
# Close database connection
|
|
|
|
db.close()
|
2018-04-19 23:45:10 +00:00
|
|
|
|
2019-01-06 18:50:36 +00:00
|
|
|
# Get only movies added or modified and store subtitles for them
|
|
|
|
altered_movies = set(movies_now_in_db).difference(set(current_movies_db))
|
|
|
|
for altered_movie in altered_movies:
|
|
|
|
store_subtitles_movie(path_replace_movie(altered_movie[1]))
|
2018-04-19 23:45:10 +00:00
|
|
|
|
2018-10-01 14:44:27 +00:00
|
|
|
logging.debug('BAZARR All movies synced from Radarr into database.')
|
2019-01-15 16:25:13 +00:00
|
|
|
|
2018-06-27 02:47:58 +00:00
|
|
|
list_missing_subtitles_movies()
|
2018-10-01 14:44:27 +00:00
|
|
|
logging.debug('BAZARR All movie missing subtitles updated in database.')
|
2019-01-15 16:25:13 +00:00
|
|
|
|
2018-12-17 04:18:53 +00:00
|
|
|
q4ws.append("Update movies list from Radarr is ended.")
|
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
|
2018-04-19 23:45:10 +00:00
|
|
|
def get_profile_list():
|
2018-12-15 00:36:28 +00:00
|
|
|
apikey_radarr = settings.radarr.apikey
|
2019-01-15 16:25:13 +00:00
|
|
|
|
2018-04-19 23:45:10 +00:00
|
|
|
# Get profiles data from radarr
|
|
|
|
global profiles_list
|
|
|
|
profiles_list = []
|
2019-01-15 16:25:13 +00:00
|
|
|
|
2018-07-11 02:31:58 +00:00
|
|
|
url_radarr_api_movies = url_radarr + "/api/profile?apikey=" + apikey_radarr
|
|
|
|
try:
|
2018-09-02 13:48:50 +00:00
|
|
|
profiles_json = requests.get(url_radarr_api_movies, timeout=15, verify=False)
|
2018-07-11 02:31:58 +00:00
|
|
|
except requests.exceptions.ConnectionError as errc:
|
2018-10-19 03:33:01 +00:00
|
|
|
logging.exception("BAZARR Error trying to get profiles from Radarr. Connection Error.")
|
2018-07-11 02:31:58 +00:00
|
|
|
except requests.exceptions.Timeout as errt:
|
2018-10-19 03:33:01 +00:00
|
|
|
logging.exception("BAZARR Error trying to get profiles from Radarr. Timeout Error.")
|
2018-07-11 02:31:58 +00:00
|
|
|
except requests.exceptions.RequestException as err:
|
2018-10-19 03:33:01 +00:00
|
|
|
logging.exception("BAZARR Error trying to get profiles from Radarr.")
|
2018-07-11 02:31:58 +00:00
|
|
|
else:
|
|
|
|
# Parsing data returned from radarr
|
|
|
|
for profile in profiles_json.json():
|
|
|
|
profiles_list.append([profile['id'], profile['language'].capitalize()])
|
2018-04-19 23:45:10 +00:00
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
|
2018-04-19 23:45:10 +00:00
|
|
|
def profile_id_to_language(id):
|
|
|
|
for profile in profiles_list:
|
|
|
|
if id == profile[0]:
|
|
|
|
return profile[1]
|
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
|
2018-04-19 23:45:10 +00:00
|
|
|
if __name__ == '__main__':
|
2018-08-06 00:41:03 +00:00
|
|
|
update_movies()
|