Fix for not detecting external subtitle language

This commit is contained in:
tomkat83 2017-05-01 13:07:41 +02:00
parent 0b7c9dc820
commit 04dfd78a9b
2 changed files with 50 additions and 12 deletions

View file

@ -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))

View file

@ -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