bazarr/get_episodes.py

87 lines
4.2 KiB
Python
Raw Normal View History

from get_argv import config_dir
2017-10-20 20:59:21 +08:00
import os
2017-09-16 08:49:46 +08:00
import sqlite3
import requests
2017-12-06 12:07:37 +08:00
import logging
2017-09-16 08:49:46 +08:00
2018-08-16 04:51:46 +08:00
from get_general_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():
from get_sonarr_settings import get_sonarr_settings
url_sonarr = get_sonarr_settings()[0]
apikey_sonarr = get_sonarr_settings()[2]
2017-10-03 10:59:45 +08:00
# Open database connection
db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
2017-10-03 10:59:45 +08:00
c = db.cursor()
2017-09-16 08:49:46 +08:00
2017-10-17 07:27:19 +08:00
# Get current episodes id in DB
current_episodes_db = c.execute('SELECT sonarrSeriesId, sonarrEpisodeId, title, path, season, episode, scene_name, monitored FROM table_episodes').fetchall()
2017-10-03 10:59:45 +08:00
# Get sonarrId for each series from database
2017-10-17 07:27:19 +08:00
current_episodes_sonarr = []
seriesIdList = c.execute("SELECT sonarrSeriesId FROM table_shows").fetchall()
2017-10-03 10:59:45 +08:00
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)
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
current_episodes_sonarr.append((episode['seriesId'], episode['id'], episode['title'], episode['episodeFile']['path'], episode['seasonNumber'], episode['episodeNumber'], sceneName, str(bool(episode['monitored']))))
2017-09-16 08:49:46 +08:00
added_episodes = list(set(current_episodes_sonarr) - set(current_episodes_db))
removed_episodes = list(set(current_episodes_db) - set(current_episodes_sonarr))
2017-10-17 07:27:19 +08:00
for removed_episode in removed_episodes:
c.execute('DELETE FROM table_episodes WHERE sonarrEpisodeId = ?', (removed_episode[1],))
db.commit()
2017-10-17 07:27:19 +08:00
for added_episode in added_episodes:
try:
c.execute('''INSERT INTO table_episodes(sonarrSeriesId, sonarrEpisodeId, title, path, season, episode, scene_name, monitored) VALUES (?, ?, ?, ?, ?, ?, ?, ?)''', added_episode)
except sqlite3.IntegrityError as e:
logging.exception("You're probably an early adopter of Bazarr and this is a known issue. Please open an issue on Github and we'll fix this.")
else:
db.commit()
store_subtitles(path_replace(added_episode[3]))
# Close database connection
c.close()
logging.debug('All episodes synced from Sonarr into database.')
2017-10-17 07:27:19 +08:00
list_missing_subtitles()
logging.debug('All missing subtitles updated in database.')