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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
# Gnu General Public License - see LICENSE.TXT
import xbmcaddon
import xbmcgui
import xbmcplugin
import xbmc
import re
import encodings
import string
import random
import urllib
import json
import httplib
import base64
import sys
import binascii
from downloadutils import DownloadUtils
from simple_logging import SimpleLogging
from clientinfo import ClientInformation
from json_rpc import json_rpc
from translation import i18n
from datamanager import DataManager
# define our global download utils
downloadUtils = DownloadUtils()
log = SimpleLogging(__name__)
###########################################################################
class PlayUtils():
def getPlayUrl(self, id, media_source, force_transcode, play_session_id):
log.debug("getPlayUrl")
addonSettings = xbmcaddon.Addon(id='plugin.video.embycon')
playback_type = addonSettings.getSetting("playback_type")
server = downloadUtils.getServer()
log.debug("playback_type: {0}", playback_type)
if force_transcode:
log.debug("playback_type: FORCED_TRANSCODE")
playurl = None
log.debug("play_session_id: {0}", play_session_id)
media_source_id = media_source.get("Id")
log.debug("media_source_id: {0}", media_source_id)
is_h265 = False
streams = media_source.get("MediaStreams", [])
for stream in streams:
if stream.get("Type", "") == "Video" and stream.get("Codec", "") in ["hevc", "h265"]:
is_h265 = True
break
if is_h265:
log.debug("H265_IS_TRUE")
h265_action = addonSettings.getSetting("h265_action")
if h265_action == "1":
log.debug("H265 override play action: setting to Direct Streaming")
playback_type = "1"
elif h265_action == "2":
log.debug("H265 override play action: setting to Transcode Streaming")
playback_type = "2"
if force_transcode:
playback_type = "2"
# transcode
if playback_type == "2":
playback_bitrate = addonSettings.getSetting("playback_bitrate")
log.debug("playback_bitrate: {0}", playback_bitrate)
playback_max_width = addonSettings.getSetting("playback_max_width")
playback_video_force_8 = addonSettings.getSetting("playback_video_force_8") == "true"
clientInfo = ClientInformation()
deviceId = clientInfo.getDeviceId()
bitrate = int(playback_bitrate) * 1000
user_token = downloadUtils.authenticate()
playurl = ("%s/emby/Videos/%s/master.m3u8" +
"?MediaSourceId=%s" +
"&PlaySessionId=%s" +
"&VideoCodec=h264" +
"&AudioCodec=ac3" +
"&MaxAudioChannels=6" +
"&deviceId=%s" +
"&VideoBitrate=%s" +
"&maxWidth=%s")
playurl = playurl % (server, id, media_source_id, play_session_id, deviceId, bitrate, playback_max_width)
if playback_video_force_8:
playurl = playurl + "&MaxVideoBitDepth=8"
playurl = playurl + "&api_key=" + user_token
# do direct path playback
elif playback_type == "0":
playurl = media_source.get("Path")
# handle DVD structure
if (media_source.get("VideoType") == "Dvd"):
playurl = playurl + "/VIDEO_TS/VIDEO_TS.IFO"
elif (media_source.get("VideoType") == "BluRay"):
playurl = playurl + "/BDMV/index.bdmv"
smb_username = addonSettings.getSetting('smbusername')
smb_password = addonSettings.getSetting('smbpassword')
# add smb creds
if smb_username == '':
playurl = playurl.replace("\\\\", "smb://")
else:
playurl = playurl.replace("\\\\", "smb://" + smb_username + ':' + smb_password + '@')
playurl = playurl.replace("\\", "/")
# do direct http streaming playback
elif playback_type == "1":
playurl = ("%s/emby/Videos/%s/stream" +
"?static=true" +
"&PlaySessionId=%s" +
"&MediaSourceId=%s")
playurl = playurl % (server, id, play_session_id, media_source_id)
user_token = downloadUtils.authenticate()
playurl = playurl + "&api_key=" + user_token
log.debug("Playback URL: {0}", playurl)
return playurl, playback_type
def getStrmDetails(self, media_source):
playurl = None
listitem_props = []
contents = media_source.get('Path').encode('utf-8') # contains contents of strm file with linebreaks
line_break = '\r'
if '\r\n' in contents:
line_break = '\r\n'
elif '\n' in contents:
line_break = '\n'
lines = contents.split(line_break)
for line in lines:
line = line.strip()
log.debug("STRM Line: {0}", line)
if line.startswith('#KODIPROP:'):
match = re.search('#KODIPROP:(?P<item_property>[^=]+?)=(?P<property_value>.+)', line)
if match:
item_property = match.group('item_property')
property_value = match.group('property_value')
log.debug("STRM property found: {0} value: {1}", item_property, property_value)
listitem_props.append((item_property, property_value))
else:
log.debug("STRM #KODIPROP incorrect format")
elif line.startswith('#'):
# unrecognized, treat as comment
log.debug("STRM unrecognized line identifier, ignored")
elif line != '':
playurl = line
log.debug("STRM playback url found")
log.debug("Playback URL: {0} ListItem Properties: {1}", playurl, listitem_props)
return playurl, listitem_props
def getChecksum(item):
userdata = item['UserData']
checksum = "%s_%s_%s_%s_%s_%s_%s" % (
item['Etag'],
userdata['Played'],
userdata['IsFavorite'],
userdata.get('Likes', "-"),
userdata['PlaybackPositionTicks'],
userdata.get('UnplayedItemCount', "-"),
userdata.get("PlayedPercentage", "-")
)
return checksum
def getArt(item, server, widget=False):
art = {
'thumb': '',
'fanart': '',
'poster': '',
'banner': '',
'clearlogo': '',
'clearart': '',
'discart': '',
'landscape': '',
'tvshow.fanart': '',
'tvshow.poster': '',
'tvshow.clearart': '',
'tvshow.clearlogo': '',
'tvshow.banner': '',
'tvshow.landscape': ''
}
item_id = item["Id"]
image_id = item_id
imageTags = item["ImageTags"]
if imageTags is not None and imageTags["Primary"] is not None:
image_tag = imageTags["Primary"]
if widget:
art['thumb'] = downloadUtils.imageUrl(image_id, "Primary", 0, 400, 400, image_tag, server=server)
else:
art['thumb'] = downloadUtils.getArtwork(item, "Primary", server=server)
item_type = item["Type"]
if item_type == "Episode" or item_type == "Season":
art['tvshow.poster'] = downloadUtils.getArtwork(item, "Primary", parent=True, server=server)
art['tvshow.clearart'] = downloadUtils.getArtwork(item, "Art", parent=True, server=server)
art['tvshow.clearlogo'] = downloadUtils.getArtwork(item, "Logo", parent=True, server=server)
art['tvshow.banner'] = downloadUtils.getArtwork(item, "Banner", parent=True, server=server)
art['tvshow.landscape'] = downloadUtils.getArtwork(item, "Thumb", parent=True, server=server)
art['tvshow.fanart'] = downloadUtils.getArtwork(item, "Backdrop", parent=True, server=server)
elif item_type == "Series":
art['tvshow.poster'] = downloadUtils.getArtwork(item, "Primary", parent=False, server=server)
art['tvshow.clearart'] = downloadUtils.getArtwork(item, "Art", parent=False, server=server)
art['tvshow.clearlogo'] = downloadUtils.getArtwork(item, "Logo", parent=False, server=server)
art['tvshow.banner'] = downloadUtils.getArtwork(item, "Banner", parent=False, server=server)
art['tvshow.landscape'] = downloadUtils.getArtwork(item, "Thumb", parent=False, server=server)
art['tvshow.fanart'] = downloadUtils.getArtwork(item, "Backdrop", parent=False, server=server)
if item_type == "Episode":
art['landscape'] = downloadUtils.getArtwork(item, "Thumb", parent=True, server=server)
else:
art['poster'] = art['thumb']
art['fanart'] = downloadUtils.getArtwork(item, "Backdrop", server=server)
if not art['fanart']:
art['fanart'] = downloadUtils.getArtwork(item, "Backdrop", parent=True, server=server)
if not art['landscape']:
art['landscape'] = downloadUtils.getArtwork(item, "Thumb", server=server)
if not art['landscape']:
art['landscape'] = art['fanart']
if not art['thumb']:
art['thumb'] = art['landscape']
art['banner'] = downloadUtils.getArtwork(item, "Banner", server=server)
art['clearlogo'] = downloadUtils.getArtwork(item, "Logo", server=server)
art['clearart'] = downloadUtils.getArtwork(item, "Art", server=server)
art['discart'] = downloadUtils.getArtwork(item, "Disc", server=server)
return art
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
def cache_artwork():
log.debug("cache_artwork")
xbmcplugin.endOfDirectory(int(sys.argv[1]), cacheToDisc=False)
# is the web server enabled
web_query = {"setting": "services.webserver"}
result = json_rpc('Settings.GetSettingValue').execute(web_query)
xbmc_webserver_enabled = result['result']['value']
if not xbmc_webserver_enabled:
xbmcgui.Dialog().ok(i18n('notice'), i18n('http_control'))
return
# get the port
web_port = {"setting": "services.webserverport"}
result = json_rpc('Settings.GetSettingValue').execute(web_port)
xbmc_port = result['result']['value']
log.debug("xbmc_port: {0}", xbmc_port)
# get the user
web_user = {"setting": "services.webserverusername"}
result = json_rpc('Settings.GetSettingValue').execute(web_user)
xbmc_username = result['result']['value']
log.debug("xbmc_username: {0}", xbmc_username)
# get the password
web_pass = {"setting": "services.webserverpassword"}
result = json_rpc('Settings.GetSettingValue').execute(web_pass)
xbmc_password = result['result']['value']
# ask to delete all textures
question_result = xbmcgui.Dialog().yesno(i18n('delete'), i18n('delete_existing'))
if question_result:
pdialog = xbmcgui.DialogProgress()
pdialog.create(i18n('deleting_textures'), "")
index = 0
json_result = json_rpc('Textures.GetTextures').execute()
textures = json_result.get("result", {}).get("textures", [])
log.debug("texture ids: {0}", textures)
total = len(textures)
for texture in textures:
texture_id = texture["textureid"]
params = {"textureid": int(texture_id)}
json_result = json_rpc('Textures.RemoveTexture').execute(params)
percentage = int((float(index) / float(total)) * 100)
message = "%s of %s" % (index, total)
pdialog.update(percentage, "%s" % (message))
index += 1
if pdialog.iscanceled():
break
del textures
del pdialog
question_result = xbmcgui.Dialog().yesno(i18n('cache_all_textures_title'), i18n('cache_all_textures'))
if not question_result:
return
params = {"properties": ["url"]}
json_result = json_rpc('Textures.GetTextures').execute(params)
textures = json_result.get("result", {}).get("textures", [])
texture_urls = set()
for texture in textures:
url = texture.get("url")
url = urllib.unquote(url)
url = url.replace("image://", "")
url = url[0:-1]
texture_urls.add(url)
del textures
del json_result
url = ('{server}/emby/Users/{userid}/Items?' +
'&Recursive=true' +
'&IncludeItemTypes=Movie,Series,Episode,BoxSet' +
'&ImageTypeLimit=1' +
'&format=json')
data_manager = DataManager()
results = data_manager.GetContent(url)
if results is None:
results = []
if isinstance(results, dict):
results = results.get("Items")
server = downloadUtils.getServer()
missing_texture_urls = set()
image_types = ["thumb", "poster", "banner", "clearlogo", "tvshow.poster", "tvshow.banner", "tvshow.landscape"]
for item in results:
art = getArt(item, server)
for image_type in image_types:
image_url = art[image_type]
if image_url not in texture_urls and not image_url.endswith("&Tag=") and len(image_url) > 0:
missing_texture_urls.add(image_url)
log.debug("texture_urls: {0}", texture_urls)
log.debug("missing_texture_urls: {0}", missing_texture_urls)
log.debug("Number of existing textures: {0}", len(texture_urls))
log.debug("Number of missing textures: {0}", len(missing_texture_urls))
kodi_http_server = "localhost:" + str(xbmc_port)
headers = {}
if xbmc_password:
auth = "%s:%s" % (xbmc_username, xbmc_password)
headers = {'Authorization': 'Basic %s' % base64.b64encode(auth)}
pdialog = xbmcgui.DialogProgress()
pdialog.create(i18n('caching_textures'), "")
total = len(missing_texture_urls)
index = 1
count_done = 0
for get_url in missing_texture_urls:
log.debug("texture_url: {0}", get_url)
url = double_urlencode(get_url)
kodi_texture_url = ("/image/image://%s" % url)
log.debug("kodi_texture_url: {0}", kodi_texture_url)
percentage = int((float(index) / float(total)) * 100)
message = "%s of %s" % (index, total)
pdialog.update(percentage, "%s" % (message))
conn = httplib.HTTPConnection(kodi_http_server, timeout=20)
conn.request(method="GET", url=kodi_texture_url, headers=headers)
data = conn.getresponse()
if data.status == 200:
count_done += 1
log.debug("Get Image Result: {0}", data.status)
index += 1
if pdialog.iscanceled():
break
pdialog.close()
del pdialog
report_text = i18n('existing_textures') + str(len(texture_urls)) + "\n"
report_text += i18n('missing_textures') + str(len(missing_texture_urls)) + "\n"
report_text += i18n('loaded_textures') + str(count_done)
xbmcgui.Dialog().ok(i18n('done'), report_text)
def double_urlencode(text):
text = single_urlencode(text)
text = single_urlencode(text)
return text
def single_urlencode(text):
# urlencode needs a utf- string
text = urllib.urlencode({'blahblahblah': text.encode('utf-8')})
text = text[13:]
return text.decode('utf-8') #return the result again as unicode
def send_event_notification(method, data):
next_data = json.dumps(data)
source_id = "embycon"
data = '\\"[\\"{0}\\"]\\"'.format(binascii.hexlify(next_data))
command = 'XBMC.NotifyAll({0}.SIGNAL,{1},{2})'.format(source_id, method, data)
log.debug("Sending notification event data: {0}", command)
xbmc.executebuiltin(command)
|