forked from spaam/svtplay-dl
/
svtplay_dl.py
executable file
·1214 lines (1052 loc) · 38.8 KB
/
svtplay_dl.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
import sys
if sys.version_info > (3, 0):
from urllib.request import Request, urlopen
from urllib.error import HTTPError, URLError
from urllib.parse import urlparse, parse_qs, unquote_plus, quote_plus
from io import StringIO
else:
from urllib2 import Request, urlopen, HTTPError, URLError
from urlparse import urlparse, parse_qs
from urllib import unquote_plus, quote_plus
import StringIO
import re
import os
import subprocess
from optparse import OptionParser
import xml.etree.ElementTree as ET
import shlex
import json
import time
import logging
import base64
import struct
import binascii
import hashlib
__version__ = "0.8.2012.12.23"
class Options:
"""
Options used when invoking the script from another Python script.
Simple container class used when calling get_media() from another Python
script. The variables corresponds to the command line parameters parsed
in main() when the script is called directly.
When called from a script there are a few more things to consider:
* Logging is done to 'log'. main() calls setup_log() which sets the
logging to either stdout or stderr depending on the silent level.
A user calling get_media() directly can either also use setup_log()
or configure the log manually.
* Progress information is printed to 'progress_stream' which defaults to
sys.stderr but can be changed to any stream.
* Many errors results in calls to system.exit() so catch 'SystemExit'-
Exceptions to prevent the entire application from exiting if that happens.
"""
def __init__(self):
self.output = None
self.resume = False
self.live = False
self.silent = False
self.quality = None
self.hls = False
log = logging.getLogger('svtplay_dl')
progress_stream = sys.stderr
def readbyte(data, pos):
return struct.unpack("B", data[pos])[0]
def read16(data, pos):
endpos = pos + 2
return struct.unpack(">H", data[pos:endpos])[0]
def read24(data, pos):
end = pos + 3
return struct.unpack(">L", "\x00" + data[pos:end])[0]
def read32(data, pos):
end = pos + 4
return struct.unpack(">i", data[pos:end])[0]
def read64(data, pos):
end = pos + 8
return struct.unpack(">Q", data[pos:end])[0]
def readstring(data, pos):
length = 0
while (data[pos + length] != "\x00"):
length += 1
endpos = pos + length
string = data[pos:endpos]
pos += length + 1
return pos, string
def readboxtype(data, pos):
boxsize = read32(data, pos)
tpos = pos + 4
endpos = tpos + 4
boxtype = data[tpos:endpos]
if boxsize > 1:
boxsize -= 8
pos += 8
return pos, boxsize, boxtype
def readbox(data, pos):
version = readbyte(data, pos)
pos += 1
flags = read24(data, pos)
pos += 3
bootstrapversion = read32(data, pos)
pos += 4
byte = readbyte(data, pos)
pos += 1
profile = (byte & 0xC0) >> 6
live = (byte & 0x20) >> 5
update = (byte & 0x10) >> 4
timescale = read32(data, pos)
pos += 4
currentmediatime = read64(data, pos)
pos += 8
smptetimecodeoffset = read64(data, pos)
pos += 8
temp = readstring(data, pos)
movieidentifier = temp[1]
pos = temp[0]
serverentrycount = readbyte(data, pos)
pos += 1
serverentrytable = []
i = 0
while i < serverentrycount:
temp = readstring(data, pos)
serverentrytable.append(temp[1])
pos = temp[0]
i += 1
qualityentrycount = readbyte(data, pos)
pos += 1
qualityentrytable = []
i = 0
while i < qualityentrycount:
temp = readstring(data, pos)
qualityentrytable.append(temp[1])
pos = temp[0]
i += 1
tmp = readstring(data, pos)
drm = tmp[1]
pos = tmp[0]
tmp = readstring(data, pos)
metadata = tmp[1]
pos = tmp[0]
segmentruntable = readbyte(data, pos)
pos += 1
if segmentruntable > 0:
tmp = readboxtype(data, pos)
boxtype = tmp[2]
boxsize = tmp[1]
pos = tmp[0]
if boxtype == "asrt":
antal = readasrtbox(data, pos)
pos += boxsize
fragRunTableCount = readbyte(data, pos)
pos += 1
i = 0
while i < fragRunTableCount:
tmp = readboxtype(data, pos)
boxtype = tmp[2]
boxsize = tmp[1]
pos = tmp[0]
if boxtype == "afrt":
readafrtbox(data, pos)
pos += boxsize
i += 1
return antal
def readafrtbox(data, pos):
version = readbyte(data, pos)
pos += 1
flags = read24(data, pos)
pos += 3
timescale = read32(data, pos)
pos += 4
qualityentry = readbyte(data, pos)
pos += 1
i = 0
while i < qualityentry:
temp = readstring(data, pos)
qualitysegmulti = temp[1]
pos = temp[0]
i += 1
fragrunentrycount = read32(data, pos)
pos += 4
i = 0
while i < fragrunentrycount:
firstfragment = read32(data, pos)
pos += 4
timestamp = read64(data, pos)
pos += 8
duration = read32(data, pos)
pos += 4
i += 1
def readasrtbox(data, pos):
version = readbyte(data, pos)
pos += 1
flags = read24(data, pos)
pos += 3
qualityentrycount = readbyte(data, pos)
pos += 1
qualitysegmentmodifers = []
i = 0
while i < qualityentrycount:
temp = readstring(data, pos)
qualitysegmentmodifers.append(temp[1])
pos = temp[0]
i += 1
seqCount = read32(data, pos)
pos += 4
ret = {}
i = 0
while i < seqCount:
firstseg = read32(data, pos)
pos += 4
fragPerSeg = read32(data, pos)
pos += 4
tmp = i + 1
ret[tmp] = {"first": firstseg, "total": fragPerSeg}
i += 1
return ret
def parsem3u(data):
if not data.startswith("#EXTM3U"):
raise ValueError("Does not apprear to be a ext m3u file")
files=[]
streaminfo={}
globdata={}
data=data.replace("\r","\n")
for l in data.split("\n")[1:]:
if not l:
continue
if l.startswith("#EXT-X-STREAM-INF:"):
#not a proper parser
info = [x.strip().split("=",1) for x in l[18:].split(",")]
streaminfo.update({info[1][0]: info[1][1]})
elif l.startswith("#EXT-X-ENDLIST"):
break
elif l.startswith("#EXT-X-"):
globdata.update(dict([l[7:].strip().split(":",1)]))
elif l.startswith("#EXTINF:"):
dur, title=l[8:].strip().split(",",1)
streaminfo['duration'] = dur
streaminfo['title'] = title
elif l[0]=='#':
pass
else:
files.append((l, streaminfo))
streaminfo={}
return globdata,files
def decode_f4f(fragID, fragData ):
start = fragData.find( "mdat" ) + 4
if (fragID > 1):
for dummy in range( 2 ):
tagLen, = struct.unpack_from( ">L", fragData, start )
tagLen &= 0x00ffffff
start += tagLen + 11 + 4
return start
def get_http_data(url, method="GET", header="", data=""):
""" Get the page to parse it for streams """
request = Request(url)
request.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
if len(header) > 0:
request.add_header('Content-Type', header)
if len(data) > 0:
request.add_data(data)
try:
response = urlopen(request)
except HTTPError as e:
log.error("Something wrong with that url")
log.error("Error code: %s" % e.code)
sys.exit(5)
except URLError as e:
log.error("Something wrong with that url")
log.error("Error code: %s" % e.reason)
sys.exit(5)
except ValueError as e:
log.error("Try adding http:// before the url")
sys.exit(5)
if sys.version_info > (3, 0):
data = response.read().decode('utf-8')
else:
try:
data = response.read()
except socket.error as e:
log.error("Lost the connection to the server")
sys.exit(5)
response.close()
return data
def progress(byte, total):
""" Print some info about how much we have downloaded """
ratio = float(byte) / total
percent = round(ratio*100, 2)
tlen = str(len(str(total)))
fmt = "Downloaded %"+tlen+"dkB of %dkB bytes (% 3.2f%%)"
progresstr = fmt % (byte >> 10, total >> 10, percent)
columns = int(os.getenv("COLUMNS", "80"))
if len(progresstr) < columns - 13:
p = int((columns - len(progresstr) - 3) * ratio)
q = int((columns - len(progresstr) - 3) * (1 - ratio))
progresstr = "[" + ("#" * p) + (" " * q) + "] " + progresstr
progress_stream.write(progresstr + '\r')
if byte >= total:
progress_stream.write('\n')
progress_stream.flush()
def download_hds(options, url, output, swf):
data = get_http_data(url)
streams = {}
bootstrap = {}
xml = ET.XML(data)
prefix = xml.find("{http://ns.adobe.com/f4m/1.0}id").text
if sys.version_info < (2, 7):
bootstrapIter = xml.getiterator("{http://ns.adobe.com/f4m/1.0}bootstrapInfo")
mediaIter = xml.getiterator("{http://ns.adobe.com/f4m/1.0}media")
else:
bootstrapIter = xml.iter("{http://ns.adobe.com/f4m/1.0}bootstrapInfo")
mediaIter = xml.iter("{http://ns.adobe.com/f4m/1.0}media")
for i in bootstrapIter:
bootstrap[i.attrib["id"]] = i.text
for i in mediaIter:
streams[int(i.attrib["bitrate"])] = {"url": i.attrib["url"], "bootstrapInfoId": i.attrib["bootstrapInfoId"], "metadata": i.find("{http://ns.adobe.com/f4m/1.0}metadata").text}
test = select_quality(options, streams)
bootstrap = base64.b64decode(bootstrap[test["bootstrapInfoId"]])
box = readboxtype(bootstrap, 0)
if box[2] == "abst":
antal = readbox(bootstrap, box[0])
baseurl = url[0:url.rfind("/")]
i = 1
if output != "-":
extension = re.search("(\.[a-z0-9]+)$", output)
if not extension:
output = output + ".flv"
log.info("Outfile: %s", output)
file_d = open(output, "wb")
else:
file_d = sys.stdout
file_d.write(binascii.a2b_hex( "464c56010500000009000000001200010c00000000000000" ) )
file_d.write(base64.b64decode(test["metadata"]))
file_d.write(binascii.a2b_hex("00000000"))
while i <= antal[1]["total"]:
url = "%s/%sSeg1-Frag%s" % (baseurl, test["url"], i)
if output != "-":
progressbar(antal[1]["total"], i)
data = get_http_data(url)
number = decode_f4f(i, data)
file_d.write(data[number:])
i += 1
if output != "-":
file_d.close()
progress_stream.write('\n')
def download_hls(options, url, output, live, other):
data = get_http_data(url)
globaldata, files = parsem3u(data)
streams ={}
for i in files:
streams[int(i[1]["BANDWIDTH"])] = i[0]
test = select_quality(options, streams)
m3u8 = get_http_data(test)
globaldata, files = parsem3u(m3u8)
encrypted = False
key = None
try:
keydata = globaldata["KEY"]
encrypted = True
match = re.search("URI=\"(http://.*)\"", keydata)
key = get_http_data(match.group(1))
rand = os.urandom(16)
except:
pass
try:
from Crypto.Cipher import AES
decryptor = AES.new(key, AES.MODE_CBC, rand)
except ImportError:
log.error("You need to install pycrypto to download encrypted HLS streams")
sys.exit(2)
n=1
if output != "-":
extension = re.search("(\.[a-z0-9]+)$", output)
if not extension:
output = output + ".ts"
log.info("Outfile: %s", output)
file_d = open(output, "wb")
else:
file_d = sys.stdout
for i in files:
if output != "-":
progressbar(len(files), n)
data = get_http_data(i[0])
if encrypted:
lots = StringIO.StringIO(data)
plain = ""
crypt = lots.read(1024)
decrypted = decryptor.decrypt(crypt)
while decrypted:
plain += decrypted
crypt = lots.read(1024)
decrypted = decryptor.decrypt(crypt)
data = plain
file_d.write(data)
n += 1
if output != "-":
file_d.close()
progress_stream.write('\n')
def download_http(url, output):
""" Get the stream from HTTP """
response = urlopen(url)
total_size = response.info()['Content-Length']
total_size = int(total_size)
bytes_so_far = 0
if output != "-":
extension = re.search("(\.[a-z0-9]+)$", url)
if extension:
output = output + extension.group(1)
log.info("Outfile: %s", output)
file_d = open(output,"wb")
else:
file_d = sys.stdout
lastprogress = 0
while 1:
chunk = response.read(8192)
bytes_so_far += len(chunk)
if not chunk:
break
file_d.write(chunk)
if output != "-":
now = time.time()
if lastprogress + 1 < now:
lastprogress = now
progress(bytes_so_far, total_size)
if output != "-":
file_d.close()
def download_rtmp(options, url, output, live, extra_args, resume):
""" Get the stream from RTMP """
args = []
if live:
args.append("-v")
if resume:
args.append("-e")
extension = re.search("(\.[a-z0-9]+)$", url)
if output != "-":
if not extension:
extension = re.search("-y (.+):[-_a-z0-9\/]", extra_args)
if not extension:
output = output + ".flv"
else:
output = output + "." + extension.group(1)
else:
output = output + extension.group(1)
log.info("Outfile: %s", output)
args += ["-o", output]
if options.silent or output == "-":
args.append("-q")
if extra_args:
args += shlex.split(extra_args)
command = ["rtmpdump", "-r", url] + args
try:
subprocess.call(command)
except OSError as e:
log.error("Could not execute rtmpdump: " + e.strerror)
def select_quality(options, streams):
sort = sorted(streams.keys(), key=int)
if options.quality:
quality = options.quality
else:
quality = sort.pop()
try:
selected = streams[int(quality)]
except (KeyError, ValueError):
log.error("Can't find that quality. (Try one of: %s)",
", ".join(map(str, sort)))
sys.exit(4)
return selected
class Justin():
def __init__(self, options, output, live, resume):
self.options = options
self.output = output
self.live = live
self.resume = resume
def get(self, url):
other = "-a ondemand"
data = get_http_data(url)
data = re.sub("<(\d+)", "<_\g<1>", data)
data = re.sub("</(\d+)", "</_\g<1>", data)
xml = ET.XML(data)
if sys.version_info < (2, 7):
sa = list(xml)
else:
sa = list(xml)
streams = {}
for i in sa:
if i.tag[1:][:-1] != "iv":
try:
stream = {}
stream["token"] = i.find("token").text
stream["url"] = i.find("connect").text + "/" + i.find("play").text
streams[int(i.find("video_height").text)] = stream
except AttributeError:
None
test = select_quality(self.options, streams)
other = "-j '%s' -W %s" % (test["token"], self.resume)
self.resume = False
download_rtmp(self.options, test["url"], self.output, self.live, other, self.resume)
class Justin2():
def __init__(self, options, output, live, resume):
self.options = options
self.output = output
self.live = live
self.resume = resume
def get(self, url):
other = "-a ondemand"
data = get_http_data(url)
xml = ET.XML(data)
url = xml.find("archive").find("video_file_url").text
download_http(url, self.output)
class Hbo():
def __init__(self, options, output, live, resume):
self.options = options
self.output = output
self.live = live
self.resume = resume
def get(self, url):
data = get_http_data(url)
xml = ET.XML(data)
videoid = xml.find("content")[1].find("videoId").text
url = "http://render.cdn.hbo.com/data/content/global/videos/data/%s.xml" % videoid
data = get_http_data(url)
xml = ET.XML(data)
ss = xml.find("videos")
if sys.version_info < (2, 7):
sa = list(ss.getiterator("size"))
else:
sa = list(ss.iter("size"))
streams = {}
for i in sa:
stream = {}
stream["path"] = i.find("tv14").find("path").text
streams[int(i.attrib["width"])] = stream
test = select_quality(self.options, streams)
download_rtmp(self.options, test["path"], self.output, self.live, "", self.resume)
class Sr():
def __init__(self, options, output, live, other, resume):
self.options = options
self.output = output
self.live = live
self.other = other
self.resume = resume
def get(self, url):
url = url + self.other
data = get_http_data(url)
xml = ET.XML(data)
url = xml.find("entry").find("ref").attrib["href"]
download_http(url, self.output)
class Urplay():
def __init__(self, options, output, live, resume):
self.options = options
self.output = output
self.live = live
self.resume = resume
def get(self, url):
other = "-a ondemand"
data = get_http_data(url)
match = re.search('file=(.*)\&plugins', data)
if match:
path = "mp" + match.group(1)[-1] + ":" + match.group(1)
other = "-a ondemand -y %s" % path
download_rtmp(self.options, "rtmp://streaming.ur.se/", self.output, self.live, other, self.resume)
class Qbrick():
def __init__(self, options, output, live, resume):
self.options = options
self.output = output
self.live = live
self.resume = resume
def get(self, url):
data = get_http_data(url)
xml = ET.XML(data)
server = xml.find("head").find("meta").attrib["base"]
streams = xml.find("body").find("switch")
if sys.version_info < (2, 7):
sa = list(streams.getiterator("video"))
else:
sa = list(streams.iter("video"))
streams = {}
for i in sa:
streams[int(i.attrib["system-bitrate"])] = i.attrib["src"]
path = select_quality(self.options, streams)
other = "-y %s" % path
download_rtmp(self.options, server, self.output, self.live, other, self.resume)
class Kanal5():
def __init__(self, options, output, live, resume):
self.options = options
self.output = output
self.live = live
self.resume = resume
def get(self, url):
data = json.loads(get_http_data(url))
self.live = data["isLive"]
steambaseurl = data["streamBaseUrl"]
streams = {}
for i in data["streams"]:
stream = {}
stream["source"] = i["source"]
streams[int(i["bitrate"])] = stream
test = select_quality(self.options, streams)
filename = test["source"]
match = re.search("^(.*):", filename)
self.output = "%s.%s" % (self.output, match.group(1))
other = "-W %s -y %s " % ("http://www.kanal5play.se/flash/StandardPlayer.swf", filename)
download_rtmp(self.options, steambaseurl, self.output, self.live, other, self.resume)
class Kanal9():
def __init__(self, options, output, live, other, resume):
self.options = options
self.output = output
self.live = live
self.other = other
self.resume = resume
def get(self, url):
try:
from pyamf import remoting
except ImportError:
log.error("You need to install pyamf to download content from kanal5 and kanal9")
log.error("In debian the package is called python-pyamf")
sys.exit(2)
player_id = 811317479001
publisher_id = 22710239001
const = "9f79dd85c3703b8674de883265d8c9e606360c2e"
env = remoting.Envelope(amfVersion=3)
env.bodies.append(("/1", remoting.Request(target="com.brightcove.player.runtime.PlayerMediaFacade.findMediaById", body=[const, player_id, self.other, publisher_id], envelope=env)))
env = str(remoting.encode(env).read())
url = "http://" + url + "/services/messagebroker/amf?playerKey=AQ~~,AAAABUmivxk~,SnCsFJuhbr0vfwrPJJSL03znlhz-e9bk"
header = "application/x-amf"
data = get_http_data(url, "POST", header, env)
streams = {}
for i in remoting.decode(data).bodies[0][1].body['renditions']:
stream = {}
stream["uri"] = i["defaultURL"]
streams[i["encodingRate"]] = stream
test = select_quality(self.options, streams)
filename = test["uri"]
match = re.search("(rtmp[e]{0,1}://.*)\&(.*)$", filename)
other = "-W %s -y %s " % ("http://admin.brightcove.com/viewer/us1.25.04.01.2011-05-24182704/connection/ExternalConnection_2.swf", match.group(2))
download_rtmp(self.options, match.group(1), self.output, self.live, other, self.resume)
class Expressen():
def __init__(self, options, output, live, resume):
self.options = options
self.output = output
self.live = live
self.resume = resume
def get(self, url):
other = ""
data = get_http_data(url)
xml = ET.XML(data)
ss = xml.find("vurls")
if sys.version_info < (2, 7):
sa = list(ss.getiterator("vurl"))
else:
sa = list(ss.iter("vurl"))
streams = {}
for i in sa:
streams[int(i.attrib["bitrate"])] = i.text
test = select_quality(self.options, streams)
filename = test
match = re.search("rtmp://([0-9a-z\.]+/[0-9]+/)(.*).flv", filename)
filename = "rtmp://%s" % match.group(1)
other = "-y %s" % match.group(2)
download_rtmp(self.options, filename, self.output, self.live, other, self.resume)
class Aftonbladet():
def __init__(self, options, output, live, resume):
self.options = options
self.output = output
self.live = live
self.resume = resume
def get(self, url, start):
data = get_http_data(url)
xml = ET.XML(data)
url = xml.find("articleElement").find("mediaElement").find("baseUrl").text
path = xml.find("articleElement").find("mediaElement").find("media").attrib["url"]
other = "-y %s" % path
if start > 0:
other = other + " -A %s" % str(start)
if url == None:
log.error("Can't find any video on that page")
sys.exit(3)
if url[0:4] == "rtmp":
download_rtmp(self.options, url, self.output, self.live, other, self.resume)
else:
filename = url + path
download_http(filename, self.output)
class Viaplay():
def __init__(self, options, output, live, resume):
self.options = options
self.output = output
self.live = live
self.resume = resume
def get(self, url):
other = ""
data = get_http_data(url)
xml = ET.XML(data)
filename = xml.find("Product").find("Videos").find("Video").find("Url").text
if filename[:4] == "http":
data = get_http_data(filename)
xml = ET.XML(data)
filename = xml.find("Url").text
other = "-W http://flvplayer.viastream.viasat.tv/play/swf/player110516.swf?rnd=1315434062"
download_rtmp(self.options, filename, self.output, self.live, other, self.resume)
class Tv4play():
def __init__(self, options, output, live, resume):
self.options = options
self.output = output
self.live = live
self.resume = resume
def get(self, url):
data = get_http_data(url)
xml = ET.XML(data)
ss = xml.find("items")
if sys.version_info < (2, 7):
sa = list(ss.getiterator("item"))
else:
sa = list(ss.iter("item"))
if xml.find("live").text:
self.live = True
streams = {}
sa.pop(len(sa)-1)
for i in sa:
stream = {}
stream["uri"] = i.find("base").text
stream["path"] = i.find("url").text
streams[int(i.find("bitrate").text)] = stream
if len(streams) == 1:
test = streams[streams.keys()[0]]
else:
test = select_quality(self.options, streams)
swf = "http://www.tv4play.se/flash/tv4playflashlets.swf"
other = "-W %s -y %s" % (swf, test["path"])
if test["uri"][0:4] == "rtmp":
download_rtmp(self.options, test["uri"], self.output, self.live, other, self.resume)
elif test["uri"][len(test["uri"])-3:len(test["uri"])] == "f4m":
match = re.search("\/se\/secure\/", test["uri"])
if match:
log.error("This stream is encrypted. Use --hls option")
sys.exit(2)
manifest = "%s?hdcore=2.8.0&g=hejsan" % test["path"]
download_hds(self.options, manifest, self.output, swf)
class Svtplay():
def __init__(self, options, output, live, resume):
self.options = options
self.output = output
self.live = live
self.resume = resume
def get(self, url):
url = url + "?type=embed"
data = get_http_data(url)
match = re.search("value=\"(/(public)?(statiskt)?/swf/video/svtplayer-[0-9\.]+swf)\"", data)
swf = "http://www.svtplay.se" + match.group(1)
other = "-W " + swf
url = url + "&output=json&format=json"
data = json.loads(get_http_data(url))
self.live = data["video"]["live"]
streams = {}
for i in data["video"]["videoReferences"]:
if self.options.hls and i["playerType"] == "ios":
stream = {}
stream["url"] = i["url"]
streams[int(i["bitrate"])] = stream
elif not self.options.hls and i["playerType"] == "flash":
stream = {}
stream["url"] = i["url"]
streams[int(i["bitrate"])] = stream
if len(streams) == 0:
log.error("Can't find any streams.")
sys.exit(2)
elif len(streams) == 1:
test = streams[streams.keys()[0]]
else:
test = select_quality(self.options, streams)
if test["url"][0:4] == "rtmp":
download_rtmp(self.options, test["url"], self.output, self.live, other, self.resume)
elif self.options.hls:
download_hls(self.options, test["url"], self.output, self.live, other)
elif test["url"][len(test["url"])-3:len(test["url"])] == "f4m":
match = re.search("\/se\/secure\/", test["url"])
if match:
log.error("This stream is encrypted. Use --hls option")
sys.exit(2)
manifest = "%s?hdcore=2.8.0&g=hejsan" % test["url"]
download_hds(self.options, manifest, self.output, swf)
else:
download_http(test["url"], self.output)
def progressbar(total, pos, msg=""):
"""
Given a total and a progress position, output a progress bar
to stderr. It is important to not output anything else while
using this, as it relies soley on the behavior of carriage
return (\\r).
Can also take an optioal message to add after the
progressbar. It must not contain newliens.
The progress bar will look something like this:
[099/500][=========...............................] ETA: 13:36:59
Of course, the ETA part should be supplied be the calling
function.
"""
width = 50 # TODO hardcoded progressbar width
rel_pos = int(float(pos)/total*width)
bar = str()
# FIXME ugly generation of bar
for i in range(0, rel_pos):
bar += "="
for i in range(rel_pos, width):
bar += "."
# Determine how many digits in total (base 10)
digits_total = len(str(total))
fmt_width = "%0" + str(digits_total) + "d"
fmt = "\r[" + fmt_width + "/" + fmt_width + "][%s] %s"
progress_stream.write(fmt % (pos, total, bar, msg))
def get_media(url, options):
output = options.output
live = options.live
resume = options.resume
silent = options.silent
hls = options.hls
if not output or os.path.isdir(output):
data = get_http_data(url)
match = re.search("(?i)<title>\s*(.*?)\s*</title>", data)
if match:
if sys.version_info > (3, 0):
title = re.sub('[^\w\s-]', '', match.group(1)).strip().lower()
if output:
output = output + re.sub('[-\s]+', '-', title)
else:
output = re.sub('[-\s]+', '-', title)
else:
title = unicode(re.sub('[^\w\s-]', '', match.group(1)).strip().lower())
if output:
output = unicode(output + re.sub('[-\s]+', '-', title))
else:
output = unicode(re.sub('[-\s]+', '-', title))
if re.findall("(twitch|justin).tv", url):
parse = urlparse(url)
match = re.search("/b/(\d+)", parse.path)
if match:
url = "http://api.justin.tv/api/broadcast/by_archive/%s.xml?onsite=true" % match.group(1)
justin = Justin2(options, output, live, resume)
justin.get(url)
else:
match = re.search("/(.*)", parse.path)
if match:
user = match.group(1)
data = get_http_data(url)
match = re.search("embedSWF\(\"(.*)\", \"live", data)
if not match:
log.error("Can't find swf file.")
other = match.group(1)
url = "http://usher.justin.tv/find/%s.xml?type=any&p=2321" % user
live = True
justin = Justin(options, output, live, other)
justin.get(url)
if re.findall("hbo.com", url):
parse = urlparse(url)
try:
other = parse[5]
except KeyError:
log.error("Something wrong with that url")
sys.exit(2)
match = re.search("^/(.*).html", other)
if not match:
log.error("Cant find video file")
sys.exit(2)
url = "http://www.hbo.com/data/content/" + match.group(1) + ".xml"
hbo = Hbo(options, output, live, resume)
hbo.get(url)
if re.findall("tv4play", url):
parse = urlparse(url)
try:
vid = parse_qs(parse[4])["video_id"][0]
except KeyError:
log.error("Can't find video file")
sys.exit(2)
url = "http://premium.tv4play.se/api/web/asset/%s/play" % vid
tv4play = Tv4play(options, output, live, resume)
tv4play.get(url)
elif re.findall("(tv3play|tv6play|tv8play)", url):