Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
morpheus65535 committed Oct 1, 2018
2 parents c576e36 + fc34550 commit 7c70564
Show file tree
Hide file tree
Showing 16 changed files with 308 additions and 339 deletions.
165 changes: 36 additions & 129 deletions bazarr.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bazarr_version = '0.6.4'
bazarr_version = '0.6.5'

import gc
gc.enable()
Expand Down Expand Up @@ -85,7 +85,8 @@ def configure_logging():
import time

from get_languages import load_language_in_db, language_from_alpha3
from get_providers import *
from get_providers import load_providers, get_providers, get_providers_auth
load_providers()

from get_series import *
from get_episodes import *
Expand Down Expand Up @@ -312,26 +313,29 @@ 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 + 'search_json/<query>', method='GET')
@custom_auth_basic(check_credentials)
def search_json(query):
authorize()
db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
c = db.cursor()

c.execute("SELECT title, sonarrSeriesId FROM table_shows WHERE title LIKE ? ORDER BY title", ('%'+query+'%',))
series = c.fetchall()

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]))]))

if get_general_settings()[12] is True:
c.execute("SELECT title, sonarrSeriesId FROM table_shows WHERE title LIKE ? ORDER BY title",
('%' + query + '%',))
series = c.fetchall()
for serie in series:
search_list.append(dict([('name', serie[0]), ('url', base_url + 'episodes/' + str(serie[1]))]))

if get_general_settings()[13] is True:
c.execute("SELECT title, radarrId FROM table_movies WHERE title LIKE ? ORDER BY title", ('%' + query + '%',))
movies = c.fetchall()
for movie in movies:
search_list.append(dict([('name', movie[0]), ('url', base_url + 'movie/' + str(movie[1]))]))
c.close()

response.content_type = 'application/json'
return dict(items=search_list)

Expand Down Expand Up @@ -796,6 +800,8 @@ def save_settings():
settings_general_ip = request.forms.get('settings_general_ip')
settings_general_port = request.forms.get('settings_general_port')
settings_general_baseurl = request.forms.get('settings_general_baseurl')
if settings_general_baseurl.endswith('/') is False:
settings_general_baseurl += '/'
settings_general_loglevel = request.forms.get('settings_general_loglevel')
settings_general_sourcepath = request.forms.getall('settings_general_sourcepath')
settings_general_destpath = request.forms.getall('settings_general_destpath')
Expand Down Expand Up @@ -1473,28 +1479,8 @@ def get_subtitle():
sonarrEpisodeId = request.forms.get('sonarrEpisodeId')
# tvdbid = request.forms.get('tvdbid')

db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
c = db.cursor()
c.execute("SELECT * FROM table_settings_providers WHERE enabled = 1")
enabled_providers = c.fetchall()
c.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
providers_list = get_providers()
providers_auth = get_providers_auth()

try:
result = download_subtitle(episodePath, language, hi, providers_list, providers_auth, sceneName, 'series')
Expand All @@ -1518,28 +1504,8 @@ def manual_search_json():
language = request.forms.get('language')
hi = request.forms.get('hi')

db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
c = db.cursor()
c.execute("SELECT * FROM table_settings_providers WHERE enabled = 1")
enabled_providers = c.fetchall()
c.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
providers_list = get_providers()
providers_auth = get_providers_auth()

data = manual_search(episodePath, language, hi, providers_list, providers_auth, sceneName, 'series')
return dict(data=data)
Expand All @@ -1559,19 +1525,8 @@ def manual_get_subtitle():
sonarrSeriesId = request.forms.get('sonarrSeriesId')
sonarrEpisodeId = request.forms.get('sonarrEpisodeId')

db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
c = db.cursor()
provider = c.execute("SELECT * FROM table_settings_providers WHERE name = ?",(selected_provider,)).fetchone()
c.close()
providers_auth = {}
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
providers_list = get_providers()
providers_auth = get_providers_auth()

try:
result = manual_download_subtitle(episodePath, language, hi, subtitle, selected_provider, providers_auth, sceneName, 'series')
Expand All @@ -1597,28 +1552,8 @@ def get_subtitle_movie():
radarrId = request.forms.get('radarrId')
# tmdbid = request.forms.get('tmdbid')

db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
c = db.cursor()
c.execute("SELECT * FROM table_settings_providers WHERE enabled = 1")
enabled_providers = c.fetchall()
c.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
providers_list = get_providers()
providers_auth = get_providers_auth()

