Skip to content

Commit fd4720f

Browse files
Improve getting Widevine CDM from repo (#436)
1 parent ed824c2 commit fd4720f

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

lib/inputstreamhelper/__init__.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
set_setting, set_setting_bool, textviewer, translate_path, yesno_dialog)
1212
from .utils import arch, http_download, remove_tree, run_cmd, store, system_os, temp_path, unzip
1313
from .widevine.arm import install_widevine_arm, unmount
14-
from .widevine.widevine import (backup_path, has_widevinecdm, ia_cdm_path, install_cdm_from_backup, latest_widevine_version,
15-
load_widevine_config, missing_widevine_libs, widevine_config_path, widevine_eula, widevinecdm_path)
14+
from .widevine.widevine import (backup_path, has_widevinecdm, ia_cdm_path, install_cdm_from_backup, latest_available_widevine_from_repo,
15+
latest_widevine_version, load_widevine_config, missing_widevine_libs, widevine_config_path, widevine_eula, widevinecdm_path)
1616
from .unicodes import compat_path
1717

1818
# NOTE: Work around issue caused by platform still using os.popen()
@@ -171,14 +171,11 @@ def _supports_widevine():
171171
@staticmethod
172172
def _install_widevine_x86(bpath):
173173
"""Install Widevine CDM on x86 based architectures."""
174-
cdm_version = latest_widevine_version()
174+
cdm = latest_available_widevine_from_repo()
175+
cdm_version = cdm.get('version')
175176

176177
if not store('download_path'):
177-
cdm_os = config.WIDEVINE_OS_MAP[system_os()]
178-
cdm_arch = config.WIDEVINE_ARCH_MAP_X86[arch()]
179-
url = config.WIDEVINE_DOWNLOAD_URL.format(version=cdm_version, os=cdm_os, arch=cdm_arch)
180-
181-
downloaded = http_download(url)
178+
downloaded = http_download(cdm.get('url'))
182179
else:
183180
downloaded = True
184181

@@ -279,13 +276,13 @@ def _update_widevine(self):
279276
elif 'x86' in arch():
280277
component = 'Widevine CDM'
281278
current_version = wv_config['version']
279+
latest_version = latest_available_widevine_from_repo().get('version')
282280
else:
283281
component = 'Chrome OS'
284282
current_version = wv_config['version']
285-
286-
latest_version = latest_widevine_version()
283+
latest_version = latest_widevine_version()
287284
if not latest_version:
288-
log(3, 'Updating widevine failed. Could not determine latest version.')
285+
log(3, 'Updating Widevine CDM failed. Could not determine latest version.')
289286
return
290287

291288
log(0, 'Latest {component} version is {version}', component=component, version=latest_version)

lib/inputstreamhelper/utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ def http_get(url):
7373
return content.decode()
7474

7575

76+
def http_head(url):
77+
"""Perform an HTTP HEAD request and return status code"""
78+
req = Request(url)
79+
req.get_method = lambda: 'HEAD'
80+
try:
81+
resp = urlopen(req)
82+
return resp.getcode()
83+
except HTTPError as exc:
84+
return exc.getcode()
85+
86+
7687
def http_download(url, message=None, checksum=None, hash_alg='sha1', dl_size=None, background=False): # pylint: disable=too-many-statements
7788
"""Makes HTTP request and displays a progress dialog on download."""
7889
if checksum:

lib/inputstreamhelper/widevine/widevine.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from .. import config
1010
from ..kodiutils import addon_profile, exists, get_setting_int, listdir, localize, log, mkdirs, ok_dialog, open_file, set_setting, translate_path, yesno_dialog
11-
from ..utils import arch, cmd_exists, hardlink, http_download, http_get, remove_tree, run_cmd, store, system_os
11+
from ..utils import arch, cmd_exists, hardlink, http_download, http_get, http_head, remove_tree, run_cmd, store, system_os
1212
from ..unicodes import compat_path, to_unicode
1313

1414

@@ -28,13 +28,13 @@ def install_cdm_from_backup(version):
2828

2929
def widevine_eula():
3030
"""Displays the Widevine EULA and prompts user to accept it."""
31-
32-
cdm_version = latest_widevine_version(eula=True)
3331
if 'x86' in arch():
32+
cdm_version = latest_available_widevine_from_repo().get('version')
3433
cdm_os = config.WIDEVINE_OS_MAP[system_os()]
3534
cdm_arch = config.WIDEVINE_ARCH_MAP_X86[arch()]
3635
else: # grab the license from the x86 files
3736
log(0, 'Acquiring Widevine EULA from x86 files.')
37+
cdm_version = latest_widevine_version(eula=True)
3838
cdm_os = 'mac'
3939
cdm_arch = 'x64'
4040

@@ -170,6 +170,20 @@ def latest_widevine_version(eula=False):
170170
return arm_device['version']
171171

172172

173+
def latest_available_widevine_from_repo():
174+
"""Returns the latest available Widevine CDM version and url from Google's library CDM repository"""
175+
cdm_versions = http_get(config.WIDEVINE_VERSIONS_URL).strip('\n').split('\n')
176+
cdm_os = config.WIDEVINE_OS_MAP[system_os()]
177+
cdm_arch = config.WIDEVINE_ARCH_MAP_X86[arch()]
178+
available_cdms = []
179+
for cdm_version in cdm_versions:
180+
cdm_url = config.WIDEVINE_DOWNLOAD_URL.format(version=cdm_version, os=cdm_os, arch=cdm_arch)
181+
http_status = http_head(cdm_url)
182+
if http_status == 200:
183+
available_cdms.append(dict(version=cdm_version, url=cdm_url))
184+
return available_cdms[-1]
185+
186+
173187
def remove_old_backups(bpath):
174188
"""Removes old Widevine backups, if number of allowed backups is exceeded"""
175189
from distutils.version import LooseVersion # pylint: disable=import-error,no-name-in-module,useless-suppression

0 commit comments

Comments
 (0)