From 7a1a7843e6ea05422cd482058d477f398d9dc9b8 Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 19 Jun 2022 15:55:46 -0400 Subject: [PATCH] Download all external subs when playback starts --- resources/lib/play_utils.py | 18 ++++++++++++------ resources/lib/utils.py | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/resources/lib/play_utils.py b/resources/lib/play_utils.py index 87eb4a3..ebdcfe7 100644 --- a/resources/lib/play_utils.py +++ b/resources/lib/play_utils.py @@ -15,7 +15,7 @@ from six.moves.urllib.parse import urlencode from .jellyfin import api from .lazylogger import LazyLogger from .dialogs import ResumeDialog -from .utils import send_event_notification, convert_size, get_device_id, translate_string, load_user_details, translate_path +from .utils import send_event_notification, convert_size, get_device_id, translate_string, load_user_details, translate_path, download_external_sub from .kodi_utils import HomeWindow from .datamanager import clear_old_cache_data from .item_functions import extract_item_info, add_gui_item, get_art @@ -838,12 +838,18 @@ def external_subs(media_source, list_item, item_id): language = stream.get('Language', '') codec = stream.get('Codec', '') - url_root = '{}/Videos/{}/{}/Subtitles/{}'.format(server, item_id, source_id, index) + url = '{}{}'.format(server, stream.get('DeliveryUrl')) if language: - url = '{}/0/Stream.{}.{}?api_key={}'.format( - url_root, language, codec, token) + ''' + Starting in 10.8, the server no longer provides language + specific download points. We have to download the file + and name it with the language code ourselves so Kodi + will parse it correctly + ''' + subtitle_file = download_external_sub(language, codec, url) else: - url = '{}/0/Stream.{}?api_key={}'.format(url_root, codec, token) + # If there is no language defined, we can go directly to the server + subtitle_file = url default = "" if stream['IsDefault']: @@ -855,7 +861,7 @@ def external_subs(media_source, list_item, item_id): sub_name = '{} ( {} ) {} {}'.format(language, codec, default, forced) sub_names.append(sub_name) - externalsubs.append(url) + externalsubs.append(subtitle_file) if len(externalsubs) == 0: return diff --git a/resources/lib/utils.py b/resources/lib/utils.py index bbe5746..f2fd64f 100644 --- a/resources/lib/utils.py +++ b/resources/lib/utils.py @@ -15,6 +15,7 @@ import time import math import os import hashlib +import requests from datetime import datetime from dateutil import tz import re @@ -363,3 +364,18 @@ def translate_path(path): return xbmcvfs.translatePath(path) else: return xbmc.translatePath(path) + + +def download_external_sub(language, codec, url): + + # Download the subtitle file + r = requests.get(url) + r.raise_for_status() + + # Write the subtitle file to the local filesystem + file_name = 'Stream.{}.{}'.format(language, codec) + file_path = py2_decode(translate_path('special://temp/{}'.format(file_name))) + with open(file_path, 'wb') as f: + f.write(r.content) + + return file_path