forked from khoda/xbmc-oppetarkiv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault.py
executable file
·491 lines (384 loc) · 11.9 KB
/
default.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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# -*- coding: utf-8 -*-
import re
import json
import time
import urllib
import xbmc
import xbmcgui
import xbmcaddon
import xbmcplugin
import CommonFunctions
import os
import resources.lib.helper as helper
import resources.lib.svt as svt
MODE_CHANNELS = "kanaler"
MODE_A_TO_O = "a-o"
MODE_PROGRAM = "pr"
MODE_LIVE = "live"
MODE_LATEST = "ep"
MODE_LATEST_NEWS = "en"
MODE_VIDEO = "video"
MODE_CATEGORIES = "categories"
MODE_CATEGORY = "ti"
MODE_LETTER = "letter"
MODE_RECOMMENDED = "rp"
MODE_SEARCH = "search"
MODE_BESTOF_CATEGORIES = "bestofcategories"
MODE_BESTOF_CATEGORY = "bestofcategory"
MODE_VIEW_TITLES = "view_titles"
MODE_VIEW_EPISODES = "view_episodes"
MODE_VIEW_CLIPS = "view_clips"
CURR_DIR_ITEMS = 0
pluginHandle = int(sys.argv[1])
settings = xbmcaddon.Addon()
localize = settings.getLocalizedString
common = CommonFunctions
common.plugin = "SVTOA Play 3"
# Get and set settings
common.dbg = False
if settings.getSetting('debug') == "true":
common.dbg = True
HLS_STRIP = False
if settings.getSetting("hlsstrip") == "true":
HLS_STRIP = True
FULL_PROGRAM_PARSE = False
if settings.getSetting("fullparse") == "true":
FULL_PROGRAM_PARSE = True
HIDE_SIGN_LANGUAGE = False
if settings.getSetting("hidesignlanguage") == "true":
HIDE_SIGN_LANGUAGE = True
SHOW_SUBTITLES = False
if settings.getSetting("showsubtitles") == "true":
SHOW_SUBTITLES = True
USE_ALPHA_CATEGORIES = False
if settings.getSetting("alpha") == "true":
USE_ALPHA_CATEGORIES = True
MAX_DIR_ITEMS = int(float(settings.getSetting("diritems")))
BW_SELECT = False
if settings.getSetting("bwselect") == "true":
BW_SELECT = True
LOW_BANDWIDTH = int(float(settings.getSetting("bandwidth")))
HIGH_BANDWIDTH = svt.getHighBw(LOW_BANDWIDTH)
LOW_BANDWIDH = LOW_BANDWIDTH
def viewStart():
addDirectoryItem(localize(30000), { "mode": MODE_A_TO_O })
def viewAtoO():
programs = svt.getAtoO()
for program in programs:
addDirectoryItem(program["title"], { "mode": MODE_PROGRAM, "url": program["url"], "page": 1 })
def viewProgram(url,page,index):
if FULL_PROGRAM_PARSE:
createTabIndex(url)
else:
createDirectory(url,page,index,MODE_PROGRAM,MODE_VIDEO)
def viewSearch():
keyword = common.getUserInput(localize(30102))
if keyword == "" or not keyword:
viewStart()
return
keyword = urllib.quote(keyword)
common.log("Search string: " + keyword)
keyword = re.sub(r" ","+",keyword)
url = svt.URL_TO_SEARCH + keyword
createTabIndex(url)
def viewPageResults(url,mode,page,index):
"""
Creates a directory for the search results from
the tab specified by the mode parameter.
"""
common.log("url: " + url + " mode: " + mode)
dirtype = None
if MODE_VIEW_TITLES == mode:
dirtype = MODE_PROGRAM
elif MODE_VIEW_EPISODES == mode:
dirtype = MODE_VIDEO
elif MODE_VIEW_CLIPS == mode:
dirtype = MODE_VIDEO
else:
common.log("Undefined mode")
viewStart()
return
createDirectory(url,page,index,mode,dirtype)
def createDirectory(url,page,index,callertype,dirtype):
"""
Creates a directory with list items from the supplied program
page (url).
"""
#if not url.startswith("/"):
# url = "/" + url
tabname = svt.TAB_EPISODES
if MODE_RECOMMENDED == callertype:
tabname = svt.TAB_RECOMMENDED
elif MODE_LATEST_NEWS == callertype:
tabname = svt.TAB_NEWS
elif MODE_VIEW_CLIPS == callertype:
tabname = svt.TAB_CLIPS
elif MODE_CATEGORY == callertype or MODE_VIEW_TITLES == callertype:
tabname = svt.TAB_TITLES
html = svt.getPage(url)
visafler = common.parseDOM(html, "a", attrs = { "class": "[^\"']*svtCenterUnknown[^\"']*" }, ret = "href")
if len(visafler) == 1:
kolla = True
populateDirNoPaging(url,dirtype,tabname)
url = visafler[0]
while (kolla):
html = svt.getPage(url)
visafler = common.parseDOM(html, "a", attrs = { "class": "[^\"']*svtCenterUnknown[^\"']*" }, ret = "href")
if len(visafler) == 2:
populateDirNoPaging(url,dirtype,tabname)
url = visafler[1]
else:
kolla = False
populateDirNoPaging(url,dirtype,tabname)
return
else:
populateDirNoPaging(url,dirtype,tabname)
return
def populateDirNoPaging(url,mode,tabname):
"""
Program pages that have less than 8 videos
does not have a way to fetch the Ajax URL.
Use the normal page URL to populate the
directory.
"""
common.log("url: " + url + ", mode: " + mode + ", tabname: " + tabname)
articles = svt.getArticles(url,None,tabname)
for article in articles:
createDirItem(article,mode)
def createDirItem(article,mode):
"""
Given an article and a mode; create directory item
for the article.
"""
global CURR_DIR_ITEMS
if (not HIDE_SIGN_LANGUAGE) or (article["title"].lower().endswith("teckentolkad") == False and article["title"].lower().find("teckenspråk".decode("utf-8")) == -1):
params = {}
params["mode"] = mode
params["url"] = article["url"]
folder = False
if mode == MODE_PROGRAM:
folder = True
params["page"] = 1
addDirectoryItem(article["title"], params, article["thumbnail"], folder, False, article["info"])
CURR_DIR_ITEMS += 1
def startVideo(url):
"""
Starts the XBMC player if a valid video url is
found for the given page url.
"""
#if not url.startswith("/"):
# url = "/" + url
url = url + svt.JSON_SUFFIX
common.log("url: " + url)
html = svt.getPage(url)
jsonString = common.replaceHTMLCodes(html)
jsonObj = json.loads(jsonString)
common.log(jsonString)
subtitle = None
player = xbmc.Player()
startTime = time.time()
videoUrl = None
extension = "None"
args = ""
for video in jsonObj["video"]["videoReferences"]:
"""
Determine which file extension that will be used
m3u8 is preferred, hence the break.
Order: m3u8, f4m, mp4, flv
"""
tmpurl = video["url"]
argpos = tmpurl.rfind("?")
if argpos > 0:
args = tmpurl[argpos:]
tmpurl = tmpurl[:argpos]
if tmpurl.endswith(".m3u8"):
extension = "HLS"
videoUrl = tmpurl
break
if tmpurl.endswith(".f4m"):
extension = "F4M"
videoUrl = tmpurl
continue
if tmpurl.endswith(".mp4"):
extension = "MP4"
videoUrl = tmpurl
continue
if tmpurl.endswith(".flv"):
extension = "FLV"
videoUrl = tmpurl
continue
videoUrl = tmpurl
for sub in jsonObj["video"]["subtitleReferences"]:
if sub["url"].endswith(".wsrt"):
subtitle = sub["url"]
else:
if len(sub["url"]) > 0:
common.log("Skipping unknown subtitle: " + sub["url"])
if extension == "HLS" and HLS_STRIP:
videoUrl = hlsStrip(videoUrl)
elif extension == "HLS" and BW_SELECT:
videoUrl = getStream(videoUrl)
if extension == "F4M":
videoUrl = videoUrl.replace("/z/", "/i/").replace("manifest.f4m","master.m3u8")
if extension == "MP4":
videoUrl = mp4Handler(jsonObj)
if extension == "None" and videoUrl:
# No supported video was found
common.log("No supported video extension found for URL: " + videoUrl)
videoUrl = None
if videoUrl:
if args and not (HLS_STRIP or BW_SELECT):
common.log("Appending arguments: "+args)
videoUrl = videoUrl + args
if extension == "MP4" and videoUrl.startswith("rtmp://"):
videoUrl = videoUrl + " swfUrl="+svt.SWF_URL+" swfVfy=1"
xbmcplugin.setResolvedUrl(pluginHandle, True, xbmcgui.ListItem(path=videoUrl))
if subtitle:
while not player.isPlaying() and time.time() - startTime < 10:
time.sleep(1.)
player.setSubtitles(subtitle)
if not SHOW_SUBTITLES:
player.showSubtitles(False)
else:
# No video URL was found
dialog = xbmcgui.Dialog()
dialog.ok("SVT PLAY", localize(30100))
def mp4Handler(jsonObj):
"""
If there are several mp4 streams in the JSON object:
pick the one with the highest bandwidth.
Some programs are available with multiple mp4 streams
for different bitrates. This function ensures that the one
with the highest bitrate is chosen.
Can possibly be extended to support some kind of quality
setting in the plugin.
"""
videos = []
# Find all mp4 videos
for video in jsonObj["video"]["videoReferences"]:
if video["url"].endswith(".mp4"):
videos.append(video)
if len(videos) == 1:
return videos[0]["url"]
bitrate = 0
url = ""
# Find the video with the highest bitrate
for video in videos:
if video["bitrate"] > bitrate:
bitrate = video["bitrate"]
url = video["url"]
common.log("mp4 handler info: bitrate="+str(bitrate)+" url="+url)
return url
def hlsStrip(videoUrl):
"""
Extracts the stream that supports the
highest bandwidth and is not using the avc1.77.30 codec.
Returns the path to a m3u8 file on the local disk with a
reference to the extracted stream.
"""
common.log("Stripping file: " + videoUrl)
ufile = urllib.urlopen(videoUrl)
lines = ufile.readlines()
newplaylist = "#EXTM3U\n"
hlsurl = ""
bandwidth = 0
foundhigherquality = False
for line in lines:
if foundhigherquality:
# The stream url is on the line proceeding the header
foundhigherquality = False
hlsurl = line
if "EXT-X-STREAM-INF" in line: # The header
if not "avc1.77.30" in line:
match = re.match(r'.*BANDWIDTH=(\d+).+',line)
if match:
if bandwidth < int(match.group(1)):
foundhigherquality = True
bandwidth = int(match.group(1))
continue
if bandwidth == 0:
return None
ufile.close()
hlsurl = hlsurl.rstrip()
common.log("Returned stream url : " + hlsurl)
return hlsurl
def getStream(url):
"""
Returns a stream matching the set bandwidth
"""
f = urllib.urlopen(url)
lines = f.readlines()
marker = "#EXT-X-STREAM-INF"
found = False
for line in lines:
if found:
# The stream url is on the line proceeding the header
hlsurl = line
break
if marker in line: # The header
match = re.match(r'.*BANDWIDTH=(\d+)000.+',line)
if match:
if LOW_BANDWIDTH < int(match.group(1)) < HIGH_BANDWIDTH:
common.log("Found stream with bandwidth " + match.group(1) + " for selected bandwidth " + str(LOW_BANDWIDTH))
found = True
f.close()
hlsurl = hlsurl.rstrip()
common.log("Returned stream url: " + hlsurl)
return hlsurl
def addDirectoryItem(title, params, thumbnail = None, folder = True, live = False, info = None):
li = xbmcgui.ListItem(title)
if thumbnail:
li.setThumbnailImage(thumbnail)
if live:
li.setProperty("IsLive", "true")
if not folder:
li.setProperty("IsPlayable", "true")
if info:
li.setInfo("Video", info)
xbmcplugin.addDirectoryItem(pluginHandle, sys.argv[0] + '?' + urllib.urlencode(params), li, folder)
params = helper.getUrlParameters(sys.argv[2])
mode = params.get("mode")
url = urllib.unquote_plus(params.get("url", ""))
page = params.get("page")
letter = params.get("letter")
index = params.get("index")
if not index:
index = "0"
if not mode:
viewStart()
elif mode == MODE_A_TO_O:
if USE_ALPHA_CATEGORIES:
viewAlphaDirectories()
else:
viewAtoO()
elif mode == MODE_LIVE:
viewLive()
elif mode == MODE_CATEGORIES:
viewCategories()
elif mode == MODE_CATEGORY:
viewCategory(url,page,index)
elif mode == MODE_PROGRAM:
viewProgram(url,page,index)
elif mode == MODE_VIDEO:
startVideo(url)
elif mode == MODE_LATEST:
viewLatest(mode,page,index)
elif mode == MODE_LATEST_NEWS:
viewLatest(mode,page,index)
elif mode == MODE_LETTER:
viewProgramsByLetter(letter)
elif mode == MODE_RECOMMENDED:
viewLatest(mode,page,index)
elif mode == MODE_SEARCH:
viewSearch()
elif mode == MODE_VIEW_TITLES or \
mode == MODE_VIEW_EPISODES or \
mode == MODE_VIEW_CLIPS:
viewPageResults(url,mode,page,index)
elif mode == MODE_BESTOF_CATEGORIES:
viewBestOfCategories()
elif mode == MODE_BESTOF_CATEGORY:
viewBestOfCategory(url)
elif mode == MODE_CHANNELS:
viewChannels()
xbmcplugin.endOfDirectory(pluginHandle)