Commit 360392cb authored by Dustyn Gibson's avatar Dustyn Gibson
Browse files

Various fixes to subtitles.

Fix flags
Leave epObj.subtitles as a string, instead of always a list.
Some error checking, still a lot of cleaning and optimizing to do.
SiCKRAGETV/sickrage-issues#1913
parent 08bf6b30
......@@ -580,9 +580,9 @@
</td>
<td class="col-subtitles" align="center">
#if $epResult["subtitles"]:
#for $sub_lang in [babelfish.Language.fromalpha2(x.strip()) for x in $epResult["subtitles"].split(',') if x != ""]:
#for $sub_lang in [babelfish.Language.fromietf(x.strip()) for x in $epResult["subtitles"].split(',') if x.strip() in babelfish.language_converters['alpha2'].codes]:
#if $sub_lang.alpha2 != u'':
<img src="$sbRoot/images/flags/${sub_lang.alpha2}.png" width="16" height="11" alt="${sub_lang}" />
<img src="$sbRoot/images/flags/${sub_lang.alpha2}.png" width="16" height="11" alt="${sub_lang.name}" />
#else
<img src="$sbRoot/images/flags/unknown.png" width="16" height="11" alt="Unknown" />
#end if
......
......@@ -18,7 +18,7 @@
<h1 class="title">$title</h1>
#end if
#if $whichSubs:
#set subsLanguage = $babelfish.Language.fromalpha2($whichSubs).name if not $whichSubs == 'all' else 'All'
#set subsLanguage = $babelfish.Language.fromietf($whichSubs).name if not $whichSubs == 'all' else 'All'
#end if
#if not $whichSubs or ($whichSubs and not $ep_counts):
......@@ -30,7 +30,7 @@
<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.fromalpha2(x.strip()) for x in $sickbeard.SUBTITLES_LANGUAGES]:
#for $sub_lang in [$babelfish.Language.fromietf(x.strip()) for x in $sickbeard.SUBTITLES_LANGUAGES]:
#<option value="$sub_lang.alpha2">$sub_lang.name</option>
#end for
</select>
......
......@@ -967,7 +967,7 @@ class PostProcessor(object):
else:
cur_ep.status = common.Quality.compositeStatus(common.DOWNLOADED, new_ep_quality)
cur_ep.subtitles = []
cur_ep.subtitles = u''
cur_ep.subtitles_searchcount = 0
......
......@@ -55,11 +55,7 @@ def getEnabledServiceList():
return [x['name'] for x in sortedServiceList() if x['enabled']]
def isValidLanguage(language):
try:
check = babelfish.Language.fromalpha2(language)
except:
return u''
return check
return language if language in babelfish.language_converters['alpha2'].codes else u''
def getLanguageName(selectLang):
return babelfish.Language.fromalpha2(selectLang).name
......@@ -72,11 +68,13 @@ def wantedLanguages(sqlLike = False):
def subtitlesLanguages(video_path):
"""Return a list detected subtitles for the given video file"""
return subliminal.video.scan_subtitle_languages(video_path)
languages = subliminal.video.scan_subtitle_languages(video_path)
return u','.join([lang.alpha2 for lang in languages if lang.alpha2 in babelfish.language_converters['alpha2'].codes])
# Return a list with languages that have alpha2 code
def subtitleLanguageFilter():
return [language for language in babelfish.LANGUAGE_MATRIX if language.alpha2 != u'']
return [language for language in babelfish.LANGUAGE_MATRIX if language.alpha2 in babelfish.language_converters['alpha2'].codes]
class SubtitlesFinder():
"""
......@@ -137,11 +135,15 @@ class SubtitlesFinder():
previous_subtitles = epObj.subtitles
try:
subtitles = epObj.downloadSubtitles()
epObj.downloadSubtitles()
except:
logger.log(u'Unable to find subtitles', logger.DEBUG)
return
newSubtitles = list(set(epObj.subtitles) - set(previous_subtitles))
if newSubtitles:
logger.log(u'Downloaded subtitles for S%02dE%02d in %s' % (epToSub["season"], epToSub["episode"], ', '.join(newSubtitles)))
def _getRules(self):
"""
Define the hours to wait between 2 subtitles search depending on:
......
......@@ -1099,7 +1099,7 @@ class TVShow(object):
episode) + " doesn't exist, removing it and changing our status to " + statusStrings[sickbeard.EP_DEFAULT_DELETED_STATUS],
logger.DEBUG)
curEp.status = sickbeard.EP_DEFAULT_DELETED_STATUS
curEp.subtitles = list()
curEp.subtitles = u''
curEp.subtitles_searchcount = 0
curEp.subtitles_lastsearch = str(datetime.datetime.min)
curEp.location = ''
......@@ -1351,7 +1351,7 @@ class TVEpisode(object):
self._episode = episode
self._absolute_number = 0
self._description = ""
self._subtitles = list()
self._subtitles = u''
self._subtitles_searchcount = 0
self._subtitles_lastsearch = str(datetime.datetime.min)
self._airdate = datetime.date.fromordinal(1)
......@@ -1436,7 +1436,6 @@ class TVEpisode(object):
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:
languages = set()
......@@ -1480,27 +1479,19 @@ class TVEpisode(object):
subliminal.save_subtitles(foundSubs, directory=subs_new_path, single=not sickbeard.SUBTITLES_MULTI)
for video, subtitle in foundSubs.items():
for sub in subtitle:
added_subtitles.append(sub.language.alpha2)
except Exception as e:
logger.log("Error occurred when downloading subtitles: " + traceback.format_exc(), logger.ERROR)
return
if sickbeard.SUBTITLES_MULTI:
self.refreshSubtitles()
else:
self.subtitles = added_subtitles
self.refreshSubtitles()
self.subtitles_searchcount = self.subtitles_searchcount + 1
self.subtitles_lastsearch = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.saveToDB()
newsubtitles = set(self.subtitles).difference(set(previous_subtitles))
if newsubtitles:
subtitleList = ", ".join(subliminal.Subtitle(x).language for x in newsubtitles)
newSubtitles = list(set(self.subtitles.split(',')) - set(previous_subtitles.split(',')))
if len(newSubtitles):
subtitleList = ", ".join([babelfish.Language.fromietf(lang).name for lang in newSubtitles])
logger.log(u"%s: Downloaded %s subtitles for S%02dE%02d" %
(self.show.indexerid, subtitleList, self.season, self.episode), logger.DEBUG)
......@@ -1511,10 +1502,11 @@ class TVEpisode(object):
(self.show.indexerid, self.season, self.episode), logger.DEBUG)
if sickbeard.SUBTITLES_HISTORY:
for video, subtitle in foundSubs.items():
history.logSubtitle(self.show.indexerid, self.season, self.episode, self.status, subtitle.language.name)
for vid, sub in foundSubs.items():
logger.log(u'history.logSubtitle %s' % sub.language.name, logger.DEBUG)
history.logSubtitle(self.show.indexerid, self.season, self.episode, self.status, sub.language.name)
return subtitles
return self.subtitles
def checkForMetaFiles(self):
......@@ -1598,8 +1590,7 @@ class TVEpisode(object):
self.description = sqlResults[0]["description"]
if not self.description:
self.description = ""
if sqlResults[0]["subtitles"] and sqlResults[0]["subtitles"]:
self.subtitles = sqlResults[0]["subtitles"].split(",")
self.subtitles = sqlResults[0]["subtitles"] or u''
self.subtitles_searchcount = sqlResults[0]["subtitles_searchcount"]
self.subtitles_lastsearch = sqlResults[0]["subtitles_lastsearch"]
self.airdate = datetime.date.fromordinal(int(sqlResults[0]["airdate"]))
......@@ -1934,7 +1925,7 @@ class TVEpisode(object):
self.name) + "\n"
toReturn += "location: " + str(self.location) + "\n"
toReturn += "description: " + str(self.description) + "\n"
toReturn += "subtitles: " + str(",".join(self.subtitles)) + "\n"
toReturn += "subtitles: " + str(self.subtitles) + "\n"
toReturn += "subtitles_searchcount: " + str(self.subtitles_searchcount) + "\n"
toReturn += "subtitles_lastsearch: " + str(self.subtitles_lastsearch) + "\n"
toReturn += "airdate: " + str(self.airdate.toordinal()) + " (" + str(self.airdate) + ")\n"
......@@ -2022,7 +2013,7 @@ class TVEpisode(object):
"subtitles_searchcount = ?, subtitles_lastsearch = ?, airdate = ?, hasnfo = ?, hastbn = ?, status = ?, "
"location = ?, file_size = ?, release_name = ?, is_proper = ?, showid = ?, season = ?, episode = ?, "
"absolute_number = ?, version = ?, release_group = ? WHERE episode_id = ?",
[self.indexerid, self.indexer, self.name, self.description, ",".join([sub for sub in self.subtitles]),
[self.indexerid, self.indexer, self.name, self.description, self.subtitles,
self.subtitles_searchcount, self.subtitles_lastsearch, self.airdate.toordinal(), self.hasnfo,
self.hastbn,
self.status, self.location, self.file_size, self.release_name, self.is_proper, self.show.indexerid,
......@@ -2048,8 +2039,7 @@ class TVEpisode(object):
"((SELECT episode_id FROM tv_episodes WHERE showid = ? AND season = ? AND episode = ?)"
",?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);",
[self.show.indexerid, self.season, self.episode, self.indexerid, self.indexer, self.name,
self.description,
",".join([sub for sub in self.subtitles]), self.subtitles_searchcount, self.subtitles_lastsearch,
self.description, self.subtitles, self.subtitles_searchcount, self.subtitles_lastsearch,
self.airdate.toordinal(), self.hasnfo, self.hastbn, self.status, self.location, self.file_size,
self.release_name, self.is_proper, self.show.indexerid, self.season, self.episode,
self.absolute_number, self.version, self.release_group]]
......@@ -2077,7 +2067,7 @@ class TVEpisode(object):
"indexer": self.indexer,
"name": self.name,
"description": self.description,
"subtitles": ",".join([sub for sub in self.subtitles]),
"subtitles": self.subtitles,
"subtitles_searchcount": self.subtitles_searchcount,
"subtitles_lastsearch": self.subtitles_lastsearch,
"airdate": self.airdate.toordinal(),
......@@ -2560,6 +2550,7 @@ class TVEpisode(object):
related_files = postProcessor.PostProcessor(self.location).list_associated_files(
self.location, base_name_only=True, subfolders=True)
#This is wrong. Cause of pp not moving subs.
if self.show.subtitles and sickbeard.SUBTITLES_DIR != '':
related_subs = postProcessor.PostProcessor(self.location).list_associated_files(sickbeard.SUBTITLES_DIR,
subtitles_only=True, subfolders=True)
......
......@@ -2013,24 +2013,23 @@ class Home(WebRoot):
return json.dumps({'result': 'failure'})
# try do download subtitles for that episode
previous_subtitles = set(babelfish.Language.fromalpha2(x) for x in ep_obj.subtitles)
previous_subtitles = ep_obj.subtitles
try:
ep_obj.subtitles = set(babelfish.Language.fromalpha2(x) for x in ep_obj.downloadSubtitles().values()[0])
ep_obj.downloadSubtitles()
except:
return json.dumps({'result': 'failure'})
# return the correct json value
if previous_subtitles != ep_obj.subtitles:
newSubtitles = list(set(ep_obj.subtitles.split(',')) - set(previous_subtitles.split(',')))
if len(newSubtitles):
newLangs = [babelfish.Language.fromietf(x) for x in newSubtitles]
status = 'New subtitles downloaded: %s' % ' '.join([
"<img src='" + sickbeard.WEB_ROOT + "/images/flags/" + x.alpha2 +
".png' alt='" + x.name + "'/>" for x in
sorted(list(ep_obj.subtitles.difference(previous_subtitles)))])
".png' alt='" + x.name + "'/>" for x in newLangs])
else:
status = 'No subtitles downloaded'
ui.notifications.message('Subtitles Search', status)
return json.dumps({'result': status, 'subtitles': ','.join(sorted([x.alpha2 for x in
ep_obj.subtitles.union(
previous_subtitles)]))})
return json.dumps({'result': status, 'subtitles': ','.join(sorted(ep_obj.subtitles.split(',')))})
def setSceneNumbering(self, show, indexer, forSeason=None, forEpisode=None, forAbsolute=None, sceneSeason=None,
sceneEpisode=None, sceneAbsolute=None):
......
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