bazarr/list_subtitles.py

261 lines
11 KiB
Python
Raw Normal View History

import gc
2017-09-16 00:49:46 +00:00
import os
import enzyme
import babelfish
2017-09-19 10:43:14 +00:00
from subliminal import *
2017-09-16 00:49:46 +00:00
import sqlite3
import ast
2018-01-10 16:44:47 +00:00
import langdetect
from bs4 import UnicodeDammit
from itertools import islice
2017-09-16 00:49:46 +00:00
from get_general_settings import *
from get_languages import *
2017-09-16 00:49:46 +00:00
2018-03-28 20:33:29 +00:00
gc.enable()
2017-09-16 00:49:46 +00:00
def store_subtitles(file):
languages = []
2017-09-28 01:55:21 +00:00
actual_subtitles = []
2017-11-28 04:09:24 +00:00
if os.path.exists(file):
2017-11-18 04:12:37 +00:00
if os.path.splitext(file)[1] == '.mkv':
2017-09-16 00:49:46 +00:00
try:
with open(file, 'rb') as f:
mkv = enzyme.MKV(f)
for subtitle_track in mkv.subtitle_tracks:
try:
if alpha2_from_alpha3(subtitle_track.language) != None:
actual_subtitles.append([str(alpha2_from_alpha3(subtitle_track.language)),None])
2017-09-16 00:49:46 +00:00
except:
pass
except:
pass
2017-12-31 21:24:52 +00:00
brazilian_portuguese = [".pt-br", ".pob", "pb"]
try:
subtitles = core.search_external_subtitles(file)
except:
pass
else:
for subtitle, language in subtitles.iteritems():
if str(os.path.splitext(subtitle)[0]).lower().endswith(tuple(brazilian_portuguese)) is True:
actual_subtitles.append([str("pb"), path_replace_reverse(os.path.join(os.path.dirname(file), subtitle))])
elif str(language) != 'und':
actual_subtitles.append([str(language), path_replace_reverse(os.path.join(os.path.dirname(file), subtitle))])
else:
with open(path_replace(os.path.join(os.path.dirname(file), subtitle)), 'r') as f:
text = list(islice(f, 100))
text = ' '.join(text)
encoding = UnicodeDammit(text)
try:
text = text.decode(encoding.original_encoding)
except Exception as e:
logging.exception('Error trying to detect character encoding for this subtitles file: ' + path_replace(os.path.join(os.path.dirname(file), subtitle)) + ' You should try to delete this subtitles file manually and ask Bazarr to download it again.')
else:
detected_language = langdetect.detect(text)
if len(detected_language) > 0:
actual_subtitles.append([str(detected_language), path_replace_reverse(os.path.join(os.path.dirname(file), subtitle))])
conn_db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30)
c_db = conn_db.cursor()
c_db.execute("UPDATE table_episodes SET subtitles = ? WHERE path = ?", (str(actual_subtitles), path_replace_reverse(file)))
conn_db.commit()
2017-12-31 21:24:52 +00:00
c_db.close()
2017-09-16 00:49:46 +00:00
2017-09-19 10:43:14 +00:00
return actual_subtitles
2018-04-19 23:45:10 +00:00
def store_subtitles_movie(file):
languages = []
actual_subtitles = []
if os.path.exists(file):
if os.path.splitext(file)[1] == '.mkv':
try:
with open(file, 'rb') as f:
mkv = enzyme.MKV(f)
for subtitle_track in mkv.subtitle_tracks:
try:
if alpha2_from_alpha3(subtitle_track.language) != None:
actual_subtitles.append([str(alpha2_from_alpha3(subtitle_track.language)), None])
2018-04-19 23:45:10 +00:00
except:
pass
except:
pass
subtitles = core.search_external_subtitles(file)
brazilian_portuguese = [".pt-br", ".pob", "pb"]
2018-04-19 23:45:10 +00:00
for subtitle, language in subtitles.iteritems():
if str(os.path.splitext(subtitle)[0]).lower().endswith(tuple(brazilian_portuguese)) is True:
actual_subtitles.append([str("pb"), path_replace_reverse_movie(os.path.join(os.path.dirname(file), subtitle))])
elif str(language) != 'und':
actual_subtitles.append([str(language), path_replace_reverse_movie(os.path.join(os.path.dirname(file), subtitle))])
2018-04-19 23:45:10 +00:00
else:
if os.path.splitext(subtitle)[1] != ".sub":
with open(path_replace_movie(os.path.join(os.path.dirname(file), subtitle)), 'r') as f:
text = list(islice(f, 100))
text = ' '.join(text)
encoding = UnicodeDammit(text)
try:
text = text.decode(encoding.original_encoding)
except Exception as e:
logging.exception('Error trying to detect character encoding for this subtitles file: ' + path_replace_movie(os.path.join(os.path.dirname(file), subtitle)) + ' You should try to delete this subtitles file manually and ask Bazarr to download it again.')
else:
detected_language = langdetect.detect(text)
if len(detected_language) > 0:
actual_subtitles.append([str(detected_language), path_replace_reverse_movie(os.path.join(os.path.dirname(file), subtitle))])
2018-04-19 23:45:10 +00:00
conn_db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30)
c_db = conn_db.cursor()
c_db.execute("UPDATE table_movies SET subtitles = ? WHERE path = ?", (str(actual_subtitles), path_replace_reverse_movie(file)))
2018-04-19 23:45:10 +00:00
conn_db.commit()
c_db.close()
return actual_subtitles
2017-10-16 23:27:19 +00:00
def list_missing_subtitles(*no):
query_string = ''
try:
2017-11-21 20:42:18 +00:00
query_string = " WHERE table_shows.sonarrSeriesId = " + str(no[0])
2017-10-16 23:27:19 +00:00
except:
pass
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-09-16 00:49:46 +00:00
c_db = conn_db.cursor()
2017-10-16 23:27:19 +00:00
episodes_subtitles = c_db.execute("SELECT table_episodes.sonarrEpisodeId, table_episodes.subtitles, table_shows.languages FROM table_episodes INNER JOIN table_shows on table_episodes.sonarrSeriesId = table_shows.sonarrSeriesId" + query_string).fetchall()
c_db.close()
missing_subtitles_global = []
2018-08-05 04:44:55 +00:00
use_embedded_subs = get_general_settings()[23]
2017-10-03 02:59:45 +00:00
for episode_subtitles in episodes_subtitles:
actual_subtitles_temp = []
2017-10-03 02:59:45 +00:00
actual_subtitles = []
desired_subtitles = []
missing_subtitles = []
2017-10-19 11:10:52 +00:00
if episode_subtitles[1] != None:
if use_embedded_subs == "True":
actual_subtitles = ast.literal_eval(episode_subtitles[1])
else:
actual_subtitles_temp = ast.literal_eval(episode_subtitles[1])
for subtitle in actual_subtitles_temp:
if subtitle[1] != None:
actual_subtitles.append(subtitle)
2017-11-16 14:22:55 +00:00
if episode_subtitles[2] != None:
2017-11-16 03:51:28 +00:00
desired_subtitles = ast.literal_eval(episode_subtitles[2])
actual_subtitles_list = []
if desired_subtitles == None:
missing_subtitles_global.append(tuple(['[]', episode_subtitles[0]]))
else:
for item in actual_subtitles:
if item[0] == "pt-BR":
actual_subtitles_list.append("pb")
else:
actual_subtitles_list.append(item[0])
2017-11-16 03:51:28 +00:00
missing_subtitles = list(set(desired_subtitles) - set(actual_subtitles_list))
2017-11-16 14:22:55 +00:00
missing_subtitles_global.append(tuple([str(missing_subtitles), episode_subtitles[0]]))
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)
c_db = conn_db.cursor()
2017-10-03 02:59:45 +00:00
c_db.executemany("UPDATE table_episodes SET missing_subtitles = ? WHERE sonarrEpisodeId = ?", (missing_subtitles_global))
conn_db.commit()
c_db.close()
2017-09-16 00:49:46 +00:00
2018-04-24 14:48:52 +00:00
def list_missing_subtitles_movies(*no):
query_string = ''
try:
query_string = " WHERE table_movies.radarrId = " + str(no[0])
except:
pass
conn_db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30)
c_db = conn_db.cursor()
movies_subtitles = c_db.execute("SELECT radarrId, subtitles, languages FROM table_movies" + query_string).fetchall()
c_db.close()
missing_subtitles_global = []
2018-08-05 04:44:55 +00:00
use_embedded_subs = get_general_settings()[23]
2018-04-24 14:48:52 +00:00
for movie_subtitles in movies_subtitles:
actual_subtitles_temp = []
2018-04-24 14:48:52 +00:00
actual_subtitles = []
desired_subtitles = []
missing_subtitles = []
if movie_subtitles[1] != None:
if use_embedded_subs == "True":
actual_subtitles = ast.literal_eval(movie_subtitles[1])
else:
actual_subtitles_temp = ast.literal_eval(movie_subtitles[1])
for subtitle in actual_subtitles_temp:
if subtitle[1] != None:
actual_subtitles.append(subtitle)
2018-04-24 14:48:52 +00:00
if movie_subtitles[2] != None:
desired_subtitles = ast.literal_eval(movie_subtitles[2])
actual_subtitles_list = []
if desired_subtitles == None:
missing_subtitles_global.append(tuple(['[]', movie_subtitles[0]]))
else:
for item in actual_subtitles:
if item[0] == "pt-BR":
actual_subtitles_list.append("pb")
else:
actual_subtitles_list.append(item[0])
2018-04-24 14:48:52 +00:00
missing_subtitles = list(set(desired_subtitles) - set(actual_subtitles_list))
missing_subtitles_global.append(tuple([str(missing_subtitles), movie_subtitles[0]]))
conn_db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30)
c_db = conn_db.cursor()
c_db.executemany("UPDATE table_movies SET missing_subtitles = ? WHERE radarrId = ?", (missing_subtitles_global))
conn_db.commit()
c_db.close()
def series_full_scan_subtitles():
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-09-16 00:49:46 +00:00
c_db = conn_db.cursor()
2017-10-03 02:59:45 +00:00
episodes = c_db.execute("SELECT path FROM table_episodes").fetchall()
2017-09-16 00:49:46 +00:00
c_db.close()
2017-10-03 02:59:45 +00:00
for episode in episodes:
2017-11-28 03:06:51 +00:00
store_subtitles(path_replace(episode[0]))
2017-10-16 23:27:19 +00:00
gc.collect()
def movies_full_scan_subtitles():
conn_db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30)
2018-04-19 23:45:10 +00:00
c_db = conn_db.cursor()
movies = c_db.execute("SELECT path FROM table_movies").fetchall()
c_db.close()
for movie in movies:
store_subtitles_movie(path_replace_movie(movie[0]))
2018-04-19 23:45:10 +00:00
gc.collect()
2017-10-16 23:27:19 +00:00
def series_scan_subtitles(no):
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-10-16 23:27:19 +00:00
c_db = conn_db.cursor()
episodes = c_db.execute("SELECT path FROM table_episodes WHERE sonarrSeriesId = ?", (no,)).fetchall()
c_db.close()
for episode in episodes:
2017-11-28 03:06:51 +00:00
store_subtitles(path_replace(episode[0]))
2017-10-16 23:27:19 +00:00
list_missing_subtitles(no)
2017-11-16 14:22:55 +00:00
2018-04-24 14:48:52 +00:00
def movies_scan_subtitles(no):
conn_db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30)
c_db = conn_db.cursor()
movies = c_db.execute("SELECT path FROM table_movies WHERE radarrId = ?", (no,)).fetchall()
c_db.close()
for movie in movies:
store_subtitles_movie(path_replace_movie(movie[0]))
2018-04-24 14:48:52 +00:00
2018-08-05 04:44:55 +00:00
list_missing_subtitles_movies(no)