try:
result = download_subtitle(moviePath, language, hi, providers_list, providers_auth, sceneName, 'movie')
Expand All @@ -1642,28 +1577,8 @@ def manual_search_movie_json():
language = request.forms.get('language')
hi = request.forms.get('hi')

db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
c = db.cursor()
c.execute("SELECT * FROM table_settings_providers WHERE enabled = 1")
enabled_providers = c.fetchall()
c.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
providers_list = get_providers()
providers_auth = get_providers_auth()

data = manual_search(moviePath, language, hi, providers_list, providers_auth, sceneName, 'movie')
return dict(data=data)
Expand All @@ -1682,19 +1597,8 @@ def manual_get_subtitle_movie():
subtitle = request.forms.get('subtitle')
radarrId = request.forms.get('radarrId')

db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
c = db.cursor()
provider = c.execute("SELECT * FROM table_settings_providers WHERE name = ?",(selected_provider,)).fetchone()
c.close()
providers_auth = {}
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
providers_list = get_providers()
providers_auth = get_providers_auth()

try:
result = manual_download_subtitle(moviePath, language, hi, subtitle, selected_provider, providers_auth, sceneName, 'movie')
Expand Down Expand Up @@ -1756,6 +1660,9 @@ def test_url(protocol, url):
else:
return dict(status=True, version=result)

import warnings
# Mute DeprecationWarning
warnings.simplefilter("ignore", DeprecationWarning)

logging.info('Bazarr is started and waiting for request on http://' + str(ip) + ':' + str(port) + str(base_url))
run(host=ip, port=port, server='waitress', app=app)
Expand Down
48 changes: 33 additions & 15 deletions get_episodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,19 @@ def sync_episodes():
c = db.cursor()

# 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()
current_episodes_db = c.execute('SELECT sonarrEpisodeId FROM table_episodes').fetchall()

# Get sonarrId for each series from database
current_episodes_db_list = [x[0] for x in current_episodes_db]
current_episodes_sonarr = []
episodes_to_update = []
episodes_to_add = []

# Get sonarrId for each series from database
seriesIdList = c.execute("SELECT sonarrSeriesId FROM table_shows").fetchall()

# Close database connection
c.close()

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
Expand All @@ -61,27 +69,37 @@ def sync_episodes():
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']))))

added_episodes = list(set(current_episodes_sonarr) - set(current_episodes_db))
removed_episodes = list(set(current_episodes_db) - set(current_episodes_sonarr))
# Add episodes in sonarr to current episode list
current_episodes_sonarr.append(episode['id'])

if episode['id'] in current_episodes_db_list:
episodes_to_update.append((episode['title'], episode['episodeFile']['path'], episode['seasonNumber'], episode['episodeNumber'], sceneName, str(bool(episode['monitored'])), episode['id']))
else:
episodes_to_add.append((episode['seriesId'], episode['id'], episode['title'], episode['episodeFile']['path'], episode['seasonNumber'], episode['episodeNumber'], sceneName, str(bool(episode['monitored']))))

removed_episodes = list(set(current_episodes_db_list) - set(current_episodes_sonarr))

# Update or insert movies in DB
db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
c = db.cursor()

updated_result = c.executemany('''UPDATE table_episodes SET title = ?, path = ?, season = ?, episode = ?, scene_name = ?, monitored = ? WHERE sonarrEpisodeId = ?''', episodes_to_update)
db.commit()

added_result = c.executemany('''INSERT OR IGNORE INTO table_episodes(sonarrSeriesId, sonarrEpisodeId, title, path, season, episode, scene_name, monitored) VALUES (?, ?, ?, ?, ?, ?, ?, ?)''', episodes_to_add)
db.commit()

for removed_episode in removed_episodes:
c.execute('DELETE FROM table_episodes WHERE sonarrEpisodeId = ?', (removed_episode[1],))
c.execute('DELETE FROM table_episodes WHERE sonarrEpisodeId = ?', (removed_episode,))
db.commit()

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()

for added_episode in episodes_to_add:
store_subtitles(path_replace(added_episode[3]))

logging.debug('All episodes synced from Sonarr into database.')

list_missing_subtitles()
Expand Down
Loading

0 comments on commit 7c70564

Please sign in to comment.