-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathservice.py
More file actions
422 lines (352 loc) · 15.1 KB
/
service.py
File metadata and controls
422 lines (352 loc) · 15.1 KB
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
418
419
420
421
422
# -*- coding: utf-8 -*-
import sys
import os
# Ensures yt-dlp is on the python path
# Workaround for issue caused by upstream commit
dir_path = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(dir_path, 'lib'))
import json
import sys
import xbmc
import xbmcaddon
import xbmcgui
import xbmcplugin
from urllib.parse import urlparse, parse_qs, urlencode
class replacement_stderr(sys.stderr.__class__):
def isatty(self): return False
sys.stderr.__class__ = replacement_stderr
def debug(content):
log(content, xbmc.LOGDEBUG)
def notice(content):
log(content, xbmc.LOGINFO)
def log(msg, level=xbmc.LOGINFO):
addon = xbmcaddon.Addon()
addonID = addon.getAddonInfo('id')
xbmc.log('%s: %s' % (addonID, msg), level)
# python embedded (as used in kodi) has a known bug for second calls of strptime.
# The python bug is docmumented here https://bugs.python.org/issue27400
# The following workaround patch is borrowed from https://forum.kodi.tv/showthread.php?tid=112916&pid=2914578#pid2914578
def patch_strptime():
import datetime
#fix for datatetime.strptime returns None
class proxydt(datetime.datetime):
@staticmethod
def strptime(date_string, format):
import time
return datetime.datetime(*(time.strptime(date_string, format)[0:6]))
datetime.datetime = proxydt
def showInfoNotification(message):
xbmcgui.Dialog().notification("SendToKodi", message, xbmcgui.NOTIFICATION_INFO, 5000)
def showErrorNotification(message):
xbmcgui.Dialog().notification("SendToKodi", message,
xbmcgui.NOTIFICATION_ERROR, 5000)
# Get the plugin url in plugin:// notation.
__url__ = sys.argv[0]
# Get the plugin handle as an integer number.
__handle__ = int(sys.argv[1])
def getParams():
result = {}
paramstring = sys.argv[2]
additionalParamsIndex = paramstring.find(' ')
if additionalParamsIndex == -1:
result['url'] = paramstring[1:]
result['ydlOpts'] = {}
else:
result['url'] = paramstring[1:additionalParamsIndex]
additionalParamsString = paramstring[additionalParamsIndex:]
additionalParams = json.loads(additionalParamsString)
result['ydlOpts'] = additionalParams['ydlOpts']
return result
def guess_manifest_type(f, url):
protocol = f.get('protocol', "")
if protocol.startswith("m3u"):
return "hls"
elif protocol.startswith("rtmp") or protocol == "rtsp":
return "rtmp"
elif protocol == "ism":
return "ism"
for s in [".m3u", ".m3u8", ".hls", ".mpd", ".rtmp", ".ism"]:
offset = url.find(s, 0)
while offset != -1:
if offset == len(url) - len(s) or not url[offset + len(s)].isalnum():
if s.startswith(".m3u"):
s = ".hls"
return s[1:]
offset = url.find(s, offset + 1)
return None
try:
import inputstreamhelper
def isa_supports(stream):
if stream is None or len(stream) < 1:
return False
return inputstreamhelper.Helper(stream).check_inputstream()
except ImportError:
def isa_supports(stream):
return False
def createListItemFromVideo(result):
debug(result)
url = None
isa = None
headers = None
# first try existing manifest
manifest_url = result.get('manifest_url') if usemanifest else None
if manifest_url is not None and isa_supports(guess_manifest_type(result, manifest_url)):
isa = True
url = manifest_url
headers = result.get('http_headers')
log("Picked original manifest")
# then move on to heuristic format selection
if url is None:
have_video = False
have_audio = False
dash_video = []
dash_audio = []
filtered_format = None
all_formats = result.get('formats', [])
for f in all_formats:
vcodec = f.get('vcodec', "none")
acodec = f.get('acodec', "none")
if vcodec != "none":
have_video = True
if acodec != "none":
have_audio = True
container = f.get('container', "")
if vcodec != "none" and acodec == "none" and container in ["mp4_dash", "webm_dash"]:
dash_video.append(f)
if vcodec == "none" and acodec != "none" and container in ["m4a_dash", "webm_dash"]:
dash_audio.append(f)
# workaround for unknown ISA bug that causes audio to fail when
# multiple streams are available, though seemingly only when they have
# different sample rates.
if len(dash_audio) > 1:
dash_audio = [dash_audio[-1]]
# ytdl returns formats from worst to best
for f in reversed(all_formats):
# assume that manifests are either video+audio regardless of acodec, or audio only
vcodec = f.get('vcodec')
acodec = f.get('acodec')
if (have_video and vcodec == "none") or (not have_video and acodec == "none"):
continue
# Streams with adaptive manifests:
# ytdl will sometimes return a manifest_url in individual formats
# but not a global one. When this happens it (always?) means that
# it's functionally a global manifest.
manifest_url = f.get('manifest_url') if usemanifest else None
if manifest_url is not None and isa_supports(guess_manifest_type(f, manifest_url)):
url = manifest_url
isa = True
headers = f.get('http_headers')
log("Picked format " + f.get('format', "") + " manifest")
break
# MPEG-DASH streams without adaptive manifest:
if usedashbuilder and (not have_video or len(dash_video) > 0) and (not have_audio or len(dash_audio) > 0) and ((have_video and f == dash_video[-1]) or (not have_video and have_audio and f == dash_audio[-1])) and isa_supports("mpd"):
import dash_builder
builder = dash_builder.Manifest(result.get('duration', "0"))
video_success = not have_video
audio_success = not have_audio
for fvideo in dash_video:
fid = fvideo.get('format', "")
try:
builder.add_video_format(fvideo)
video_success = True
log("Added video stream {} to DASH manifest".format(fid))
except Exception as e:
log("Failed to add DASH video stream {}: {}".format(fid, e))
for faudio in dash_audio:
fid = faudio.get('format', "")
try:
builder.add_audio_format(faudio)
audio_success = True
log("Added audio stream {} to DASH manifest".format(fid))
except Exception as e:
log("Failed to add DASH audio stream {}: {}".format(fid, e))
if video_success and audio_success:
url = dash_builder.start_httpd(builder.emit())
isa = True
headers = f.get('http_headers')
log("Picked DASH with custom manifest")
break
# Non-adaptive manifests or files on servers:
if not 'url' in f:
continue
# TODO: implement support for making/serving global HLS manifests for m3u8 and mp4 urls
if (have_video and vcodec == "none") or (have_audio and acodec == "none"):
continue
manifest_type = guess_manifest_type(f, f['url'])
if manifest_type is not None and not isa_supports(manifest_type):
continue
width = f.get('width', 0)
if width is not None and width > maxwidth:
if filtered_format is None:
filtered_format = f
continue
url = f['url']
isa = isa_supports(manifest_type)
headers = f.get('http_headers')
log("Picked raw format " + f.get('format', ""))
break
# if nothing could be selected, try playing anything we can
if url is None and filtered_format is not None:
url = filtered_format['url']
isa = isa_supports(guess_manifest_type(filtered_format, url))
headers = f.get('http_headers')
if url is None:
# yeah we're definitely cooked
url = result.get('url')
if url is not None:
isa = isa_supports(guess_manifest_type(result, url))
headers = result.get('http_headers')
if url is None:
msg = "No supported streams found"
showErrorNotification(msg)
raise Exception("Error: " + msg)
log("creating list item for url {}".format(url))
list_item = xbmcgui.ListItem(result['title'], path=url)
video_info = list_item.getVideoInfoTag()
video_info.setTitle(result['title'])
video_info.setPlot(result.get('description', None))
if result.get('thumbnail', None) is not None:
list_item.setArt({'thumb': result['thumbnail']})
subtitles = result.get('subtitles', {})
if subtitles:
list_item.setSubtitles([
subtitleListEntry['url']
for lang in subtitles
for subtitleListEntry in subtitles[lang]
])
if isa:
list_item.setProperty('inputstream', 'inputstream.adaptive')
# Many sites will throw a 403 unless the http headers (e.g. user agent and referer)
# sent when downloading a manifest and streaming match those originally sent by yt-dlp.
if headers is None:
headers = result.get('http_headers')
if headers is not None:
headers = urlencode(headers)
list_item.setProperty('inputstream.adaptive.manifest_headers', headers)
list_item.setProperty('inputstream.adaptive.stream_headers', headers)
return list_item
def createListItemFromFlatPlaylistItem(video):
listItemUrl = __url__ + "?" + video['url']
title = video['title'] if 'title' in video else video['url']
# add the extra parameters to every playlist item
paramstring = sys.argv[2]
additionalParamsIndex = paramstring.find(' ')
if additionalParamsIndex != -1:
additionalParamsString = paramstring[additionalParamsIndex:]
listItemUrl = listItemUrl + " " + additionalParamsString
listItem = xbmcgui.ListItem(
path = listItemUrl,
label = title
)
video_info = listItem.getVideoInfoTag()
video_info.setTitle(title)
# both `true` and `false` are recommended here...
listItem.setProperty("IsPlayable","true")
return listItem
# get the index of the first video to be played in the submitted playlist url
def playlistIndex(url, playlist):
query = urlparse(url).query
queryParams = parse_qs(query)
if 'v' not in queryParams:
return None
v = queryParams['v'][0]
try:
# youtube playlist indices start at 1
index = int(queryParams.get('index')[0]) - 1
if playlist['entries'][index]['id'] == v:
return index
except:
pass
for i, entry in enumerate(playlist['entries']):
if entry['id'] == v:
return i
return None
# Open the settings if no parameters have been passed. Prevents crash.
# This happens when the addon is launched from within the Kodi OSD.
if not sys.argv[2]:
xbmcaddon.Addon().openSettings()
exit()
# Use the chosen resolver while forcing to use youtube_dl on legacy python 2 systems (dlp is python 3.6+)
if xbmcplugin.getSetting(int(sys.argv[1]),"resolver") == "0" or sys.version_info[0] == 2:
from youtube_dl import YoutubeDL
using_yt_dlp = False
else:
# import lib.yt_dlp as yt_dlp
from yt_dlp import YoutubeDL
using_yt_dlp = True
# patch broken strptime (see above)
patch_strptime()
# extract_flat: Do not resolve URLs, return the immediate result.
# Pass in 'in_playlist' to only show this behavior for
# playlist items.
ydl_opts = {'extract_flat': 'in_playlist'}
params = getParams()
url = str(params['url'])
ydl_opts.update(params['ydlOpts'])
if using_yt_dlp:
try:
deno_enabled = xbmcplugin.getSetting(int(sys.argv[1]), "deno_enabled") == 'true'
if deno_enabled:
from deno_manager import get_ydl_opts
auto_download = xbmcplugin.getSetting(int(sys.argv[1]), "deno_autodownload") == 'true'
ydl_opts.update(get_ydl_opts(auto_download=auto_download))
except Exception as e:
log("Failed to configure Deno: {}".format(str(e)), xbmc.LOGWARNING)
usemanifest = xbmcplugin.getSetting(int(sys.argv[1]),"usemanifest") == 'true'
usedashbuilder = xbmcplugin.getSetting(int(sys.argv[1]),"usedashbuilder") == 'true'
maxwidth = int(xbmcplugin.getSetting(int(sys.argv[1]), "maxresolution"))
ydl = YoutubeDL(ydl_opts)
ydl.add_default_info_extractors()
with ydl:
progress = xbmcgui.DialogProgressBG()
progress.create("Resolving " + url)
try:
result = ydl.extract_info(url, download=False)
except:
progress.close()
showErrorNotification("Could not resolve the url, check the log for more info")
import traceback
log(msg=traceback.format_exc(), level=xbmc.LOGERROR)
exit()
progress.close()
if 'entries' in result:
# more than one video
pl = xbmc.PlayList(1)
pl.clear()
# determine which index in the queue to start playing from
indexToStartAt = playlistIndex(url, result)
if indexToStartAt == None:
indexToStartAt = 0
unresolvedEntries = list(result['entries'])
startingEntry = unresolvedEntries.pop(indexToStartAt)
# populate the queue with unresolved entries so that the starting entry can be inserted
for video in unresolvedEntries:
if 'url' in video:
list_item = createListItemFromFlatPlaylistItem(video)
pl.add(list_item.getPath(), list_item)
# make sure the starting ListItem has a resolved url, to avoid recursion and crashes
try:
if 'url' in startingEntry:
startingItem = createListItemFromVideo(ydl.extract_info(startingEntry['url'], download=False))
else:
startingItem = createListItemFromVideo(startingEntry)
except Exception:
showErrorNotification("Could not resolve the url, check the log for more info")
import traceback
log(msg=traceback.format_exc(), level=xbmc.LOGERROR)
exit()
pl.add(startingItem.getPath(), startingItem, indexToStartAt)
#xbmc.Player().play(pl) # this probably works again
# ...but start playback the same way the Youtube plugin does it:
xbmc.executebuiltin('Playlist.PlayOffset(%s,%d)' % ('video', indexToStartAt))
else:
# Just a video, pass the item to the Kodi player.
try:
list_item = createListItemFromVideo(result)
except Exception:
showErrorNotification("Could not resolve the url, check the log for more info")
import traceback
log(msg=traceback.format_exc(), level=xbmc.LOGERROR)
xbmcplugin.setResolvedUrl(__handle__, False, listitem=xbmcgui.ListItem())
exit()
xbmcplugin.setResolvedUrl(__handle__, True, listitem=list_item)