1
0
mirror of https://github.com/spaam/svtplay-dl.git synced 2024-12-01 07:34:16 +01:00
svtplay-dl/lib/svtplay_dl/fetcher/hls.py

140 lines
3.9 KiB
Python
Raw Normal View History

2013-03-02 21:26:28 +01:00
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
from __future__ import absolute_import
2013-02-12 19:39:52 +01:00
import sys
import os
import re
from svtplay_dl.utils import get_http_data, select_quality
2013-03-25 19:54:41 +01:00
from svtplay_dl.output import progressbar, progress_stream, ETA
from svtplay_dl.log import log
2014-02-08 16:08:39 +01:00
from svtplay_dl.utils.urllib import urlparse
from svtplay_dl.error import UIException
class HLSException(UIException):
def __init__(self, url, message):
self.url = url
super(HLSException, self).__init__(message)
class LiveHLSException(HLSException):
def __init__(self, url):
super(LiveHLSException, self).__init__(
url, "This is a live HLS stream, and they are not supported.")
2013-02-12 19:39:52 +01:00
2014-02-08 16:08:39 +01:00
def _get_full_url(url, srcurl):
if url[:4] == 'http':
return url
urlp = urlparse(srcurl)
# remove everything after last / in the path of the URL
baseurl = re.sub(r'^([^\?]+)/[^/]*(\?.*)?$', r'\1', srcurl)
returl = "%s/%s" % (baseurl, url)
# Append optional query parameters
if urlp.query:
returl += "?%s" % urlp.query
return returl
def download_hls(options, url):
2013-02-12 19:39:52 +01:00
data = get_http_data(url)
globaldata, files = parsem3u(data)
streams = {}
if options.live and not options.force:
raise LiveHLSException(url)
2013-02-12 19:39:52 +01:00
for i in files:
streams[int(i[1]["BANDWIDTH"])] = i[0]
test = select_quality(options, streams)
2014-02-08 16:08:39 +01:00
test = _get_full_url(test, url)
2013-02-12 19:39:52 +01:00
m3u8 = get_http_data(test)
globaldata, files = parsem3u(m3u8)
encrypted = False
key = None
try:
keydata = globaldata["KEY"]
encrypted = True
2013-04-21 12:39:56 +02:00
except KeyError:
2013-02-12 19:39:52 +01:00
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(r'URI="(https?://.*?)"', keydata)
2013-02-12 19:39:52 +01:00
key = get_http_data(match.group(1))
rand = os.urandom(16)
decryptor = AES.new(key, AES.MODE_CBC, rand)
if options.output != "-":
extension = re.search(r"(\.[a-z0-9]+)$", options.output)
2013-02-12 19:39:52 +01:00
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
2013-03-25 19:54:41 +01:00
n = 0
eta = ETA(len(files))
2013-02-12 19:39:52 +01:00
for i in files:
2014-02-08 16:08:39 +01:00
item = _get_full_url(i[0], test)
2013-02-12 19:39:52 +01:00
if options.output != "-":
2013-03-25 19:54:41 +01:00
eta.increment()
progressbar(len(files), n, ''.join(['ETA: ', str(eta)]))
n += 1
2014-02-08 16:08:39 +01:00
2013-02-12 19:39:52 +01:00
data = get_http_data(item)
if encrypted:
2013-09-03 19:10:07 +02:00
data = decryptor.decrypt(data)
2013-02-12 19:39:52 +01:00
file_d.write(data)
if options.output != "-":
file_d.close()
progress_stream.write('\n')
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(",")]
for i in range(0, len(info)):
if info[i][0] == "BANDWIDTH":
streaminfo.update({info[i][0]: info[i][1]})
2013-02-12 19:39:52 +01:00
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