except ImportError:
pass
-CACHE_DIR = os.path.expanduser("~/.cache/webdl")
+CACHE_DIR = os.path.join(os.environ.get("XDG_CACHE_HOME", os.path.expanduser("~/.cache")), "webdl")
USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:21.0) Gecko/20100101 Firefox/21.0"
class Node(object):
import sbs
sbs.fill_nodes(root_node)
- import plus7
- plus7.fill_nodes(root_node)
+### import plus7
+### plus7.fill_nodes(root_node)
import brightcove
brightcove.fill_nodes(root_node)
return open(filename)
src = _urlopen(url)
- dst = open(filename, "w")
+ dst = open(filename, "wb")
try:
shutil.copyfileobj(src, dst)
except Exception, e:
return ext == ".mp4"
-def download_rtmp(filename, vbase, vpath, hash_url=None):
+def download_hds(filename, video_url, pvswf=None):
filename = sanify_filename(filename)
+ video_url = video_url.replace("http://", "hds://")
print "Downloading: %s" % filename
- if vpath.endswith(".flv"):
- vpath = vpath[:-4]
cmd = [
- "rtmpdump",
+ "livestreamer",
"-o", filename,
- "-r", vbase,
- "-y", vpath,
+ "%s pvswf=%s" % (video_url, pvswf),
+ "best",
]
- if hash_url is not None:
- cmd += ["--swfVfy", hash_url]
if exec_subprocess(cmd):
return convert_to_mp4(filename)
else:
return False
-def download_urllib(filename, url, referrer=None):
+def download_hls(filename, video_url):
filename = sanify_filename(filename)
+ video_url = video_url.replace("http://", "hlsvariant://")
print "Downloading: %s" % filename
- try:
- src = _urlopen(url, referrer)
- dst = open(filename, "w")
- while True:
- buf = src.read(1024*1024)
- if not buf:
- break
- dst.write(buf)
- sys.stdout.write(".")
- sys.stdout.flush()
- print
- except KeyboardInterrupt:
- print "\nCancelled", url
+ cmd = [
+ "livestreamer",
+ "-o", filename,
+ video_url,
+ "best",
+ ]
+ if exec_subprocess(cmd):
+ return convert_to_mp4(filename)
+ else:
return False
- finally:
- try:
- src.close()
- except:
- pass
- try:
- dst.close()
- except:
- pass
-
- return convert_to_mp4(filename)
-
-def download_hls_get_stream(url):
- def parse_bandwidth(line):
- params = line.split(":", 1)[1].split(",")
- for kv in params:
- k, v = kv.split("=", 1)
- if k == "BANDWIDTH":
- return int(v)
- return 0
-
- m3u8 = grab_text(url, 0)
- best_bandwidth = None
- best_url = None
- for line in m3u8.split("\n"):
- if line.startswith("#EXT-X-STREAM-INF:"):
- bandwidth = parse_bandwidth(line)
- if best_bandwidth is None or bandwidth > best_bandwidth:
- best_bandwidth = bandwidth
- best_url = None
- elif not line.startswith("#"):
- if best_url is None:
- best_url = line.strip()
-
- if not best_url:
- raise Exception("Failed to find best stream for HLS: " + url)
-
- return best_url
-
-def download_hls_segments(outf, url):
- m3u8 = grab_text(url, 0)
-
- fail_if_not_last_segment = None
- for line in m3u8.split("\n"):
- if not line.strip() or line.startswith("#"):
- continue
-
- if fail_if_not_last_segment:
- raise e
-
- try:
- download_hls_fetch_segment(outf, line)
- except urllib2.HTTPError, e:
- fail_if_not_last_segment = e
- continue
- sys.stdout.write(".")
- sys.stdout.flush()
-
- sys.stdout.write("\n")
-
-def download_hls_fetch_segment(outf, segment_url):
- try:
- src = _urlopen(segment_url)
- shutil.copyfileobj(src, outf)
- except:
- raise
- finally:
- try:
- src.close()
- except:
- pass
-def download_hls(filename, m3u8_master_url, hack_url_func=None):
+def download_rtmp(filename, vbase, vpath, hash_url=None):
filename = sanify_filename(filename)
print "Downloading: %s" % filename
-
- if hack_url_func is None:
- hack_url_func = lambda url: url
-
- tmpdir = tempfile.mkdtemp(prefix="webdl-hls")
-
- try:
- best_stream_url = download_hls_get_stream(hack_url_func(m3u8_master_url))
- ts_file = open(filename, "w")
- download_hls_segments(ts_file, hack_url_func(best_stream_url))
- except KeyboardInterrupt:
- print "\nCancelled", m3u8_master_url
+ if vpath.endswith(".flv"):
+ vpath = vpath[:-4]
+ cmd = [
+ "rtmpdump",
+ "-o", filename,
+ "-r", vbase,
+ "-y", vpath,
+ ]
+ if hash_url is not None:
+ cmd += ["--swfVfy", hash_url]
+ if exec_subprocess(cmd):
+ return convert_to_mp4(filename)
+ else:
return False
- finally:
- shutil.rmtree(tmpdir)
- try:
- ts_file.close()
- except:
- pass
-
- return convert_to_mp4(filename)
def natural_sort(l, key=None):
ignore_list = ["a", "the"]