pull/684/head
Louis Vézina 5 years ago
parent 2b8bca97f4
commit cf44d71379

@ -5,3 +5,30 @@ from get_args import args
from helper import path_replace, path_replace_movie, path_replace_reverse, path_replace_reverse_movie
database = Sqlite3Worker(os.path.join(args.config_dir, 'db', 'bazarr.db'), max_queue_size=256)
class SqliteDictConverter:
def __init__(self):
self.keys = str()
self.values = str()
self.items = str()
def convert(self, values_dict):
if type(values_dict) is dict:
for key, value in values_dict.items():
self.keys += key + ", "
if type(value) is not str:
value = str(value)
else:
value = "'" + value + "'"
self.values += value + ", "
self.items += key + "=" + value + ", "
self.keys = self.keys.rstrip(", ")
self.values = self.values.rstrip(", ")
self.items = self.items.rstrip(", ")
return self
else:
pass
dict_converter = SqliteDictConverter()

@ -4,7 +4,7 @@ import requests
import logging
import re
from queueconfig import notifications
from database import TableShows, TableEpisodes, wal_cleaning
from database import database, dict_converter
from get_args import args
from config import settings, url_sonarr
@ -16,7 +16,6 @@ from get_subtitle import episode_download_subtitles
def update_all_episodes():
series_full_scan_subtitles()
logging.info('BAZARR All existing episode subtitles indexed from disk.')
wal_cleaning()
def sync_episodes():
@ -25,11 +24,7 @@ def sync_episodes():
apikey_sonarr = settings.sonarr.apikey
# Get current episodes id in DB
current_episodes_db = TableEpisodes.select(
TableEpisodes.sonarr_episode_id,
TableEpisodes.path,
TableEpisodes.sonarr_series_id
)
current_episodes_db = database.execute("SELECT sonarrEpisodeId, path, sonarrSeriesId FROM table_episodes")
current_episodes_db_list = [x.sonarr_episode_id for x in current_episodes_db]
@ -39,16 +34,13 @@ def sync_episodes():
altered_episodes = []
# Get sonarrId for each series from database
seriesIdList = TableShows.select(
TableShows.sonarr_series_id,
TableShows.title
)
seriesIdList = database.execute("SELECT sonarrSeriesId, title FROM table_shows")
seriesIdListLength = seriesIdList.count()
for i, seriesId in enumerate(seriesIdList, 1):
notifications.write(msg='Getting episodes data from Sonarr...', queue='get_episodes', item=i, length=seriesIdListLength)
# Get episodes data for a series from Sonarr
url_sonarr_api_episode = url_sonarr + "/api/episode?seriesId=" + str(seriesId.sonarr_series_id) + "&apikey=" + apikey_sonarr
url_sonarr_api_episode = url_sonarr + "/api/episode?seriesId=" + str(seriesId['sonarr_series_id']) + "&apikey=" + apikey_sonarr
try:
r = requests.get(url_sonarr_api_episode, timeout=60, verify=False)
r.raise_for_status()
@ -103,8 +95,8 @@ def sync_episodes():
current_episodes_sonarr.append(episode['id'])
if episode['id'] in current_episodes_db_list:
episodes_to_update.append({'sonarr_series_id': episode['seriesId'],
'sonarr_episode_id': episode['id'],
episodes_to_update.append({'sonarrSeriesId': episode['seriesId'],
'sonarrEpisodeId': episode['id'],
'title': episode['title'],
'path': episode['episodeFile']['path'],
'season': episode['seasonNumber'],
@ -117,8 +109,8 @@ def sync_episodes():
'audio_codec': audioCodec,
'episode_file_id': episode['episodeFile']['id']})
else:
episodes_to_add.append({'sonarr_series_id': episode['seriesId'],
'sonarr_episode_id': episode['id'],
episodes_to_add.append({'sonarrSeriesId': episode['seriesId'],
'sonarrEpisodeId': episode['id'],
'title': episode['title'],
'path': episode['episodeFile']['path'],
'season': episode['seasonNumber'],
@ -133,21 +125,9 @@ def sync_episodes():
# Update existing episodes in DB
episode_in_db_list = []
episodes_in_db = TableEpisodes.select(
TableEpisodes.sonarr_series_id,
TableEpisodes.sonarr_episode_id,
TableEpisodes.title,
TableEpisodes.path,
TableEpisodes.season,
TableEpisodes.episode,
TableEpisodes.scene_name,
TableEpisodes.monitored,
TableEpisodes.format,
TableEpisodes.resolution,
TableEpisodes.video_codec,
TableEpisodes.audio_codec,
TableEpisodes.episode_file_id
).dicts()
episodes_in_db = database.execute("SELECT sonarrSeriesId, sonarrEpisodeId, title, path, season, episode, "
"scene_name, monitored, format, resolution, video_codec, audio_codec, "
"episode_file_id FROM table_episodes")
for item in episodes_in_db:
episode_in_db_list.append(item)
@ -155,20 +135,18 @@ def sync_episodes():
episodes_to_update_list = [i for i in episodes_to_update if i not in episode_in_db_list]
for updated_episode in episodes_to_update_list:
TableEpisodes.update(
updated_episode
).where(
TableEpisodes.sonarr_episode_id == updated_episode['sonarr_episode_id']
).execute()
query = dict_converter.convert(updated_episode)
database.execute("UPDATE table_episodes SET ? WHERE sonarrEpisodeId=?",
(query.items, updated_episode['sonarr_episode_id']))
altered_episodes.append([updated_episode['sonarr_episode_id'],
updated_episode['path'],
updated_episode['sonarr_series_id']])
# Insert new episodes in DB
for added_episode in episodes_to_add:
TableEpisodes.insert(
added_episode
).on_conflict_ignore().execute()
query = dict_converter.convert(added_episode)
database.execute("INSERT OR IGNORE INTO table_episodes(?) VALUES(?)",
(query.keys, query.values))
altered_episodes.append([added_episode['sonarr_episode_id'],
added_episode['path']])
@ -176,9 +154,7 @@ def sync_episodes():
removed_episodes = list(set(current_episodes_db_list) - set(current_episodes_sonarr))
for removed_episode in removed_episodes:
TableEpisodes.delete().where(
TableEpisodes.sonarr_episode_id == removed_episode
).execute()
database.execute("DELETE FROM table_episodes WHERE sonarrEpisodeId=?", (removed_episode,))
# Store subtitles for added or modified episodes
for i, altered_episode in enumerate(altered_episodes, 1):

@ -12,13 +12,12 @@ from utils import get_radarr_version
from list_subtitles import store_subtitles_movie, list_missing_subtitles_movies, movies_full_scan_subtitles
from get_subtitle import movies_download_subtitles
from database import TableMovies, wal_cleaning
from database import database, dict_converter
def update_all_movies():
movies_full_scan_subtitles()
logging.info('BAZARR All existing movie subtitles indexed from disk.')
wal_cleaning()
def update_movies():
@ -53,13 +52,9 @@ def update_movies():
return
else:
# Get current movies in DB
current_movies_db = TableMovies.select(
TableMovies.tmdb_id,
TableMovies.path,
TableMovies.radarr_id
)
current_movies_db = database.execute("SELECT tmdbId, path, radarrId FROM table_movies")
current_movies_db_list = [x.tmdb_id for x in current_movies_db]
current_movies_db_list = [x['tmdb_id'] for x in current_movies_db]
current_movies_radarr = []
movies_to_update = []
@ -208,27 +203,10 @@ def update_movies():
# Update or insert movies in DB
movies_in_db_list = []
movies_in_db = TableMovies.select(
TableMovies.radarr_id,
TableMovies.title,
TableMovies.path,
TableMovies.tmdb_id,
TableMovies.overview,
TableMovies.poster,
TableMovies.fanart,
TableMovies.audio_language,
TableMovies.scene_name,
TableMovies.monitored,
TableMovies.sort_title,
TableMovies.year,
TableMovies.alternative_titles,
TableMovies.format,
TableMovies.resolution,
TableMovies.video_codec,
TableMovies.audio_codec,
TableMovies.imdb_id,
TableMovies.movie_file_id
).dicts()
movies_in_db = database.execute("SELECT radarrId, title, path, tmdbId, overview, poster, fanart, "
"audio_language, sceneName, monitored, sortTitle, year, "
"alternativeTitles, format, resolution, video_codec, audio_codec, imdbId,"
"movie_file_id FROM table_movies")
for item in movies_in_db:
movies_in_db_list.append(item)
@ -236,11 +214,9 @@ def update_movies():
movies_to_update_list = [i for i in movies_to_update if i not in movies_in_db_list]
for updated_movie in movies_to_update_list:
TableMovies.update(
updated_movie
).where(
TableMovies.radarr_id == updated_movie['radarr_id']
).execute()
query = dict_converter.convert(updated_movie)
database.execute("UPDATE table_movies SET ? WHERE radarrId=?",
(query.items, updated_movie['radarr_id']))
altered_movies.append([updated_movie['tmdb_id'],
updated_movie['path'],
updated_movie['radarr_id'],
@ -248,9 +224,9 @@ def update_movies():
# Insert new movies in DB
for added_movie in movies_to_add:
TableMovies.insert(
added_movie
).on_conflict_ignore().execute()
query = dict_converter.convert(updated_movie)
database.execute("INSERT OR IGNORE INTO table_movies(?) VALUES(?)",
(query.keys, query.values))
altered_movies.append([added_movie['tmdb_id'],
added_movie['path'],
added_movie['radarr_id'],
@ -260,9 +236,7 @@ def update_movies():
removed_movies = list(set(current_movies_db_list) - set(current_movies_radarr))
for removed_movie in removed_movies:
TableMovies.delete().where(
TableMovies.tmdb_id == removed_movie
).execute()
database.execute("DELETE FROM table_movies WHERE tmdbId=?", (removed_movie,))
# Store subtitles for added or modified movies
for i, altered_movie in enumerate(altered_movies, 1):

@ -10,7 +10,7 @@ import datetime
from get_args import args
from config import settings, url_sonarr
from list_subtitles import list_missing_subtitles
from database import TableShows
from database import database, dict_converter
from utils import get_sonarr_version
@ -47,9 +47,7 @@ def update_series():
return
else:
# Get current shows in DB
current_shows_db = TableShows.select(
TableShows.tvdb_id
)
current_shows_db = database.execute("SELECT tvdbId FROM table_shows")
current_shows_db_list = [x.tvdb_id for x in current_shows_db]
current_shows_sonarr = []
@ -125,19 +123,8 @@ def update_series():
# Update existing series in DB
series_in_db_list = []
series_in_db = TableShows.select(
TableShows.title,
TableShows.path,
TableShows.tvdb_id,
TableShows.sonarr_series_id,
TableShows.overview,
TableShows.poster,
TableShows.fanart,
TableShows.audio_language,
TableShows.sort_title,
TableShows.year,
TableShows.alternate_titles
).dicts()
series_in_db = database.execute("SELECT title, path, tvdbId, sonarrSeriesId, overview, poster, fanart, "
"audio_language, sortTitle, year, alternateTitles FROM table_shows")
for item in series_in_db:
series_in_db_list.append(item)
@ -145,26 +132,22 @@ def update_series():
series_to_update_list = [i for i in series_to_update if i not in series_in_db_list]
for updated_series in series_to_update_list:
TableShows.update(
updated_series
).where(
TableShows.sonarr_series_id == updated_series['sonarr_series_id']
).execute()
query = dict_converter.convert(updated_series)
database.execute("UPDATE table_shows SET ? WHERE sonarrSeriesId=?",
(query.items, updated_series['sonarr_series_id']))
# Insert new series in DB
for added_series in series_to_add:
TableShows.insert(
added_series
).on_conflict_ignore().execute()
query = dict_converter.convert(added_series)
database.execute("INSERT OR IGNORE INTO table_shows(?) VALUES(?)",
(query.keys, query.values))
list_missing_subtitles(no=added_series['sonarr_series_id'])
# Remove old series from DB
removed_series = list(set(current_shows_db_list) - set(current_shows_sonarr))
for series in removed_series:
TableShows.delete().where(
TableShows.tvdb_id == series
).execute()
database.execute("DELETE FROM table_shows WHERE tvdbId=?",(series,))
logging.debug('BAZARR All series synced from Sonarr into database.')

Loading…
Cancel
Save