2017-09-17 00:11:47 +00:00
|
|
|
import os
|
2017-10-16 23:27:19 +00:00
|
|
|
import sqlite3
|
|
|
|
import ast
|
2017-12-06 04:07:37 +00:00
|
|
|
import logging
|
2017-09-17 00:11:47 +00:00
|
|
|
from babelfish import *
|
|
|
|
from subliminal import *
|
2017-09-17 13:02:16 +00:00
|
|
|
from pycountry import *
|
2017-10-16 23:27:19 +00:00
|
|
|
from get_general_settings import *
|
|
|
|
from list_subtitles import *
|
|
|
|
from utils import *
|
2018-01-23 04:25:58 +00:00
|
|
|
from notifier import send_notifications
|
2017-09-17 00:11:47 +00:00
|
|
|
|
|
|
|
# configure the cache
|
2018-01-16 04:30:41 +00:00
|
|
|
region.configure('dogpile.cache.memory')
|
2017-09-17 00:11:47 +00:00
|
|
|
|
2017-12-08 01:03:32 +00:00
|
|
|
def download_subtitle(path, language, hi, providers, providers_auth):
|
2017-11-16 18:39:47 +00:00
|
|
|
try:
|
2018-01-01 19:29:38 +00:00
|
|
|
video = scan_video(path)
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception('Error trying to extract information from this filename: ' + path)
|
2017-11-16 18:39:47 +00:00
|
|
|
return None
|
2018-01-01 19:29:38 +00:00
|
|
|
else:
|
|
|
|
try:
|
2018-01-05 14:58:32 +00:00
|
|
|
best_subtitles = download_best_subtitles([video], {Language(language)}, providers=providers, hearing_impaired=hi, provider_configs=providers_auth)
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception('Error trying to best subtitles for this file: ' + path)
|
|
|
|
return None
|
|
|
|
else:
|
2018-01-05 15:37:32 +00:00
|
|
|
try:
|
2018-01-05 15:41:08 +00:00
|
|
|
best_subtitle = best_subtitles[video][0]
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return None
|
2018-01-05 15:37:32 +00:00
|
|
|
else:
|
2018-01-10 16:44:47 +00:00
|
|
|
single = get_general_settings()[7]
|
2018-01-05 15:41:08 +00:00
|
|
|
try:
|
2018-01-10 16:44:47 +00:00
|
|
|
if single == 'True':
|
|
|
|
result = save_subtitles(video, [best_subtitle], single=True, encoding='utf-8')
|
|
|
|
else:
|
|
|
|
result = save_subtitles(video, [best_subtitle], encoding='utf-8')
|
2018-01-05 15:41:08 +00:00
|
|
|
except:
|
|
|
|
logging.error('Error saving subtitles file to disk.')
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
downloaded_provider = str(result[0]).strip('<>').split(' ')[0][:-8]
|
|
|
|
downloaded_language = pycountry.languages.lookup(str(str(result[0]).strip('<>').split(' ')[2].strip('[]'))).name
|
|
|
|
message = downloaded_language + " subtitles downloaded from " + downloaded_provider + "."
|
2018-01-23 04:25:58 +00:00
|
|
|
|
2018-01-05 15:41:08 +00:00
|
|
|
return message
|
2017-10-16 23:27:19 +00:00
|
|
|
|
|
|
|
def series_download_subtitles(no):
|
2017-12-08 01:03:32 +00:00
|
|
|
conn_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 = conn_db.cursor()
|
2017-11-21 20:42:18 +00:00
|
|
|
episodes_details = c_db.execute("SELECT path, missing_subtitles, sonarrEpisodeId FROM table_episodes WHERE sonarrSeriesId = ?", (no,)).fetchall()
|
2017-10-16 23:27:19 +00:00
|
|
|
series_details = c_db.execute("SELECT hearing_impaired FROM table_shows WHERE sonarrSeriesId = ?", (no,)).fetchone()
|
2017-12-08 01:03:32 +00:00
|
|
|
enabled_providers = c_db.execute("SELECT * FROM table_settings_providers WHERE enabled = 1").fetchall()
|
2017-10-16 23:27:19 +00:00
|
|
|
c_db.close()
|
|
|
|
|
|
|
|
providers_list = []
|
2017-12-08 01:03:32 +00:00
|
|
|
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
|
2017-10-16 23:27:19 +00:00
|
|
|
|
|
|
|
for episode in episodes_details:
|
|
|
|
for language in ast.literal_eval(episode[1]):
|
2017-12-08 01:03:32 +00:00
|
|
|
message = download_subtitle(path_replace(episode[0]), str(pycountry.languages.lookup(language).alpha_3), series_details[0], providers_list, providers_auth)
|
2017-10-16 23:27:19 +00:00
|
|
|
if message is not None:
|
|
|
|
store_subtitles(path_replace(episode[0]))
|
|
|
|
history_log(1, no, episode[2], message)
|
2018-01-23 04:25:58 +00:00
|
|
|
send_notifications(no, episode[2], message)
|
2017-11-16 18:42:23 +00:00
|
|
|
list_missing_subtitles(no)
|
2017-10-16 23:27:19 +00:00
|
|
|
|
|
|
|
def wanted_download_subtitles(path):
|
2017-12-05 00:01:10 +00:00
|
|
|
conn_db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30)
|
2017-11-16 19:09:40 +00:00
|
|
|
c_db = conn_db.cursor()
|
|
|
|
episodes_details = c_db.execute("SELECT table_episodes.path, table_episodes.missing_subtitles, table_episodes.sonarrEpisodeId, table_episodes.sonarrSeriesId, table_shows.hearing_impaired FROM table_episodes INNER JOIN table_shows on table_shows.sonarrSeriesId = table_episodes.sonarrSeriesId WHERE table_episodes.path = ? AND missing_subtitles != '[]'", (path_replace_reverse(path),)).fetchall()
|
2017-12-08 01:03:32 +00:00
|
|
|
enabled_providers = c_db.execute("SELECT * FROM table_settings_providers WHERE enabled = 1").fetchall()
|
2017-11-16 19:09:40 +00:00
|
|
|
c_db.close()
|
2017-10-16 23:27:19 +00:00
|
|
|
|
2017-11-16 19:09:40 +00:00
|
|
|
providers_list = []
|
2017-12-08 01:03:32 +00:00
|
|
|
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
|
2017-10-16 23:27:19 +00:00
|
|
|
|
2017-11-16 19:09:40 +00:00
|
|
|
for episode in episodes_details:
|
|
|
|
for language in ast.literal_eval(episode[1]):
|
2017-12-08 01:03:32 +00:00
|
|
|
message = download_subtitle(path_replace(episode[0]), str(pycountry.languages.lookup(language).alpha_3), episode[4], providers_list, providers_auth)
|
2017-11-16 19:09:40 +00:00
|
|
|
if message is not None:
|
|
|
|
store_subtitles(path_replace(episode[0]))
|
|
|
|
list_missing_subtitles(episode[3])
|
|
|
|
history_log(1, episode[3], episode[2], message)
|
2018-01-23 04:25:58 +00:00
|
|
|
send_notifications(episode[3], episode[2], message)
|
2017-11-16 17:04:20 +00:00
|
|
|
|
2017-10-23 03:00:11 +00:00
|
|
|
def wanted_search_missing_subtitles():
|
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-23 03:00:11 +00:00
|
|
|
db.create_function("path_substitution", 1, path_replace)
|
|
|
|
c = db.cursor()
|
|
|
|
|
|
|
|
c.execute("SELECT path_substitution(path) FROM table_episodes WHERE table_episodes.missing_subtitles != '[]'")
|
|
|
|
data = c.fetchall()
|
|
|
|
c.close()
|
|
|
|
|
|
|
|
for episode in data:
|
2017-11-21 20:42:18 +00:00
|
|
|
wanted_download_subtitles(episode[0])
|
2017-12-06 04:07:37 +00:00
|
|
|
|
2017-12-08 01:03:32 +00:00
|
|
|
logging.info('Finished searching for missing subtitles. Check history for more information.')
|