bazarr/list_subtitles.py

134 lines
4.6 KiB
Python
Raw Normal View History

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 pycountry
import sqlite3
import ast
from get_general_settings import *
2017-09-28 01:55:21 +00:00
def list_subtitles(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:
languages.append([str(pycountry.languages.lookup(subtitle_track.language).alpha_2),None])
except:
pass
except:
pass
subtitles = core.search_external_subtitles(file)
for subtitle, language in subtitles.iteritems():
actual_subtitles.append([str(language), path_replace_reverse(os.path.join(os.path.dirname(file), subtitle))])
return actual_subtitles
2017-09-16 00:49:46 +00:00
def store_subtitles(file):
languages = []
2017-09-28 01:55:21 +00:00
actual_subtitles = []
2017-09-16 00:49:46 +00:00
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:
languages.append([str(pycountry.languages.lookup(subtitle_track.language).alpha_2),None])
except:
pass
except:
pass
2017-10-19 23:50:54 +00:00
conn_db = sqlite3.connect('data/db/bazarr.db')
2017-09-16 00:49:46 +00:00
c_db = conn_db.cursor()
2017-09-19 10:43:14 +00:00
subtitles = core.search_external_subtitles(file)
2017-09-28 01:55:21 +00:00
2017-09-19 10:43:14 +00:00
for subtitle, language in subtitles.iteritems():
actual_subtitles.append([str(language), path_replace_reverse(os.path.join(os.path.dirname(file), subtitle))])
2017-09-16 00:49:46 +00:00
try:
2017-09-19 10:43:14 +00:00
c_db.execute("UPDATE table_episodes SET subtitles = ? WHERE path = ?", (str(actual_subtitles), path_replace_reverse(file)))
2017-09-16 00:49:46 +00:00
conn_db.commit()
except:
pass
c_db.close()
2017-09-19 10:43:14 +00:00
return actual_subtitles
2017-09-16 00:49:46 +00:00
2017-10-16 23:27:19 +00:00
def list_missing_subtitles(*no):
query_string = ''
try:
query_string = " WHERE table_episodes.sonarrSeriesId = " + str(no[0])
except:
pass
2017-10-19 23:50:54 +00:00
conn_db = sqlite3.connect('data/db/bazarr.db')
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()
2017-09-16 00:49:46 +00:00
2017-10-03 02:59:45 +00:00
missing_subtitles_global = []
for episode_subtitles in episodes_subtitles:
actual_subtitles = []
desired_subtitles = []
missing_subtitles = []
2017-10-19 11:10:52 +00:00
if episode_subtitles[1] != None:
actual_subtitles = ast.literal_eval(episode_subtitles[1])
desired_subtitles = ast.literal_eval(episode_subtitles[2])
2017-10-03 02:59:45 +00:00
actual_subtitles_list = []
if desired_subtitles == None:
pass
else:
actual_subtitles_list = []
for item in actual_subtitles:
actual_subtitles_list.append(item[0])
missing_subtitles = list(set(desired_subtitles) - set(actual_subtitles_list))
missing_subtitles_global.append(tuple([str(missing_subtitles), episode_subtitles[0]]))
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
def full_scan_subtitles():
2017-10-19 23:50:54 +00:00
conn_db = sqlite3.connect('data/db/bazarr.db')
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:
store_subtitles(path_replace(episode[0]))
2017-10-16 23:27:19 +00:00
def series_scan_subtitles(no):
2017-10-19 23:50:54 +00:00
conn_db = sqlite3.connect('data/db/bazarr.db')
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:
store_subtitles(path_replace(episode[0]))
list_missing_subtitles(no)
2017-10-03 02:59:45 +00:00
2017-09-28 01:55:21 +00:00
def new_scan_subtitles():
2017-10-19 23:50:54 +00:00
conn_db = sqlite3.connect('data/db/bazarr.db')
2017-09-28 01:55:21 +00:00
c_db = conn_db.cursor()
2017-10-03 02:59:45 +00:00
episodes = c_db.execute("SELECT path FROM table_episodes WHERE subtitles is null").fetchall()
2017-09-28 01:55:21 +00:00
c_db.close()
2017-09-16 00:49:46 +00:00
2017-10-03 02:59:45 +00:00
for episode in episodes:
store_subtitles(path_replace(episode[0]))
2017-09-16 00:49:46 +00:00
#full_scan_subtitles()
2017-09-28 01:55:21 +00:00
#new_scan_subtitles()
2017-10-03 02:59:45 +00:00
#list_missing_subtitles()