def get(self, options, url): data = get_http_data(url) match = re.search("xmlUrl: '(http://www.expressen.*)'", data) if not match: log.error("Can't find video file") sys.exit(2) url = "http://tv.expressen.se/%s/?standAlone=true&output=xml" % quote_plus(match.group(1)) url = match.group(1) 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(options, streams) filename = test match = re.search("rtmp://([0-9a-z\.]+/[0-9]+/)(.*)", filename) filename = "rtmp://%s" % match.group(1) options.other = "-y %s" % match.group(2) download_rtmp(options, filename)
def get_one_media(stream, options): if not options.output or os.path.isdir(options.output): data = stream.get_urldata() match = re.search(r"(?i)<title[^>]*>\s*(.*?)\s*</title>", data, re.S) if match: options.output_auto = True title_tag = decode_html_entities(match.group(1)) if not options.output: options.output = filenamify(title_tag) else: # output is a directory options.output = os.path.join(options.output, filenamify(title_tag)) if platform.system() == "Windows": # ugly hack. replace \ with / or add extra \ because c:\test\kalle.flv will add c:_tab_est\kalle.flv if options.output.find("\\") > 0: options.output = options.output.replace("\\", "/") videos = [] subs = [] streams = stream.get(options) if streams: for i in streams: if isinstance(i, VideoRetriever): if options.preferred: if options.preferred == i.name(): videos.append(i) else: videos.append(i) if isinstance(i, subtitle): subs.append(i) if options.subtitle and options.output != "-": if subs: subs[0].download(copy.copy(options)) if options.force_subtitle: return if len(videos) > 0: stream = select_quality(options, videos) try: stream.download() except UIException as e: if options.verbose: raise e log.error(e.message) sys.exit(2) if options.thumbnail: if hasattr(stream, "get_thumbnail"): log.info("thumb requested") if options.output != "-": log.info("getting thumbnail") stream.get_thumbnail(options) else: log.info("no thumb requested") else: log.error("Can't find any streams for that url") else: log.error("Can't find any streams for that url")
def get(self, options, 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/%s.xml" % match.group(1) 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(options, streams) download_rtmp(options, test["path"])
def download_hds(options, url, swf=None): 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 options.output != "-": extension = re.search("(\.[a-z0-9]+)$", options.output) if not extension: options.output = "%s.flv" % options.output log.info("Outfile: %s", options.output) file_d = open(options.output, "wb") else: file_d = sys.stdout metasize = struct.pack(">L", len(base64.b64decode(test["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(binascii.a2b_hex(b"00000000")) total = antal[1]["total"] eta = ETA(total) while i <= total: url = "%s/%sSeg1-Frag%s" % (baseurl, test["url"], i) if options.output != "-": eta.update(i) progressbar(total, i, ''.join(["ETA: ", str(eta)])) data = get_http_data(url) number = decode_f4f(i, data) file_d.write(data[number:]) i += 1 if options.output != "-": file_d.close() progress_stream.write('\n')
def get(self, options, url): if re.findall("dn.se", url): data = get_http_data(url) match = re.search("data-qbrick-mcid=\"([0-9A-F]+)\"", data) if not match: match = re.search("mediaId = \'([0-9A-F]+)\';", data) if not match: log.error("Can't find video file") sys.exit(2) mcid = "%sDE1BA107" % match.group(1) else: mcid = match.group(1) host = "http://vms.api.qbrick.com/rest/v3/getsingleplayer/%s" % mcid elif re.findall("di.se", url): data = get_http_data(url) match = re.search("ccid: \"(.*)\"\,", data) if not match: log.error("Can't find video file") sys.exit(2) host = "http://vms.api.qbrick.com/rest/v3/getplayer/%s" % match.group(1) elif re.findall("svd.se", url): match = re.search("_([0-9]+)\.svd", url) if not match: log.error("Can't find video file") sys.exit(2) data = get_http_data("http://www.svd.se/?service=ajax&type=webTvClip&articleId=%s" % match.group(1)) match = re.search("mcid=([A-F0-9]+)\&width=", data) if not match: log.error("Can't find video file") sys.exit(2) host = "http://vms.api.qbrick.com/rest/v3/getsingleplayer/%s" % match.group(1) else: log.error("Can't find site") sys.exit(2) data = get_http_data(host) xml = ET.XML(data) try: url = xml.find("media").find("item").find("playlist").find("stream").find("format").find("substream").text except AttributeError: log.error("Can't find video file") sys.exit(2) 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(options, streams) options.other = "-y %s" % path download_rtmp(options, server)
def get_one_media(stream, options): # Make an automagic filename if not filename(options, stream): return videos = [] subs = [] streams = stream.get(options) for i in streams: if isinstance(i, VideoRetriever): if options.preferred: if options.preferred.lower() == i.name(): videos.append(i) else: videos.append(i) if isinstance(i, subtitle): subs.append(i) if options.subtitle and options.output != "-": if subs: subs[0].download() if options.force_subtitle: return if len(videos) == 0: log.error("Can't find any streams for that url") else: if options.list_quality: list_quality(videos) return stream = select_quality(options, videos) log.info("Selected to download %s, bitrate: %s", stream.name(), stream.bitrate) if options.get_url: print(stream.url) return try: stream.download() except UIException as e: if options.verbose: raise e log.error(e.message) sys.exit(2) if options.thumbnail and hasattr(stream, "get_thumbnail"): if options.output != "-": log.info("Getting thumbnail") stream.get_thumbnail(options) else: log.warning("Can not get thumbnail when fetching to stdout")
def get(self, options, url): data = get_http_data(url) match = re.search(r'resource:[ ]*"([^"]*)",', data) resource_url = match.group(1) resource_data = get_http_data(resource_url) resource = json.loads(resource_data) streams = {} for stream in resource['links']: streams[stream['bitrateKbps']] = stream['uri'] if len(streams) == 1: uri = streams[list(streams.keys())[0]] else: uri = select_quality(options, streams) # need -v ? options.other = "-v -y '" + uri.replace("rtmp://vod.dr.dk/cms/", "") + "'" download_rtmp(options, uri)
def get(self, options, 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) data = get_http_data(url) xml = ET.XML(data) url = xml.find("archive").find("video_file_url").text download_http(options, 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.") options.other = match.group(1) url = "http://usher.justin.tv/find/%s.xml?type=any&p=2321" % user options.live = True 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"] = "%s/%s" % (i.find("connect").text, i.find("play").text) streams[int(i.find("video_height").text)] = stream except AttributeError: pass if len(streams) > 0: test = select_quality(options, streams) options.other = "-j '%s' -W %s" % (test["token"], options.other) options.resume = False download_rtmp(options, test["url"]) else: log.error("Can't any streams") sys.exit(2)
def get(self, options, url): cj = CookieJar() match = re.search(".*video/([0-9]+)", url) if not match: log.error("Can't find video file") sys.exit(2) video_id = match.group(1) if options.username and options.password: #bogus cc = Cookie(None, 'asdf', None, '80', '80', 'www.kanal5play.se', None, None, '/', None, False, False, 'TestCookie', None, None, None) cj.set_cookie(cc) #get session cookie data = get_http_data("http://www.kanal5play.se/", cookiejar=cj) authurl = "https://kanal5swe.appspot.com/api/user/login?callback=jQuery171029989&email=%s&password=%s&_=136250" % (options.username, options.password) data = get_http_data(authurl) match = re.search("({.*})\);", data) jsondata = json.loads(match.group(1)) if jsondata["success"] == False: log.error(jsondata["message"]) sys.exit(2) authToken = jsondata["userData"]["auth"] cc = Cookie(version=0, name='authToken', value=authToken, port=None, port_specified=False, domain='www.kanal5play.se', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}) cj.set_cookie(cc) format = "FLASH" if options.hls: format = "IPHONE" url = "http://www.kanal5play.se/api/getVideo?format=%s&videoId=%s" % (format, video_id) data = json.loads(get_http_data(url, cookiejar=cj)) options.live = data["isLive"] if data["hasSubtitle"]: subtitle = "http://www.kanal5play.se/api/subtitles/%s" % video_id if options.hls: url = data["streams"][0]["source"] baseurl = url[0:url.rfind("/")] if data["streams"][0]["drmProtected"]: log.error("We cant download drm files for this site.") sys.exit(2) download_hls(options, url, baseurl) else: steambaseurl = data["streamBaseUrl"] streams = {} for i in data["streams"]: stream = {} stream["source"] = i["source"] streams[int(i["bitrate"])] = stream test = select_quality(options, streams) filename = test["source"] match = re.search("^(.*):", filename) options.other = "-W %s -y %s " % ("http://www.kanal5play.se/flash/K5StandardPlayer.swf", filename) download_rtmp(options, steambaseurl) if options.subtitle: if options.output != "-": data = get_http_data(subtitle, cookiejar=cj) subtitle_json(options, data)
def get_one_media(stream, options): # Make an automagic filename if not filename(stream): return if options.merge_subtitle: from svtplay_dl.utils import which if not which('ffmpeg'): log.error("--merge-subtitle needs ffmpeg. Please install ffmpeg.") log.info("https://ffmpeg.org/download.html") sys.exit(2) videos = [] subs = [] subfixes = [] error = [] streams = stream.get() try: for i in streams: if isinstance(i, VideoRetriever): if options.preferred: if options.preferred.lower() == i.name(): videos.append(i) else: videos.append(i) if isinstance(i, subtitle): subs.append(i) if isinstance(i, Exception): error.append(i) except Exception as e: if options.verbose: raise else: log.error("svtplay-dl crashed") log.error("Run again and add --verbose as an argument, to get more information") log.error("If the error persists, you can report it at https://github.com/spaam/svtplay-dl/issues") log.error("Include the URL used, the stack trace and the output of svtplay-dl --version in the issue") sys.exit(3) if options.require_subtitle and not subs: log.info("No subtitles available") return if options.subtitle and options.get_url: if subs: if options.get_all_subtitles: for sub in subs: print(sub.url) else: print(subs[0].url) if options.force_subtitle: return def options_subs_dl(subfixes): if subs: if options.get_all_subtitles: for sub in subs: sub.download() if options.merge_subtitle: if sub.subfix: subfixes += [sub.subfix] else: options.get_all_subtitles = False else: subs[0].download() elif options.merge_subtitle: options.merge_subtitle = False if options.subtitle and options.output != "-" and not options.get_url: options_subs_dl(subfixes) if options.force_subtitle: return if options.merge_subtitle and not options.subtitle: options_subs_dl(subfixes) if not videos: log.error("No videos found.") for exc in error: log.error(str(exc)) else: if options.list_quality: list_quality(videos) return try: stream = select_quality(options, videos) if options.get_url: print(stream.url) return log.info("Selected to download %s, bitrate: %s", stream.name(), stream.bitrate) stream.download() except UIException as e: if options.verbose: raise e log.error(e) sys.exit(2) if options.thumbnail and hasattr(stream, "get_thumbnail"): if options.output != "-": log.info("Getting thumbnail") stream.get_thumbnail(options) else: log.warning("Can not get thumbnail when fetching to stdout") post = postprocess(stream, options, subfixes) if stream.name() == "dash" and post.detect: post.merge() if stream.name() == "dash" and not post.detect and stream.finished: log.warning("Cant find ffmpeg/avconv. audio and video is in seperate files. if you dont want this use -P hls or hds") if options.remux: post.remux() if options.silent_semi and stream.finished: log.log(25, "Download of %s was completed" % stream.options.output)
def get_one_media(stream, options): # Make an automagic filename if not filename(stream): return if options.merge_subtitle: from svtplay_dl.utils import which if not which('ffmpeg'): log.error("--merge-subtitle needs ffmpeg. Please install ffmpeg.") log.info("https://ffmpeg.org/download.html") sys.exit(2) videos = [] subs = [] subfixes = [] error = [] streams = stream.get() try: for i in streams: if isinstance(i, VideoRetriever): if options.preferred: if options.preferred.lower() == i.name(): videos.append(i) else: videos.append(i) if isinstance(i, subtitle): subs.append(i) if isinstance(i, Exception): error.append(i) except Exception as e: if options.verbose: log.error("version: %s" % __version__) raise else: log.error("svtplay-dl crashed") log.error("Run again and add --verbose as an argument, to get more information") log.error("If the error persists, you can report it at https://github.com/spaam/svtplay-dl/issues") log.error("Include the URL used, the stack trace and the output of svtplay-dl --version in the issue") sys.exit(3) if options.require_subtitle and not subs: log.info("No subtitles available") return if options.subtitle and options.get_url: if subs: if options.get_all_subtitles: for sub in subs: print(sub.url) else: print(subs[0].url) if options.force_subtitle: return def options_subs_dl(subfixes): if subs: if options.get_all_subtitles: for sub in subs: sub.download() if options.merge_subtitle: if sub.subfix: subfixes += [sub.subfix] else: options.get_all_subtitles = False else: subs[0].download() elif options.merge_subtitle: options.merge_subtitle = False if options.subtitle and options.output != "-" and not options.get_url: options_subs_dl(subfixes) if options.force_subtitle: return if options.merge_subtitle and not options.subtitle: options_subs_dl(subfixes) if len(videos) == 0: for exc in error: log.error(str(exc)) else: if options.list_quality: list_quality(videos) return try: stream = select_quality(options, videos) if options.get_url: print(stream.url) return log.info("Selected to download %s, bitrate: %s", stream.name(), stream.bitrate) stream.download() except UIException as e: if options.verbose: raise e log.error(e) sys.exit(2) if options.thumbnail and hasattr(stream, "get_thumbnail"): if options.output != "-": log.info("Getting thumbnail") stream.get_thumbnail(options) else: log.warning("Can not get thumbnail when fetching to stdout") post = postprocess(stream, options, subfixes) if stream.name() == "dash" and post.detect: post.merge() if stream.name() == "dash" and not post.detect and stream.finished: log.warning("Cant find ffmpeg/avconv. audio and video is in seperate files. if you dont want this use -P hls or hds") if options.remux: post.remux() if options.silent_semi and stream.finished: log.log(25, "Download of %s was completed" % stream.options.output)
def get_one_media(stream, options): # Make an automagic filename if not filename(options, stream): return videos = [] subs = [] error = [] streams = stream.get(options) try: for i in streams: if isinstance(i, VideoRetriever): if options.preferred: if options.preferred.lower() == i.name(): videos.append(i) else: videos.append(i) if isinstance(i, subtitle): subs.append(i) if isinstance(i, Exception): error.append(i) except Exception as e: if options.verbose: raise e else: print("Script crashed. please run the script again and add --verbose as an argument") print("Make an issue with the url you used and include the stacktrace. please include the version of the script") sys.exit(3) if options.require_subtitle and not subs: log.info("No subtitles available") return if options.subtitle and options.output != "-": if subs: subs[0].download() if options.force_subtitle: return if len(videos) == 0: for exc in error: log.error(str(exc)) sys.exit(2) else: if options.list_quality: list_quality(videos) return stream = select_quality(options, videos) log.info("Selected to download %s, bitrate: %s", stream.name(), stream.bitrate) if options.get_url: print(stream.url) return try: stream.download() except UIException as e: if options.verbose: raise e log.error(e) sys.exit(2) if options.thumbnail and hasattr(stream, "get_thumbnail"): if options.output != "-": log.info("Getting thumbnail") stream.get_thumbnail(options) else: log.warning("Can not get thumbnail when fetching to stdout")
def get_one_media(stream, options): # Make an automagic filename if not filename(stream): return videos = [] subs = [] error = [] streams = stream.get() try: for i in streams: if isinstance(i, VideoRetriever): if options.preferred: if options.preferred.lower() == i.name(): videos.append(i) else: videos.append(i) if isinstance(i, subtitle): subs.append(i) if isinstance(i, Exception): error.append(i) except Exception as e: if options.verbose: raise else: log.error("svtplay-dl crashed") log.error("Run again and add --verbose as an argument, to get more information") log.error("If the error persists, you can report it at https://github.com/spaam/svtplay-dl/issues") log.error("Include the URL used, the stack trace and the output of svtplay-dl --version in the issue") sys.exit(3) if options.require_subtitle and not subs: log.info("No subtitles available") return if options.subtitle and options.output != "-": if subs: subs[0].download() if options.force_subtitle: return if len(videos) == 0: for exc in error: log.error(str(exc)) else: if options.list_quality: list_quality(videos) return stream = select_quality(options, videos) log.info("Selected to download %s, bitrate: %s", stream.name(), stream.bitrate) if options.get_url: print(stream.url) return try: stream.download() except UIException as e: if options.verbose: raise e log.error(e) sys.exit(2) if options.thumbnail and hasattr(stream, "get_thumbnail"): if options.output != "-": log.info("Getting thumbnail") stream.get_thumbnail(options) else: log.warning("Can not get thumbnail when fetching to stdout") post = postprocess(stream) if stream.name() == "dash" and post.detect: post.merge() if stream.name() == "dash" and not post.detect and stream.finished: log.warning("Cant find ffmpeg/avconv. audio and video is in seperate files. if you dont want this use -P hls or hds") if options.remux: post.remux()
def get(self, options, url): if re.findall("svt.se", url): data = get_http_data(url) match = re.search("data-json-href=\"(.*)\"", data) if match: filename = match.group(1).replace("&", "&").replace("&format=json", "") url = "http://www.svt.se%s" % filename else: log.error("Can't find video file") sys.exit(2) url = "%s?type=embed" % url data = get_http_data(url) match = re.search("value=\"(/(public)?(statiskt)?/swf/video/svtplayer-[0-9\.]+swf)\"", data) swf = "http://www.svtplay.se%s" % match.group(1) options.other = "-W %s" % swf url = "%s&output=json&format=json" % url data = json.loads(get_http_data(url)) options.live = data["video"]["live"] streams = {} streams2 = {} #hack.. for i in data["video"]["videoReferences"]: if options.hls and i["playerType"] == "ios": stream = {} stream["url"] = i["url"] streams[int(i["bitrate"])] = stream elif not options.hls and i["playerType"] == "flash": stream = {} stream["url"] = i["url"] streams[int(i["bitrate"])] = stream if options.hls and i["playerType"] == "flash": stream = {} stream["url"] = i["url"] streams2[int(i["bitrate"])] = stream if len(streams) == 0 and options.hls: test = streams2[0] test["url"] = test["url"].replace("/z/", "/i/").replace("manifest.f4m", "master.m3u8") elif len(streams) == 0: log.error("Can't find any streams.") sys.exit(2) elif len(streams) == 1: test = streams[list(streams.keys())[0]] else: test = select_quality(options, streams) if test["url"][0:4] == "rtmp": download_rtmp(options, test["url"]) elif options.hls: download_hls(options, test["url"]) 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(options, manifest, swf) else: download_http(options, test["url"]) if options.subtitle: try: subtitle = data["video"]["subtitleReferences"][0]["url"] except KeyError: sys.exit(1) if len(subtitle) > 0: if options.output != "-": data = get_http_data(subtitle) subtitle_wsrt(options, data)
def get(self, options, url): parse = urlparse(url) if "tv4play.se" in url: try: vid = parse_qs(parse[4])["video_id"][0] except KeyError: log.error("Can't find video file") sys.exit(2) else: match = re.search("-(\d+)$", url) if match: vid = match.group(1) else: data = get_http_data(url) match = re.search("\"vid\":\"(\d+)\",", data) if match: vid = match.group(1) else: log.error("Can't find video file") sys.exit(2) url = "http://premium.tv4play.se/api/web/asset/%s/play" % vid 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: if xml.find("live").text != "false": options.live = True streams = {} subtitle = False for i in sa: if i.find("mediaFormat").text != "smi": stream = {} stream["uri"] = i.find("base").text stream["path"] = i.find("url").text streams[int(i.find("bitrate").text)] = stream elif i.find("mediaFormat").text == "smi": subtitle = i.find("url").text if len(streams) == 1: test = streams[list(streams.keys())[0]] else: test = select_quality(options, streams) swf = "http://www.tv4play.se/flash/tv4playflashlets.swf" options.other = "-W %s -y %s" % (swf, test["path"]) if test["uri"][0:4] == "rtmp": download_rtmp(options, test["uri"]) 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(options, manifest, swf) if options.subtitle and subtitle: data = get_http_data(subtitle) subtitle_smi(options, data)
def download_hls(options, url, baseurl=None): 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) if baseurl and test[:4] != 'http': test = "%s/%s" % (baseurl, test) m3u8 = get_http_data(test) globaldata, files = parsem3u(m3u8) encrypted = False key = None try: keydata = globaldata["KEY"] encrypted = True except: pass if encrypted: try: from Crypto.Cipher import AES except ImportError: log.error("You need to install pycrypto to download encrypted HLS streams") sys.exit(2) match = re.search("URI=\"(http://.*)\"", keydata) key = get_http_data(match.group(1)) rand = os.urandom(16) decryptor = AES.new(key, AES.MODE_CBC, rand) n = 1 if options.output != "-": extension = re.search("(\.[a-z0-9]+)$", options.output) if not extension: options.output = "%s.ts" % options.output log.info("Outfile: %s", options.output) file_d = open(options.output, "wb") else: file_d = sys.stdout start = time.time() estimated = "" for i in files: item = i[0] if options.output != "-": progressbar(len(files), n, estimated) if item[0:5] != "http:": item = "%s/%s" % (baseurl, item) data = get_http_data(item) if encrypted: lots = StringIO(data) plain = b"" 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) now = time.time() dt = now - start et = dt / (n + 1) * len(files) rt = et - dt td = timedelta(seconds = int(rt)) estimated = "Estimated Remaining: " + str(td) n += 1 if options.output != "-": file_d.close() progress_stream.write('\n')