From 04dfd78a9bc124a49d919da28b6ce8da1bc9887b Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Mon, 1 May 2017 13:07:41 +0200 Subject: [PATCH] Fix for not detecting external subtitle language --- resources/lib/PlexAPI.py | 59 ++++++++++++++++++++++++++++++-------- resources/lib/variables.py | 3 ++ 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/resources/lib/PlexAPI.py b/resources/lib/PlexAPI.py index c80e6992..1d5261a6 100644 --- a/resources/lib/PlexAPI.py +++ b/resources/lib/PlexAPI.py @@ -43,7 +43,7 @@ from os import path as os_path import xbmcgui from xbmc import sleep, executebuiltin -from xbmcvfs import exists +from xbmcvfs import exists, mkdirs import clientinfo as client from downloadutils import DownloadUtils @@ -2301,24 +2301,58 @@ class API(): return kodiindex = 0 for stream in mediastreams: - index = stream.attrib['id'] # Since plex returns all possible tracks together, have to pull - # only external subtitles. + # only external subtitles - only for these a 'key' exists + if stream.attrib.get('streamType') != "3": + # Not a subtitle + continue + # Only set for additional external subtitles NOT lying beside video key = stream.attrib.get('key') - # IsTextSubtitleStream if true, is available to download from plex. - if stream.attrib.get('streamType') == "3" and key: - # Direct stream - url = ("%s%s" % (self.server, key)) - url = self.addPlexCredentialsToUrl(url) + # Only set for dedicated subtitle files lying beside video + # ext = stream.attrib.get('format') + if key: + # We do know the language - temporarily download + if stream.attrib.get('languageCode') is not None: + path = self.__download_external_subtitles( + "{server}%s" % key, + "subtitle.%s.%s" % (stream.attrib['languageCode'], + stream.attrib['codec'])) + # We don't know the language - no need to download + else: + path = self.addPlexCredentialsToUrl( + "%s%s" % (self.server, key)) # map external subtitles for mapping - mapping[kodiindex] = index - externalsubs.append(url) + mapping[kodiindex] = stream.attrib['id'] + externalsubs.append(path) kodiindex += 1 mapping = dumps(mapping) window('plex_%s.indexMapping' % playurl, value=mapping) log.info('Found external subs: %s' % externalsubs) return externalsubs + @staticmethod + def __download_external_subtitles(url, filename): + """ + One cannot pass the subtitle language for ListItems. Workaround; will + download the subtitle at url to the Kodi PKC directory in a temp dir + + Returns the path to the downloaded subtitle or None + """ + if not exists(v.EXTERNAL_SUBTITLE_TEMP_PATH): + mkdirs(v.EXTERNAL_SUBTITLE_TEMP_PATH) + path = os_path.join(v.EXTERNAL_SUBTITLE_TEMP_PATH, filename) + r = DownloadUtils().downloadUrl(url, return_response=True) + try: + r.status_code + except AttributeError: + log.error('Could not temporarily download subtitle %s' % url) + return + else: + r.encoding = 'utf-8' + with open(path, 'wb') as f: + f.write(r.content) + return path + def GetKodiPremierDate(self): """ Takes Plex' originallyAvailableAt of the form "yyyy-mm-dd" and returns @@ -2601,6 +2635,7 @@ class API(): # Append external subtitles to stream playmethod = window('%s.playmethod' % plexitem) + # Direct play automatically appends external + # BUT: Plex may add additional subtitles NOT if playmethod in ("DirectStream", "DirectPlay"): - subtitles = self.externalSubs(playurl) - listitem.setSubtitles(subtitles) + listitem.setSubtitles(self.externalSubs(playurl)) diff --git a/resources/lib/variables.py b/resources/lib/variables.py index 8b1b8e2d..0e032981 100644 --- a/resources/lib/variables.py +++ b/resources/lib/variables.py @@ -95,6 +95,9 @@ DB_TEXTURE_PATH = tryDecode(xbmc.translatePath( DB_PLEX_PATH = tryDecode(xbmc.translatePath("special://database/plex.db")) +EXTERNAL_SUBTITLE_TEMP_PATH = tryDecode(xbmc.translatePath( + "special://profile/addon_data/%s/temp/" % ADDON_ID)) + # Multiply Plex time by this factor to receive Kodi time PLEX_TO_KODI_TIMEFACTOR = 1.0 / 1000.0