diff --git a/sickbeard/webapi.py b/sickbeard/webapi.py index def8e96432bd0e861a6c16cecf87c52389f17fa9..7181dec613c6c5a7f59f81b9ca27b328cb51c784 100644 --- a/sickbeard/webapi.py +++ b/sickbeard/webapi.py @@ -45,6 +45,8 @@ except ImportError: import xml.etree.cElementTree as etree +import subliminal + dateFormat = "%Y-%m-%d" dateTimeFormat = "%Y-%m-%d %H:%M" @@ -981,6 +983,72 @@ class CMD_EpisodeSetStatus(ApiCall): else: return _responds(RESULT_SUCCESS, msg='All status set successfully.' + extra_msg) +class CMD_SubtitleSearch(ApiCall): + _help = {"desc": "search episode subtitles. the response might take some time", + "requiredParameters": {"tvdbid": {"desc": "thetvdb.com unique id of a show"}, + "season": {"desc": "the season number"}, + "episode": {"desc": "the episode number"} + } + } + + def __init__(self, args, kwargs): + # required + self.tvdbid, args = self.check_params(args, kwargs, "tvdbid", None, True, "int", []) + self.s, args = self.check_params(args, kwargs, "season", None, True, "int", []) + self.e, args = self.check_params(args, kwargs, "episode", None, True, "int", []) + # optional + # super, missing, help + ApiCall.__init__(self, args, kwargs) + + def run(self): + """ search episode subtitles """ + showObj = sickbeard.helpers.findCertainShow(sickbeard.showList, int(self.tvdbid)) + if not showObj: + return _responds(RESULT_FAILURE, msg="Show not found") + + # retrieve the episode object and fail if we can't get one + epObj = showObj.getEpisode(int(self.s), int(self.e)) + if isinstance(epObj, str): + return _responds(RESULT_FAILURE, msg="Episode not found") + + # try do download subtitles for that episode + previous_subtitles = epObj.subtitles + + try: + subtitles = epObj.downloadSubtitles() + + 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) + + for subtitle in subtitles.get(video): + 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): + helpers.chmodAsParent(subtitle.path) + except: + return _responds(RESULT_FAILURE, msg='Unable to find subtitles') + + # return the correct json value + if previous_subtitles != epObj.subtitles: + status = 'New subtitles downloaded: %s' % ' '.join(["<img src='"+sickbeard.WEB_ROOT+"/images/flags/"+subliminal.language.Language(x).alpha2+".png' alt='"+subliminal.language.Language(x).name+"'/>" for x in sorted(list(set(epObj.subtitles).difference(previous_subtitles)))]) + response = _responds(RESULT_SUCCESS, msg='New subtitles found') + else: + status = 'No subtitles downloaded' + response = _responds(RESULT_FAILURE, msg='Unable to find subtitles') + + ui.notifications.message('Subtitles Search', status) + + return response + class CMD_Exceptions(ApiCall): _help = {"desc": "display scene exceptions for all or a given show", @@ -2413,6 +2481,7 @@ _functionMaper = {"help": CMD_Help, "episode": CMD_Episode, "episode.search": CMD_EpisodeSearch, "episode.setstatus": CMD_EpisodeSetStatus, + "episode.subtitlesearch": CMD_SubtitleSearch, "exceptions": CMD_Exceptions, "history": CMD_History, "history.clear": CMD_HistoryClear, @@ -2448,5 +2517,5 @@ _functionMaper = {"help": CMD_Help, "show.stats": CMD_ShowStats, "show.update": CMD_ShowUpdate, "shows": CMD_Shows, - "shows.stats": CMD_ShowsStats + "shows.stats": CMD_ShowsStats }