-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR includes: - Introduce a kodiutils library - Import some library to blocks where they are needed - Import only the stuff we need from libraries - Add proxy-support testing
- Loading branch information
Showing
19 changed files
with
364 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
# -*- coding: utf-8 -*- | ||
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
''' Implements Kodi Helper functions ''' | ||
from __future__ import absolute_import, division, unicode_literals | ||
import xbmc | ||
from xbmcgui import Dialog | ||
from xbmcaddon import Addon | ||
from.unicodehelper import from_unicode, to_unicode | ||
|
||
ADDON = Addon('script.module.inputstreamhelper') | ||
|
||
|
||
class SafeDict(dict): | ||
''' A safe dictionary implementation that does not break down on missing keys ''' | ||
def __missing__(self, key): | ||
''' Replace missing keys with the original placeholder ''' | ||
return '{' + key + '}' | ||
|
||
|
||
def has_socks(): | ||
''' Test if socks is installed, and remember this information ''' | ||
|
||
# If it wasn't stored before, check if socks is installed | ||
if not hasattr(has_socks, 'installed'): | ||
try: | ||
import socks # noqa: F401; pylint: disable=unused-variable,unused-import | ||
has_socks.installed = True | ||
except ImportError: | ||
has_socks.installed = False | ||
return None # Detect if this is the first run | ||
|
||
# Return the stored value | ||
return has_socks.installed | ||
|
||
|
||
def localize(string_id, **kwargs): | ||
''' Return the translated string from the .po language files, optionally translating variables ''' | ||
if kwargs: | ||
import string | ||
return string.Formatter().vformat(ADDON.getLocalizedString(string_id), (), SafeDict(**kwargs)) | ||
|
||
return ADDON.getLocalizedString(string_id) | ||
|
||
|
||
def get_setting(setting_id, default=None): | ||
''' Get an add-on setting ''' | ||
value = to_unicode(ADDON.getSetting(setting_id)) | ||
if value == '' and default is not None: | ||
return default | ||
return value | ||
|
||
|
||
def set_setting(setting_id, setting_value): | ||
''' Set an add-on setting ''' | ||
return ADDON.setSetting(setting_id, setting_value) | ||
|
||
|
||
def get_global_setting(setting): | ||
''' Get a Kodi setting ''' | ||
result = execute_jsonrpc(dict(jsonrpc='2.0', id=1, method='Settings.GetSettingValue', params=dict(setting='%s' % setting))) | ||
return result.get('result', dict()).get('value') | ||
|
||
|
||
def get_proxies(): | ||
''' Return a usable proxies dictionary from Kodi proxy settings ''' | ||
usehttpproxy = get_global_setting('network.usehttpproxy') | ||
if usehttpproxy is not True: | ||
return None | ||
|
||
try: | ||
httpproxytype = int(get_global_setting('network.httpproxytype')) | ||
except ValueError: | ||
httpproxytype = 0 | ||
|
||
socks_supported = has_socks() | ||
if httpproxytype != 0 and not socks_supported: | ||
# Only open the dialog the first time (to avoid multiple popups) | ||
if socks_supported is None: | ||
Dialog().ok('', localize(30042)) # Requires PySocks | ||
return None | ||
|
||
proxy_types = ['http', 'socks4', 'socks4a', 'socks5', 'socks5h'] | ||
if 0 <= httpproxytype < 5: | ||
httpproxyscheme = proxy_types[httpproxytype] | ||
else: | ||
httpproxyscheme = 'http' | ||
|
||
httpproxyserver = get_global_setting('network.httpproxyserver') | ||
httpproxyport = get_global_setting('network.httpproxyport') | ||
httpproxyusername = get_global_setting('network.httpproxyusername') | ||
httpproxypassword = get_global_setting('network.httpproxypassword') | ||
|
||
if httpproxyserver and httpproxyport and httpproxyusername and httpproxypassword: | ||
proxy_address = '%s://%s:%s@%s:%s' % (httpproxyscheme, httpproxyusername, httpproxypassword, httpproxyserver, httpproxyport) | ||
elif httpproxyserver and httpproxyport and httpproxyusername: | ||
proxy_address = '%s://%s@%s:%s' % (httpproxyscheme, httpproxyusername, httpproxyserver, httpproxyport) | ||
elif httpproxyserver and httpproxyport: | ||
proxy_address = '%s://%s:%s' % (httpproxyscheme, httpproxyserver, httpproxyport) | ||
elif httpproxyserver: | ||
proxy_address = '%s://%s' % (httpproxyscheme, httpproxyserver) | ||
else: | ||
return None | ||
|
||
return dict(http=proxy_address, https=proxy_address) | ||
|
||
|
||
def get_userdata_path(): | ||
''' Return the profile's userdata path ''' | ||
return to_unicode(xbmc.translatePath(ADDON.getAddonInfo('profile'))) | ||
|
||
|
||
def get_addon_info(key): | ||
''' Return addon information ''' | ||
return to_unicode(ADDON.getAddonInfo(key)) | ||
|
||
|
||
def execute_jsonrpc(payload): | ||
''' Kodi JSON-RPC request. Return the response in a dictionary. ''' | ||
import json | ||
log('jsonrpc payload: {payload}', payload=payload) | ||
response = xbmc.executeJSONRPC(json.dumps(payload)) | ||
log('jsonrpc response: {response}', response=response) | ||
return json.loads(response) | ||
|
||
|
||
def log(msg, **kwargs): | ||
''' InputStream Helper log method ''' | ||
xbmc.log(msg=from_unicode('[{addon}]: {msg}'.format(addon=get_addon_info('id'), msg=msg.format(**kwargs))), level=xbmc.LOGDEBUG) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
codecov | ||
kodi-addon-checker | ||
polib | ||
proxy.py | ||
pylint | ||
tox-travis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright: (c) 2019, Dag Wieers (@dagwieers) <[email protected]> | ||
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
# pylint: disable=invalid-name,missing-docstring | ||
|
||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright: (c) 2019, Dag Wieers (@dagwieers) <[email protected]> | ||
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
# pylint: disable=duplicate-code,invalid-name,missing-docstring,protected-access | ||
|
||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright: (c) 2019, Dag Wieers (@dagwieers) <[email protected]> | ||
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
# pylint: disable=duplicate-code,invalid-name,missing-docstring,protected-access | ||
|
||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright: (c) 2019, Dag Wieers (@dagwieers) <[email protected]> | ||
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
# pylint: disable=duplicate-code,invalid-name,missing-docstring,protected-access | ||
|
||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright: (c) 2019, Dag Wieers (@dagwieers) <[email protected]> | ||
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
# pylint: disable=duplicate-code,invalid-name,missing-docstring,protected-access | ||
|
||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright: (c) 2019, Dag Wieers (@dagwieers) <[email protected]> | ||
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
# pylint: disable=duplicate-code,invalid-name,missing-docstring,protected-access | ||
|
||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright: (c) 2019, Dag Wieers (@dagwieers) <[email protected]> | ||
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
# pylint: disable=duplicate-code,invalid-name,missing-docstring,protected-access | ||
|
||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
import unittest | ||
import platform | ||
import inputstreamhelper | ||
|
||
xbmc = __import__('xbmc') | ||
xbmcaddon = __import__('xbmcaddon') | ||
xbmcgui = __import__('xbmcgui') | ||
xbmcvfs = __import__('xbmcvfs') | ||
|
||
xbmc.GLOBAL_SETTINGS['network.usehttpproxy'] = True | ||
xbmc.GLOBAL_SETTINGS['network.httpproxytype'] = 0 | ||
xbmc.GLOBAL_SETTINGS['network.httpproxyserver'] = '127.0.0.1' | ||
xbmc.GLOBAL_SETTINGS['network.httpproxyport'] = '8899' | ||
|
||
|
||
class LinuxProxyTests(unittest.TestCase): | ||
|
||
def test_check_inputstream_mpd(self): | ||
inputstreamhelper.system_os = lambda: 'Linux' | ||
platform.machine = lambda: 'x86_64' | ||
is_helper = inputstreamhelper.Helper('mpd', drm='com.widevine.alpha') | ||
is_helper.remove_widevine() | ||
is_installed = is_helper.check_inputstream() | ||
self.assertTrue(is_installed, True) | ||
|
||
def test_check_inputstream_hls_again(self): | ||
inputstreamhelper.system_os = lambda: 'Linux' | ||
platform.machine = lambda: 'AMD64' | ||
platform.architecture = lambda: ['64bit', ''] | ||
is_helper = inputstreamhelper.Helper('hls', drm='com.widevine.alpha') | ||
is_installed = is_helper.check_inputstream() | ||
self.assertTrue(is_installed, True) | ||
|
||
def test_check_inputstream_rtmp(self): | ||
inputstreamhelper.system_os = lambda: 'Linux' | ||
platform.machine = lambda: 'x86_64' | ||
is_helper = inputstreamhelper.Helper('rtmp') | ||
is_installed = is_helper.check_inputstream() | ||
self.assertTrue(is_installed, True) | ||
|
||
def test_check_inputstream_disabled(self): | ||
inputstreamhelper.system_os = lambda: 'Linux' | ||
platform.machine = lambda: 'x86_64' | ||
is_helper = inputstreamhelper.Helper('mpd', drm='com.widevine.alpha') | ||
is_helper.disable() | ||
is_installed = is_helper.check_inputstream() | ||
is_helper.enable() | ||
self.assertTrue(is_installed, True) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
{ | ||
"locale.language": "resource.language.en_gb" | ||
"locale.language": "resource.language.en_gb", | ||
"network.usehttpproxy": false, | ||
"network.httpproxytype": 0, | ||
"network.httpproxyserver": "http://proxy.server/", | ||
"network.httpproxyport": "8899", | ||
"network.httpproxyusername": "", | ||
"network.httpproxypassword": "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.