From 383c0650d77975e8f91da33e8d1bca901beba068 Mon Sep 17 00:00:00 2001 From: Johan Andersson Date: Sun, 27 Apr 2014 13:19:34 +0200 Subject: [PATCH] hds: parse and kwargssupport --- lib/svtplay_dl/fetcher/__init__.py | 5 +-- lib/svtplay_dl/fetcher/hds.py | 51 +++++++++++++++--------------- lib/svtplay_dl/service/svtplay.py | 6 ++-- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/lib/svtplay_dl/fetcher/__init__.py b/lib/svtplay_dl/fetcher/__init__.py index 4c053c3..6efcd6a 100644 --- a/lib/svtplay_dl/fetcher/__init__.py +++ b/lib/svtplay_dl/fetcher/__init__.py @@ -1,5 +1,6 @@ class VideoRetriever: - def __init__(self, options, url, bitrate): + def __init__(self, options, url, bitrate, **kwargs): self.options = options self.url = url - self.bitrate = bitrate \ No newline at end of file + self.bitrate = bitrate + self.kwargs = kwargs \ No newline at end of file diff --git a/lib/svtplay_dl/fetcher/hds.py b/lib/svtplay_dl/fetcher/hds.py index 95ea662..837d127 100644 --- a/lib/svtplay_dl/fetcher/hds.py +++ b/lib/svtplay_dl/fetcher/hds.py @@ -41,38 +41,37 @@ class LiveHDSException(HDSException): super(LiveHDSException, self).__init__( url, "This is a live HDS stream, and they are not supported.") +def hdsparse(options, manifest): + data = get_http_data(manifest) + streams = {} + bootstrap = {} + xml = ET.XML(data) + + if is_py2_old: + 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"])] = HDS(options, i.attrib["url"], i.attrib["bitrate"], manifest=manifest, bootstrap=bootstrap[i.attrib["bootstrapInfoId"]], metadata=i.find("{http://ns.adobe.com/f4m/1.0}metadata").text) + return streams + class HDS(VideoRetriever): def download(self): - data = get_http_data(self.url) - streams = {} - bootstrap = {} - xml = ET.XML(data) - if self.options.live and not self.options.force: raise LiveHDSException(self.url) - if is_py2_old: - 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(self.options, streams) - - bootstrap = base64.b64decode(bootstrap[test["bootstrapInfoId"]]) + bootstrap = base64.b64decode(self.kwargs["bootstrap"]) box = readboxtype(bootstrap, 0) antal = None if box[2] == b"abst": antal = readbox(bootstrap, box[0]) - baseurl = self.url[0:self.url.rfind("/")] + baseurl = self.kwargs["manifest"][0:self.kwargs["manifest"].rfind("/")] i = 1 if self.options.output != "-": @@ -84,16 +83,16 @@ class HDS(VideoRetriever): else: file_d = sys.stdout - metasize = struct.pack(">L", len(base64.b64decode(test["metadata"])))[1:] + metasize = struct.pack(">L", len(base64.b64decode(self.kwargs["metadata"])))[1:] file_d.write(binascii.a2b_hex(b"464c560105000000090000000012")) file_d.write(metasize) file_d.write(binascii.a2b_hex(b"00000000000000")) - file_d.write(base64.b64decode(test["metadata"])) + file_d.write(base64.b64decode(self.kwargs["metadata"])) file_d.write(binascii.a2b_hex(b"00000000")) total = antal[1]["total"] - eta = ETA(total) + eta = ETA(total)q while i <= total: - url = "%s/%sSeg1-Frag%s" % (baseurl, test["url"], i) + url = "%s/%sSeg1-Frag%s" % (baseurl, self.url, i) if self.options.output != "-": eta.update(i) progressbar(total, i, ''.join(["ETA: ", str(eta)])) diff --git a/lib/svtplay_dl/service/svtplay.py b/lib/svtplay_dl/service/svtplay.py index 5098fd3..bfbe5cc 100644 --- a/lib/svtplay_dl/service/svtplay.py +++ b/lib/svtplay_dl/service/svtplay.py @@ -9,7 +9,7 @@ import xml.etree.ElementTree as ET from svtplay_dl.service import Service, OpenGraphThumbMixin from svtplay_dl.utils import get_http_data from svtplay_dl.utils.urllib import urlparse -from svtplay_dl.fetcher.hds import HDS +from svtplay_dl.fetcher.hds import HDS, hdsparse from svtplay_dl.fetcher.hls import HLS, hlsparse from svtplay_dl.fetcher.rtmp import RTMP from svtplay_dl.fetcher.http import HTTP @@ -63,7 +63,9 @@ class Svtplay(Service, OpenGraphThumbMixin): match = re.search(r"\/se\/secure\/", i["url"]) if not match: manifest = "%s?hdcore=2.8.0&g=hejsan" % i["url"] - yield HDS(options, manifest, i["bitrate"]) + streams = hdsparse(options, manifest) + for n in list(streams.keys()): + yield streams[n] elif parse.scheme == "rtmp": embedurl = "%s?type=embed" % url data = get_http_data(embedurl)