bazarr/get_episodes.py

148 lines
6.2 KiB
Python
Raw Normal View History

2017-10-20 12:59:21 +00:00
import os
2017-09-16 00:49:46 +00:00
import sqlite3
import requests
2017-12-06 04:07:37 +00:00
import logging
2017-09-16 00:49:46 +00:00
2017-10-16 23:27:19 +00:00
from get_general_settings import *
2017-09-28 01:55:21 +00:00
from list_subtitles import *
2017-10-03 02:59:45 +00:00
def update_all_episodes():
from get_sonarr_settings import get_sonarr_settings
url_sonarr = get_sonarr_settings()[0]
url_sonarr_short = get_sonarr_settings()[1]
apikey_sonarr = get_sonarr_settings()[2]
2017-10-03 02:59:45 +00:00
# Open database connection
2017-12-05 00:01:10 +00:00
db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30)
2017-10-03 02:59:45 +00:00
c = db.cursor()
2017-09-16 00:49:46 +00:00
2017-10-03 02:59:45 +00:00
# Get Sonarr API URL from database config table
c.execute('''SELECT * FROM table_settings_sonarr''')
config_sonarr = c.fetchone()
if config_sonarr[3] == 1:
protocol_sonarr = "https"
else:
protocol_sonarr = "http"
if config_sonarr[2] == "":
base_url_sonarr = ""
else:
base_url_sonarr = "/" + config_sonarr[2].strip("/")
apikey_sonarr = config_sonarr[4]
2017-09-16 00:49:46 +00:00
2017-10-16 23:27:19 +00:00
# Get current episodes id in DB
current_episodes_db = c.execute('SELECT sonarrEpisodeId FROM table_episodes').fetchall()
current_episodes_db_list = [x[0] for x in current_episodes_db]
2017-10-03 02:59:45 +00:00
# Get sonarrId for each series from database
2017-10-16 23:27:19 +00:00
current_episodes_sonarr = []
2017-10-03 02:59:45 +00:00
c.execute("SELECT sonarrSeriesId FROM table_shows")
seriesIdList = c.fetchall()
for seriesId in seriesIdList:
# Get episodes data for a series from Sonarr
url_sonarr_api_episode = protocol_sonarr + "://" + config_sonarr[0] + ":" + str(config_sonarr[1]) + base_url_sonarr + "/api/episode?seriesId=" + str(seriesId[0]) + "&apikey=" + apikey_sonarr
2017-10-03 02:59:45 +00:00
r = requests.get(url_sonarr_api_episode)
for episode in r.json():
if episode['hasFile'] and episode['episodeFile']['size'] > 20480:
2017-10-16 23:27:19 +00:00
# Add shows in Sonarr to current shows list
current_episodes_sonarr.append(episode['id'])
2017-10-03 02:59:45 +00:00
try:
c.execute('''INSERT INTO table_episodes(sonarrSeriesId, sonarrEpisodeId, title, path, season, episode) VALUES (?, ?, ?, ?, ?, ?)''', (episode['seriesId'], episode['id'], episode['title'], episode['episodeFile']['path'], episode['seasonNumber'], episode['episodeNumber']))
2017-12-05 03:59:30 +00:00
db.commit()
2017-10-03 02:59:45 +00:00
except sqlite3.Error:
2017-10-16 23:27:19 +00:00
c.execute('''UPDATE table_episodes SET sonarrSeriesId = ?, sonarrEpisodeId = ?, title = ?, path = ?, season = ?, episode = ? WHERE sonarrEpisodeId = ?''', (episode['seriesId'], episode['id'], episode['title'], episode['episodeFile']['path'], episode['seasonNumber'], episode['episodeNumber'], episode['id']))
2017-12-05 03:59:30 +00:00
db.commit()
2017-10-03 02:59:45 +00:00
else:
continue
continue
2017-09-16 00:49:46 +00:00
2017-10-16 23:27:19 +00:00
# Delete episodes not in Sonarr anymore
deleted_items = []
for item in current_episodes_db_list:
if item not in current_episodes_sonarr:
deleted_items.append(tuple([item]))
c.executemany('DELETE FROM table_episodes WHERE sonarrEpisodeId = ?',deleted_items)
2017-10-03 02:59:45 +00:00
db.commit()
2017-09-16 00:49:46 +00:00
2017-10-03 02:59:45 +00:00
# Close database connection
c.close()
2017-12-06 04:07:37 +00:00
logging.info('All episodes updated in database.')
2017-11-16 04:27:30 +00:00
2017-10-16 23:27:19 +00:00
# Store substitles for all episodes
full_scan_subtitles()
2017-12-06 04:07:37 +00:00
logging.info('All existing subtitles indexed from disk.')
2017-10-16 23:27:19 +00:00
list_missing_subtitles()
2017-12-06 04:07:37 +00:00
logging.info('All missing subtitles updated in database.')
2017-10-16 23:27:19 +00:00
def add_new_episodes():
from get_sonarr_settings import get_sonarr_settings
url_sonarr = get_sonarr_settings()[0]
url_sonarr_short = get_sonarr_settings()[1]
apikey_sonarr = get_sonarr_settings()[2]
2017-10-16 23:27:19 +00:00
# Open database connection
2017-12-05 00:01:10 +00:00
db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30)
2017-10-16 23:27:19 +00:00
c = db.cursor()
# Get Sonarr API URL from database config table
c.execute('''SELECT * FROM table_settings_sonarr''')
config_sonarr = c.fetchone()
if config_sonarr[3] == 1:
protocol_sonarr = "https"
else:
protocol_sonarr = "http"
if config_sonarr[2] == "":
base_url_sonarr = ""
else:
base_url_sonarr = "/" + config_sonarr[2].strip("/")
apikey_sonarr = config_sonarr[4]
2017-10-16 23:27:19 +00:00
if apikey_sonarr == None:
# Close database connection
c.close()
pass
else:
# Get current episodes in DB
current_episodes_db = c.execute('SELECT sonarrEpisodeId FROM table_episodes').fetchall()
current_episodes_db_list = [x[0] for x in current_episodes_db]
current_episodes_sonarr = []
# Get sonarrId for each series from database
c.execute("SELECT sonarrSeriesId FROM table_shows")
seriesIdList = c.fetchall()
for seriesId in seriesIdList:
# Get episodes data for a series from Sonarr
url_sonarr_api_episode = protocol_sonarr + "://" + config_sonarr[0] + ":" + str(config_sonarr[1]) + base_url_sonarr + "/api/episode?seriesId=" + str(seriesId[0]) + "&apikey=" + apikey_sonarr
r = requests.get(url_sonarr_api_episode)
for episode in r.json():
if episode['hasFile'] and episode['episodeFile']['size'] > 20480:
# Add shows in Sonarr to current shows list
current_episodes_sonarr.append(episode['id'])
try:
c.execute('''INSERT INTO table_episodes(sonarrSeriesId, sonarrEpisodeId, title, path, season, episode) VALUES (?, ?, ?, ?, ?, ?)''', (episode['seriesId'], episode['id'], episode['title'], episode['episodeFile']['path'], episode['seasonNumber'], episode['episodeNumber']))
except:
pass
db.commit()
# Delete episodes not in Sonarr anymore
deleted_items = []
for item in current_episodes_db_list:
if item not in current_episodes_sonarr:
deleted_items.append(tuple([item]))
c.executemany('DELETE FROM table_episodes WHERE sonarrEpisodeId = ?',deleted_items)
# Commit changes to database table
2017-10-16 23:27:19 +00:00
db.commit()
# Close database connection
c.close()
2017-11-16 04:27:30 +00:00
# Store substitles from episodes we've just added
new_scan_subtitles()
try:
list_missing_subtitles()
except:
pass