Commit fae8a12d authored by Dustyn Gibson's avatar Dustyn Gibson
Browse files

More subtitles changes

parent 6b6bf96b
#from sickbeard import subtitles
#import sickbeard
#from sickbeard.helpers import anon_url
#from babelfish import Language
#set global $title="Config - Subtitles"
#set global $header="Subtitles"
......@@ -19,7 +19,7 @@
\$(document).ready(function() {
\$("#subtitles_languages").tokenInput(
[
<%=",\r\n".join("{id: \"" + lang[2] + "\", name: \"" + lang[3] + "\"}" for lang in subtitles.subtitleLanguageFilter())%>
<%=",\r\n".join("{id: \"" + lang.alpha2 + "\", name: \"" + lang.name + "\"}" for lang in subtitles.subtitleLanguageFilter())%>
],
{
method: "POST",
......@@ -29,7 +29,7 @@
[
<%=
",\r\n".join("{id: \"" + lang + "\", name: \"" + subtitles.getLanguageName(lang) + "\"}" for lang in sickbeard.SUBTITLES_LANGUAGES) if sickbeard.SUBTITLES_LANGUAGES != '' else ''
",\r\n".join("{id: \"" + lang + "\", name: \"" + Language.fromalpha2(lang).name + "\"}" for lang in sickbeard.SUBTITLES_LANGUAGES) if sickbeard.SUBTITLES_LANGUAGES != '' else ''
%>
]
}
......@@ -146,12 +146,11 @@
#set $curName = $curService.id
<li class="ui-state-default" id="$curName">
<input type="checkbox" id="enable_$curName" class="service_enabler" #if $curService.enabled then "checked=\"checked\"" else ""#/>
#set $provider_url = $curService.url
<a href="<%= anon_url(provider_url) %>" class="imgLink" target="_new">
##set $provider_url = $curService.url
##<a href="<%= anon_url(provider_url) %>" class="imgLink" target="_new">
<img src="$sbRoot/images/subtitles/$curService.image" alt="$curService.name" title="$curService.name" width="16" height="16" style="vertical-align:middle;"/>
</a>
##</a>
<span style="vertical-align:middle;">$curService.name.capitalize()</span>
#if not $curService.api_based then "*" else ""#
<span class="ui-icon ui-icon-arrowthick-2-n-s pull-right" style="vertical-align:middle;"></span>
</li>
#end for
......
......@@ -580,8 +580,8 @@
</td>
<td class="col-subtitles" align="center">
#if $epResult["subtitles"]:
#for $sub_lang in babelfish.LANGUAGE_MATRIX([x.strip() for x in $epResult["subtitles"].split(',') if x != ""]):
#if sub_lang.alpha2 != ""
#for $sub_lang in [babelfish.Language.fromalpha2(x.strip()) for x in $epResult["subtitles"].split(',') if x != ""]:
#if $sub_lang.alpha2 != u'':
<img src="$sbRoot/images/flags/${sub_lang.alpha2}.png" width="16" height="11" alt="${sub_lang}" />
#else
<img src="$sbRoot/images/flags/unknown.png" width="16" height="11" alt="Unknown" />
......
......@@ -18,7 +18,7 @@
<h1 class="title">$title</h1>
#end if
#if $whichSubs:
#set subsLanguage = $babelfish.language.Language($whichSubs) if not $whichSubs == 'all' else 'All'
#set subsLanguage = $babelfish.Language.fromalpha2($whichSubs).name if not $whichSubs == 'all' else 'All'
#end if
#if not $whichSubs or ($whichSubs and not $ep_counts):
......@@ -30,8 +30,8 @@
<form action="$sbRoot/manage/subtitleMissed" method="get">
Manage episodes without <select name="whichSubs" class="form-control form-control-inline input-sm">
<option value="all">All</option>
#for $sub_lang in $babelfish.LANGUAGE_MATRIX:
<option value="$sub_lang.alpha2">$sub_lang</option>
#for $sub_lang in [$babelfish.Language.fromalpha2(x.strip()) for x in $sickbeard.SUBTITLES_LANGUAGES]:
#<option value="$sub_lang.alpha2">$sub_lang.name</option>
#end for
</select>
subtitles
......
......@@ -238,8 +238,7 @@ class ProviderPool(object):
"""
def __init__(self, providers=None, provider_configs=None):
self.provider_configs = provider_configs or {}
for p in (providers or provider_manager.available_providers):
self.providers[p] = provider_manager[p]
self.providers = dict([(p, provider_manager[p]) for p in (providers or provider_manager.available_providers)])
self.initialized_providers = {}
self.discarded_providers = set()
......
......@@ -29,40 +29,40 @@ from sickbeard import history
from lib import subliminal
from lib import babelfish
subliminal.cache_region.configure('dogpile.cache.memory')
SINGLE = 'und'
def sortedServiceList():
servicesMapping = dict([(x.split(' = ')[0], x.split(' = ')[1]) for x in subliminal.providers.ProviderManager().registered_providers])
newList = []
# add all services in the priority list, in order
curIndex = 0
for curService in sickbeard.SUBTITLES_SERVICES_LIST:
if curService in servicesMapping:
curServiceDict = {'id': curService, 'image': curService+'.png', 'name': curService, 'enabled': sickbeard.SUBTITLES_SERVICES_ENABLED[curIndex] == 1, 'api_based': True, 'url': 'null'}
if curService in subliminal.provider_manager.available_providers:
curServiceDict = {'id': curService, 'image': curService+'.png', 'name': curService, 'enabled': sickbeard.SUBTITLES_SERVICES_ENABLED[curIndex] == 1}
newList.append(curServiceDict)
curIndex += 1
# add any services that are missing from that list
for curService in servicesMapping.keys():
for curService in subliminal.provider_manager.available_providers:
if curService not in [x['id'] for x in newList]:
curServiceDict = {'id': curService, 'image': curService+'.png', 'name': curService, 'enabled': False, 'api_based': True, 'url': 'null'}
curServiceDict = {'id': curService, 'image': curService+'.png', 'name': curService, 'enabled': False}
newList.append(curServiceDict)
return newList
def getEnabledServiceList():
return [x['name'] for x in sortedServiceList() if x['enabled']]
def isValidLanguage(language):
try:
check = babelfish.Language(language)
check = babelfish.Language.fromalpha2(language)
except:
return ''
return u''
return check
def getLanguageName(selectLang):
return babelfish.language.Language(selectLang).name
return babelfish.Language.fromalpha2(selectLang).name
def wantedLanguages(sqlLike = False):
wantedLanguages = sorted(sickbeard.SUBTITLES_LANGUAGES)
......@@ -72,19 +72,19 @@ def wantedLanguages(sqlLike = False):
def subtitlesLanguages(video_path):
"""Return a list detected subtitles for the given video file"""
video = subliminal.videos.Video.from_path(video_path)
subtitles = video.scan()
languages = set()
for subtitle in subtitles:
if subtitle.language and subtitle.language.alpha2:
languages.add(babelfish.language.alpha2)
else:
languages.add(SINGLE)
return list(languages)
#subtitles = subliminal.video.scan_subtitle_languages(video_path)
#languages = set()
#for subtitle in subtitles:
# if subtitle.language and subtitle.language.alpha2:
# languages.add(subtitle.language.alpha2)
# else:
# languages.add(SINGLE)
#return list(languages)
return subliminal.video.scan_subtitle_languages(video_path)
# Return a list with languages that have alpha2 code
def subtitleLanguageFilter():
return [language for language in babelfish.LANGUAGE_MATRIX if language[2] != ""]
return [language for language in babelfish.LANGUAGE_MATRIX if language.alpha2 != u'']
class SubtitlesFinder():
"""
......
......@@ -1426,46 +1426,62 @@ class TVEpisode(object):
def downloadSubtitles(self, force=False):
# TODO: Add support for force option
if not ek.ek(os.path.isfile, self.location):
logger.log(
str(self.show.indexerid) + ": Episode file doesn't exist, can't download subtitles for episode " + str(
self.season) + "x" + str(self.episode), logger.DEBUG)
logger.log(u"%s: Episode file doesn't exist, can't download subtitles for S%02dE%02d" %
(self.show.indexerid, self.season, self.episode), logger.DEBUG)
return
logger.log(str(self.show.indexerid) + ": Downloading subtitles for episode " + str(self.season) + "x" + str(
self.episode), logger.DEBUG)
logger.log(u"%s: Downloading subtitles for S%02dE%02d" % (self.show.indexerid, self.season, self.episode), logger.DEBUG)
previous_subtitles = self.subtitles
added_subtitles = []
try:
need_languages = set(sickbeard.SUBTITLES_LANGUAGES) - set(self.subtitles)
subtitles = subliminal.download_subtitles([self.location], languages=need_languages,
services=sickbeard.subtitles.getEnabledServiceList(), force=force,
multi=sickbeard.SUBTITLES_MULTI, cache_dir=sickbeard.CACHE_DIR)
languages = set()
for lang in set(sickbeard.SUBTITLES_LANGUAGES) - set(self.subtitles):
languages.add(babelfish.Language.fromalpha2(lang).alpha3)
video = subliminal.scan_video(self.location, embedded_subtitles=False)
providers = sickbeard.subtitles.getEnabledServiceList()
#logger.log(u"video = %s, languages = %s providers = %s" % (video, languages, providers), logger.DEBUG)
foundSubs = subliminal.download_best_subtitles([video], languages, providers, single=not sickbeard.SUBTITLES_MULTI, hearing_impaired=True)
if not len(foundSubs):
logger.log(u"%s: No subtitles found for S%02dE%02d on any provider" % (self.show.indexerid, self.season, self.episode), logger.DEBUG)
return
if sickbeard.SUBTITLES_DIR:
for video in subtitles:
subs_new_path = ek.ek(os.path.join, os.path.dirname(video.path), sickbeard.SUBTITLES_DIR)
dir_exists = helpers.makeDir(subs_new_path)
if not dir_exists:
logger.log(u"Unable to create subtitles folder " + subs_new_path, logger.ERROR)
else:
helpers.chmodAsParent(subs_new_path)
subs_new_path = ek.ek(os.path.join, os.path.dirname(video.path), sickbeard.SUBTITLES_DIR)
dir_exists = helpers.makeDir(subs_new_path)
if not dir_exists:
logger.log(u"Unable to create subtitles folder " + subs_new_path, logger.ERROR)
else:
helpers.chmodAsParent(subs_new_path)
subliminal.save_subtitles(foundSubs, directory=subs_new_path)
for subtitle in subtitles.get(video):
for video in downloadedSubs:
for subtitle in downloadedSubs.get(video):
added_subtitles.append(subtitle.language.alpha2)
new_file_path = ek.ek(os.path.join, subs_new_path, os.path.basename(subtitle.path))
helpers.moveFile(subtitle.path, new_file_path)
helpers.chmodAsParent(new_file_path)
else:
for video in subtitles:
for subtitle in subtitles.get(video):
added_subtitles.append(subtitle.language.alpha2)
helpers.chmodAsParent(subtitle.path)
except ServiceError as e:
logger.log("Service is unavailable: {0}".format(str(e)), logger.INFO)
return
subliminal.save_subtitles(foundSubs, single=not sickbeard.SUBTITLES_MULTI)
for video, video_subtitles in foundSubs.items():
saved_languages = set()
for video_subtitle in video_subtitles:
if video_subtitle.language in saved_languages:
continue
saved_languages.add(video_subtitle.language)
try:
added_subtitles.append(video_subtitle.language.alpha2)
except babelfish.LanguageConvertError:
added_subtitles.append(video_subtitle.language.alpha3)
# helpers.chmodAsParent(subtitle.path)
if not sickbeard.SUBTITLES_MULTI:
break
except Exception as e:
logger.log("Error occurred when downloading subtitles: " + str(e), logger.ERROR)
logger.log("Error occurred when downloading subtitles: " + traceback.format_exc(), logger.ERROR)
return
if sickbeard.SUBTITLES_MULTI:
......@@ -1480,7 +1496,7 @@ class TVEpisode(object):
newsubtitles = set(self.subtitles).difference(set(previous_subtitles))
if newsubtitles:
subtitleList = ", ".join(babelfish.language.Language(x).name for x in newsubtitles)
subtitleList = ", ".join(subliminal.Subtitle(x).language for x in newsubtitles)
logger.log(str(self.show.indexerid) + u": Downloaded " + subtitleList + " subtitles for episode " + str(
self.season) + "x" + str(self.episode), logger.DEBUG)
......@@ -1492,12 +1508,14 @@ class TVEpisode(object):
self.episode), logger.DEBUG)
if sickbeard.SUBTITLES_HISTORY:
for video in subtitles:
for subtitle in subtitles.get(video):
#TODO
for video in downloadedSubs:
for subtitle in downloadedSubs.get(video):
history.logSubtitle(self.show.indexerid, self.season, self.episode, self.status, subtitle)
return subtitles
def checkForMetaFiles(self):
oldhasnfo = self.hasnfo
......
......@@ -2013,9 +2013,9 @@ class Home(WebRoot):
return json.dumps({'result': 'failure'})
# try do download subtitles for that episode
previous_subtitles = set(babelfish.language.Language(x) for x in ep_obj.subtitles)
previous_subtitles = set(babelfish.Language.fromalpha2(x) for x in ep_obj.subtitles)
try:
ep_obj.subtitles = set(x.language for x in ep_obj.downloadSubtitles().values()[0])
ep_obj.subtitles = set(babelfish.Language.fromalpha2(x) for x in ep_obj.downloadSubtitles().values()[0])
except:
return json.dumps({'result': 'failure'})
......@@ -2903,8 +2903,7 @@ class Manage(Home, WebRoot):
result[cur_season][cur_episode]["name"] = cur_result["name"]
result[cur_season][cur_episode]["subtitles"] = ",".join(
babelfish.language.Language(subtitle).alpha2 for subtitle in cur_result["subtitles"].split(',')) if not \
result[cur_season][cur_episode]["subtitles"] = ",".join(subtitle.strip() for subtitle in cur_result["subtitles"].split(',')) if not \
cur_result["subtitles"] == '' else ''
return json.dumps(result)
......@@ -4859,8 +4858,7 @@ class ConfigSubtitles(Config):
config.change_SUBTITLES_FINDER_FREQUENCY(subtitles_finder_frequency)
config.change_USE_SUBTITLES(use_subtitles)
sickbeard.SUBTITLES_LANGUAGES = [lang.alpha2 for lang in subtitles.isValidLanguage(
subtitles_languages.replace(' ', '').split(','))] if subtitles_languages != '' else ''
sickbeard.SUBTITLES_LANGUAGES = [lang.strip() for lang in subtitles_languages.replace(' ', '').split(',')] if subtitles_languages != '' else []
sickbeard.SUBTITLES_DIR = subtitles_dir
sickbeard.SUBTITLES_HISTORY = config.checkbox_to_value(subtitles_history)
sickbeard.EMBEDDED_SUBTITLES_ALL = config.checkbox_to_value(embedded_subtitles_all)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment