-
Notifications
You must be signed in to change notification settings - Fork 20
/
kodiwrapper.py
186 lines (142 loc) · 7.35 KB
/
kodiwrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# -*- coding: utf-8 -*-
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, unicode_literals
import inputstreamhelper
import json
import xbmc
import xbmcgui
import xbmcplugin
import xbmcvfs
from resources.lib.kodiwrappers import sortmethod
try:
from urllib.parse import urlencode
except ImportError:
from urllib import urlencode
def has_socks():
''' Test if socks is installed, and remember this information '''
if not hasattr(has_socks, 'installed'):
try:
import socks # 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 has_socks.installed
class KodiWrapper:
def __init__(self, handle, url, addon):
self._handle = handle
self._url = url
self._addon = addon
self._addon_id = addon.getAddonInfo('id')
def show_listing(self, list_items, sort=None, content_type='episodes'):
listing = []
if sort is not None:
kodi_sorts = {sortmethod.ALPHABET: xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE}
kodi_sortmethod = kodi_sorts.get(sort)
xbmcplugin.addSortMethod(handle=self._handle, sortMethod=kodi_sortmethod)
else:
xbmcplugin.addSortMethod(handle=self._handle, sortMethod=xbmcplugin.SORT_METHOD_NONE)
for title_item in list_items:
list_item = xbmcgui.ListItem(label=title_item.title, thumbnailImage=title_item.art_dict.get('thumb'))
url = self._url + '?' + urlencode(title_item.url_dict)
list_item.setProperty('IsPlayable', str(title_item.is_playable))
if title_item.art_dict:
list_item.setArt(title_item.art_dict)
if title_item.video_dict:
list_item.setInfo('video', infoLabels=title_item.video_dict)
listing.append((url, list_item, not title_item.is_playable))
ok = xbmcplugin.addDirectoryItems(self._handle, listing, len(listing))
xbmcplugin.setContent(self._handle, content=content_type)
xbmcplugin.endOfDirectory(self._handle, ok)
def play(self, video):
play_item = xbmcgui.ListItem(path=video.stream_url)
if video.stream_url is not None and video.use_inputstream_adaptive:
play_item.setProperty('inputstreamaddon', 'inputstream.adaptive')
play_item.setProperty('inputstream.adaptive.manifest_type', 'mpd')
play_item.setMimeType('application/dash+xml')
play_item.setContentLookup(False)
if video.license_key is not None:
is_helper = inputstreamhelper.Helper('mpd', drm='com.widevine.alpha')
if is_helper.check_inputstream():
play_item.setProperty('inputstream.adaptive.license_type', 'com.widevine.alpha')
play_item.setProperty('inputstream.adaptive.license_key', video.license_key)
subtitles_visible = self.get_setting('showsubtitles') == 'true'
# Separate subtitle url for hls-streams
if subtitles_visible and video.subtitle_url is not None:
self.log_notice('subtitle ' + video.subtitle_url)
play_item.setSubtitles([video.subtitle_url])
xbmcplugin.setResolvedUrl(self._handle, True, listitem=play_item)
while not xbmc.Player().isPlaying() and not xbmc.Monitor().abortRequested():
xbmc.sleep(100)
xbmc.Player().showSubtitles(subtitles_visible)
def show_ok_dialog(self, title, message):
xbmcgui.Dialog().ok(self._addon.getAddonInfo('name'), title, message)
def get_localized_string(self, string_id):
return self._addon.getLocalizedString(string_id)
def get_localized_dateshort(self):
return xbmc.getRegion('dateshort')
def get_setting(self, setting_id):
return self._addon.getSetting(setting_id)
def open_settings(self):
self._addon.openSettings()
def get_global_setting(self, setting):
json_result = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "Settings.GetSettingValue", "params": {"setting": "%s"}, "id": 1}' % setting)
return json.loads(json_result).get('result', dict()).get('value')
def get_proxies(self):
usehttpproxy = self.get_global_setting('network.usehttpproxy')
if usehttpproxy is False:
return None
httpproxytype = self.get_global_setting('network.httpproxytype')
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:
message = self.get_localized_string(32061)
self.show_ok_dialog('', message)
return None
proxy_types = ['http', 'socks4', 'socks4a', 'socks5', 'socks5h']
if 0 <= httpproxytype <= 5:
httpproxyscheme = proxy_types[httpproxytype]
else:
httpproxyscheme = 'http'
httpproxyserver = self.get_global_setting('network.httpproxyserver')
httpproxyport = self.get_global_setting('network.httpproxyport')
httpproxyusername = self.get_global_setting('network.httpproxyusername')
httpproxypassword = self.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)
# Note: InputStream Adaptive is not pre-installed on Windows and in some cases users can uninstall it
def has_inputstream_adaptive_installed(self):
return xbmc.getCondVisibility('System.HasAddon("{0}")'.format('inputstream.adaptive')) == 1
def can_play_drm(self):
kodi_version = int(xbmc.getInfoLabel('System.BuildVersion').split('.')[0])
return kodi_version > 17
def get_userdata_path(self):
return xbmc.translatePath(self._addon.getAddonInfo('profile'))
def get_addon_path(self):
return xbmc.translatePath(self._addon.getAddonInfo('path'))
def get_path(self, path):
return xbmc.translatePath(path)
def make_dir(self, path):
xbmcvfs.mkdir(path)
def check_if_path_exists(self, path):
return xbmcvfs.exists(path)
def open_path(self, path):
return json.loads(open(path, 'r').read())
def delete_path(self, path):
return xbmcvfs.delete(path)
def log_notice(self, message):
''' Log info messages to Kodi '''
xbmc.log(msg='[%s] %s' % (self._addon_id, message), level=xbmc.LOGNOTICE)
def log_error(self, message):
''' Log error messages to Kodi '''
xbmc.log(msg='[%s] %s' % (self._addon_id, message), level=xbmc.LOGERROR)