Skip to content

Commit ae4972f

Browse files
committed
Create option to choose Widevine version to install from repo.
1 parent 7297b79 commit ae4972f

File tree

7 files changed

+73
-25
lines changed

7 files changed

+73
-25
lines changed

lib/inputstreamhelper/__init__.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
from .kodiutils import (addon_version, browsesingle, delete, exists, get_proxies, get_setting, get_setting_bool, get_setting_float, get_setting_int, jsonrpc,
1010
kodi_to_ascii, kodi_version, listdir, localize, log, notification, ok_dialog, progress_dialog, select_dialog,
1111
set_setting, set_setting_bool, textviewer, translate_path, yesno_dialog)
12-
from .utils import arch, http_download, parse_version, remove_tree, store, system_os, temp_path, unzip, userspace64
12+
from .utils import arch, download_path, http_download, parse_version, remove_tree, store, system_os, temp_path, unzip, userspace64
1313
from .widevine.arm import dl_extract_widevine, extract_widevine, install_widevine_arm
14-
from .widevine.widevine import (backup_path, cdm_from_repo, 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)
14+
from .widevine.widevine import (backup_path, cdm_from_repo, choose_widevine_from_repo, has_widevinecdm, ia_cdm_path, install_cdm_from_backup, latest_widevine_available_from_repo,
15+
latest_widevine_version, load_widevine_config, missing_widevine_libs, widevines_available_from_repo, 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()
@@ -173,12 +173,15 @@ def _supports_widevine(self):
173173
return True
174174

175175
@staticmethod
176-
def _install_widevine_from_repo(bpath):
176+
def _install_widevine_from_repo(bpath, choose_version=False):
177177
"""Install Widevine CDM from Google's library CDM repository"""
178-
cdm = latest_available_widevine_from_repo()
178+
if choose_version:
179+
cdm = choose_widevine_from_repo()
180+
else:
181+
cdm = latest_widevine_available_from_repo()
179182
cdm_version = cdm.get('version')
180183

181-
if not store('download_path'):
184+
if not exists(download_path(cdm.get('url'))):
182185
downloaded = http_download(cdm.get('url'))
183186
else:
184187
downloaded = True
@@ -211,7 +214,7 @@ def install_and_finish(self, progress, version):
211214
return False
212215

213216
@cleanup_decorator
214-
def install_widevine(self):
217+
def install_widevine(self, choose_version=False):
215218
"""Wrapper function that calls Widevine installer method depending on architecture"""
216219
if not self._supports_widevine():
217220
return False
@@ -220,8 +223,10 @@ def install_widevine(self):
220223
return False
221224

222225
if cdm_from_repo():
223-
result = self._install_widevine_from_repo(backup_path())
226+
result = self._install_widevine_from_repo(backup_path(), choose_version=choose_version)
224227
else:
228+
if choose_version:
229+
log(1, "Choosing a version to install is only implemented if the lib is found in googles repo.")
225230
result = install_widevine_arm(backup_path())
226231
if not result:
227232
return result
@@ -310,7 +315,7 @@ def _update_widevine(self):
310315
elif cdm_from_repo():
311316
component = 'Widevine CDM'
312317
current_version = wv_config['version']
313-
latest_version = latest_available_widevine_from_repo().get('version')
318+
latest_version = latest_widevine_available_from_repo().get('version')
314319
else:
315320
component = 'Chrome OS'
316321
current_version = wv_config['version']

lib/inputstreamhelper/api.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ def run(params):
1111
"""Route to API method"""
1212
if 2 <= len(params) <= 4:
1313
if params[1] == 'widevine_install':
14-
widevine_install()
14+
if len(params) == 3:
15+
widevine_install(choose_version=params[2])
16+
else:
17+
widevine_install()
1518
elif params[1] == 'widevine_remove':
1619
widevine_remove()
1720
elif params[1] in ('rollback', 'widevine_rollback'):
@@ -39,9 +42,10 @@ def check_inputstream(protocol, drm=None):
3942
Helper(protocol, drm=drm).check_inputstream()
4043

4144

42-
def widevine_install():
45+
def widevine_install(choose_version=False):
4346
"""The API interface to install Widevine CDM"""
44-
Helper('mpd', drm='widevine').install_widevine()
47+
choose_version = choose_version in ("True", "true")
48+
Helper('mpd', drm='widevine').install_widevine(choose_version=choose_version)
4549

4650

4751
def widevine_install_from():

lib/inputstreamhelper/kodiutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def select_dialog(heading='', opt_list=None, autoclose=0, preselect=-1, useDetai
137137
from xbmcgui import Dialog
138138
if not heading:
139139
heading = ADDON.getAddonInfo('name')
140-
return Dialog().select(heading, opt_list, autoclose=autoclose, preselect=preselect, useDetails=useDetails)
140+
return Dialog().select(heading, [str(opt) for opt in opt_list], autoclose=autoclose, preselect=preselect, useDetails=useDetails)
141141

142142

143143
def textviewer(heading='', text='', usemono=False):

lib/inputstreamhelper/utils.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ def update_temp_path(new_temp_path):
4141
move(old_temp_path, temp_path())
4242

4343

44+
def download_path(url):
45+
"""Choose download target directory based on url."""
46+
filename = url.split('/')[-1]
47+
48+
return os.path.join(temp_path(), filename)
49+
50+
4451
def _http_request(url, headers=None, time_out=10):
4552
"""Perform an HTTP request and return request"""
4653
log(0, 'Request URL: {url}', url=url)
@@ -102,11 +109,11 @@ def http_download(url, message=None, checksum=None, hash_alg='sha1', dl_size=Non
102109
if req is None:
103110
return None
104111

105-
filename = url.split('/')[-1]
112+
dl_path = download_path(url)
113+
filename = os.path.basename(dl_path)
106114
if not message: # display "downloading [filename]"
107115
message = localize(30015, filename=filename) # Downloading file
108116

109-
download_path = os.path.join(temp_path(), filename)
110117
total_length = int(req.info().get('content-length'))
111118
if dl_size and dl_size != total_length:
112119
log(2, 'The given file size does not match the request!')
@@ -120,7 +127,7 @@ def http_download(url, message=None, checksum=None, hash_alg='sha1', dl_size=Non
120127

121128
starttime = time()
122129
chunk_size = 32 * 1024
123-
with open(compat_path(download_path), 'wb') as image:
130+
with open(compat_path(dl_path), 'wb') as image:
124131
size = 0
125132
while size < total_length:
126133
try:
@@ -163,7 +170,7 @@ def http_download(url, message=None, checksum=None, hash_alg='sha1', dl_size=Non
163170
log(4, 'Download failed, checksums do not match!')
164171
return False
165172

166-
if dl_size and stat_file(download_path).st_size() != dl_size:
173+
if dl_size and stat_file(dl_path).st_size() != dl_size:
167174
progress.close()
168175
req.close()
169176
free_space = sizeof_fmt(diskspace())
@@ -172,7 +179,7 @@ def http_download(url, message=None, checksum=None, hash_alg='sha1', dl_size=Non
172179

173180
progress.close()
174181
req.close()
175-
store('download_path', download_path)
182+
store('download_path', dl_path)
176183
return True
177184

178185

lib/inputstreamhelper/widevine/widevine.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from time import time
88

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

@@ -29,7 +29,7 @@ def install_cdm_from_backup(version):
2929
def widevine_eula():
3030
"""Displays the Widevine EULA and prompts user to accept it."""
3131
if cdm_from_repo():
32-
cdm_version = latest_available_widevine_from_repo().get('version')
32+
cdm_version = latest_widevine_available_from_repo().get('version')
3333
cdm_os = config.WIDEVINE_OS_MAP[system_os()]
3434
cdm_arch = config.WIDEVINE_ARCH_MAP_REPO[arch()]
3535
else: # Grab the license from the x86 files
@@ -176,9 +176,8 @@ def latest_widevine_version(eula=False):
176176
return ''
177177
return arm_device.get('version')
178178

179-
180-
def latest_available_widevine_from_repo():
181-
"""Returns the latest available Widevine CDM version and url from Google's library CDM repository"""
179+
def widevines_available_from_repo():
180+
"""Returns all available Widevine CDM versions and urls from Google's library CDM repository"""
182181
cdm_versions = http_get(config.WIDEVINE_VERSIONS_URL).strip('\n').split('\n')
183182
cdm_os = config.WIDEVINE_OS_MAP[system_os()]
184183
cdm_arch = config.WIDEVINE_ARCH_MAP_REPO[arch()]
@@ -188,8 +187,37 @@ def latest_available_widevine_from_repo():
188187
http_status = http_head(cdm_url)
189188
if http_status == 200:
190189
available_cdms.append({'version': cdm_version, 'url': cdm_url})
191-
return available_cdms[-1]
192190

191+
return available_cdms
192+
193+
def latest_widevine_available_from_repo(available_cdms=None):
194+
"""Returns the latest available Widevine CDM version and url from Google's library CDM repository"""
195+
if not available_cdms:
196+
available_cdms = widevines_available_from_repo()
197+
latest = available_cdms[-1] # That's probably correct, but the following for loop makes sure
198+
for cdm in available_cdms:
199+
if parse_version(cdm['version']) > parse_version(latest['version']):
200+
latest = cdm
201+
202+
return latest
203+
204+
def choose_widevine_from_repo():
205+
"""Choose from the widevine versions available in Google's library CDM repository"""
206+
available_cdms = widevines_available_from_repo()
207+
latest = latest_widevine_available_from_repo(available_cdms)
208+
209+
opts = tuple(cdm['version'] for cdm in available_cdms)
210+
preselect = opts.index(latest['version'])
211+
212+
version_index = select_dialog(localize(30069), opts, preselect=preselect)
213+
if version_index == -1:
214+
log(1, 'User did not choose a version to install!')
215+
return False
216+
217+
cdm = available_cdms[version_index]
218+
log(0, 'User chose to install Widevine version {version} from {url}', version=cdm['version'], url=cdm['url'])
219+
220+
return cdm
193221

194222
def remove_old_backups(bpath):
195223
"""Removes old Widevine backups, if number of allowed backups is exceeded"""

resources/language/resource.language.en_gb/strings.po

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ msgctxt "#30068"
265265
msgid "On ARM64 with 64bit userspace, a newer version of {addon} is needed to use [B]Widevine CDM[/B] natively. Please either switch to a 32-bit userspace or install {addon} in version {version} or later."
266266
msgstr ""
267267

268+
msgctxt "#30069"
269+
msgid "Choose which version to install"
270+
msgstr ""
271+
268272

269273
### INFORMATION DIALOG
270274
msgctxt "#30800"

resources/settings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<setting label="30907" help="30908" type="folder" id="temp_path" source="" option="writeable" default="special://masterprofile/addon_data/script.module.inputstreamhelper" visible="![System.Platform.Android | String.StartsWith(System.BuildVersion, 17)]"/>
1313
<setting label="30913" help="30914" type="slider" id="backups" default="4" range="0,1,20" option="int" visible="![System.Platform.Android | String.StartsWith(System.BuildVersion, 17)]"/>
1414
<setting type="sep" visible="![System.Platform.Android | String.StartsWith(System.BuildVersion, 17)]"/>
15-
<setting label="30909" help="30910" type="action" action="RunScript(script.module.inputstreamhelper, widevine_install)" enable="String.StartsWith(System.BuildVersion,18) + System.HasAddon(inputstream.adaptive) | System.AddonIsEnabled(inputstream.adaptive)" visible="![System.Platform.Android | String.StartsWith(System.BuildVersion, 17)]"/>
15+
<setting label="30909" help="30910" type="action" action="RunScript(script.module.inputstreamhelper, widevine_install, True)" enable="String.StartsWith(System.BuildVersion,18) + System.HasAddon(inputstream.adaptive) | System.AddonIsEnabled(inputstream.adaptive)" visible="![System.Platform.Android | String.StartsWith(System.BuildVersion, 17)]"/>
1616
<setting label="30911" help="30912" type="action" action="RunScript(script.module.inputstreamhelper, widevine_remove)" enable="String.StartsWith(System.BuildVersion,18) + System.HasAddon(inputstream.adaptive) | System.AddonIsEnabled(inputstream.adaptive)" visible="![System.Platform.Android | String.StartsWith(System.BuildVersion, 17)]"/>
1717
<setting label="30915" help="30916" type="action" action="RunScript(script.module.inputstreamhelper, rollback)" enable="String.StartsWith(System.BuildVersion,18) + System.HasAddon(inputstream.adaptive) | System.AddonIsEnabled(inputstream.adaptive)" visible="![System.Platform.Android | String.StartsWith(System.BuildVersion, 17)]"/>
1818
</category>

0 commit comments

Comments
 (0)