bazarr/get_subtitle.py

68 lines
3.1 KiB
Python
Raw Normal View History

2017-09-17 00:11:47 +00:00
import os
2017-10-16 23:27:19 +00:00
import sqlite3
import ast
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 *
2017-09-17 00:11:47 +00:00
# configure the cache
region.configure('dogpile.cache.dbm', arguments={'filename': 'cachefile.dbm'})
def download_subtitle(path, language, hi, providers):
video = scan_video(path)
best_subtitles = download_best_subtitles([video], {Language(language)}, providers=providers, hearing_impaired=hi)
2017-10-03 02:59:45 +00:00
try:
best_subtitle = best_subtitles[video][0]
2017-09-17 00:11:47 +00:00
2017-10-03 02:59:45 +00:00
result = save_subtitles(video, [best_subtitle])
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 + "."
2017-09-17 00:11:47 +00:00
2017-10-03 02:59:45 +00:00
return message
except:
return None
2017-10-16 23:27:19 +00:00
def series_download_subtitles(no):
conn_db = sqlite3.connect('bazarr.db')
c_db = conn_db.cursor()
episodes_details = c_db.execute("SELECT path, missing_subtitles, sonarrEpisodeId FROM table_episodes WHERE path = ?", (no,)).fetchall()
series_details = c_db.execute("SELECT hearing_impaired FROM table_shows WHERE sonarrSeriesId = ?", (no,)).fetchone()
enabled_providers = c_db.execute("SELECT name FROM table_settings_providers WHERE enabled = 1").fetchall()
c_db.close()
providers_list = []
for provider in enabled_providers:
providers_list.append(provider[0])
for episode in episodes_details:
for language in ast.literal_eval(episode[1]):
message = download_subtitle(path_replace(episode[0]), str(pycountry.languages.lookup(language).alpha_3), series_details[0], providers_list)
if message is not None:
store_subtitles(path_replace(episode[0]))
history_log(1, no, episode[2], message)
list_missing_subtitles(no)
def wanted_download_subtitles(path):
conn_db = sqlite3.connect('bazarr.db')
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()
enabled_providers = c_db.execute("SELECT name FROM table_settings_providers WHERE enabled = 1").fetchall()
c_db.close()
providers_list = []
for provider in enabled_providers:
providers_list.append(provider[0])
for episode in episodes_details:
for language in ast.literal_eval(episode[1]):
message = download_subtitle(path_replace(episode[0]), str(pycountry.languages.lookup(language).alpha_3), episode[4], providers_list)
if message is not None:
store_subtitles(path_replace(episode[0]))
list_missing_subtitles(episode[3])
history_log(1, episode[3], episode[2], message)