From 73d6bfde890fc3033bcf5dad28f5f2d44adb82b7 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Thu, 10 Aug 2017 21:05:46 +0200 Subject: [PATCH] Fix playback not starting in some circumstances - Fixes #330 --- resources/lib/PlexAPI.py | 13 +++++++++---- resources/lib/utils.py | 10 ++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/resources/lib/PlexAPI.py b/resources/lib/PlexAPI.py index 65db0967..f03b0d80 100644 --- a/resources/lib/PlexAPI.py +++ b/resources/lib/PlexAPI.py @@ -49,7 +49,7 @@ from xbmcvfs import exists import clientinfo as client from downloadutils import DownloadUtils from utils import window, settings, language as lang, tryDecode, tryEncode, \ - DateToKodi, exists_dir + DateToKodi, exists_dir, slugify from PlexFunctions import PMSHttpsEnabled import plexdb_functions as plexdb import variables as v @@ -2395,9 +2395,14 @@ class API(): 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) + log.debug('Writing temp subtitle to %s' % path) + try: + with open(path, 'wb') as f: + f.write(r.content) + except UnicodeEncodeError: + log.debug('Need to slugify the filename %s' % path) + with open(slugify(path), 'wb') as f: + f.write(r.content) return path def GetKodiPremierDate(self): diff --git a/resources/lib/utils.py b/resources/lib/utils.py index 27354384..ee1631d1 100644 --- a/resources/lib/utils.py +++ b/resources/lib/utils.py @@ -221,6 +221,16 @@ def tryDecode(string, encoding='utf-8'): return string +def slugify(text): + """ + Normalizes text (in unicode or string) to e.g. enable safe filenames. + Returns unicode + """ + if not isinstance(text, unicode): + text = unicode(text) + return unicode(normalize('NFKD', text).encode('ascii', 'ignore')) + + def escape_html(string): """ Escapes the following: