X-Git-Url: https://code.delx.au/youtube-cgi/blobdiff_plain/a02f6a351646aa37180db5c4cc4e4d107db7ef7e..72632096a78cec42c319ffd6ed44171c855d3b16:/youtube.cgi diff --git a/youtube.cgi b/youtube.cgi index 409b025..7e76456 100755 --- a/youtube.cgi +++ b/youtube.cgi @@ -4,7 +4,6 @@ from __future__ import division import cookielib import cgi -import itertools import json from lxml import html import os @@ -79,14 +78,27 @@ cookiejar = cookielib.CookieJar() urlopener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar)) referrer = "" -def urlopen(url): +def urlopen(url, offset=None): global referrer req = urllib2.Request(url) if referrer: req.add_header("Referer", referrer) referrer = url + req.add_header("User-Agent", USER_AGENT) - return urlopener.open(req) + + if offset: + req.add_header("Range", "bytes=%d-" % offset) + + res = urlopener.open(req) + + content_range = res.info().getheader("Content-Range") + if content_range: + tokens = content_range.split() + assert tokens[0] == "bytes" + start = int(tokens[1].split("-")[0]) + assert start == offset + return res def parse_url(url): f = urlopen(url) @@ -121,6 +133,11 @@ def get_player_config(doc): p2 = line.rfind(";") if p1 >= 0 and p2 > 0: return json.loads(line[p1+1:p2]) + if "ytplayer.config =" in line: + p1 = line.find("ytplayer.config =") + p2 = line.rfind(";") + if p1 >= 0 and p2 > 0: + return json.loads(line[p1+18:p2]) if "'PLAYER_CONFIG': " in line: p1 = line.find(":") if p1 >= 0: @@ -128,29 +145,75 @@ def get_player_config(doc): convert_from_old_itag(player_config) return player_config -def get_best_video(player_config): - url_data = urlparse.parse_qs(player_config["args"]["url_encoded_fmt_stream_map"]) - url_data = itertools.izip_longest( - url_data["url"], - url_data["type"], - url_data["quality"], - url_data.get("sig", []), +def extract_function(output, script, func_name): + p1 = script.find("function " + func_name) + p2 = script.find("}", p1) + code = script[p1:p2+1] + output.append(code) + deps = re.findall(R"[^\.]\b([a-zA-Z]+)\(", code) + deps = set(deps) + deps.remove(func_name) + for dep in deps: + extract_function(output, script, dep) + +def decode_signature(js_url, s): + script = urlopen(js_url).read() + func_name = re.search(R"\b([a-zA-Z]+)\([a-zA-Z]+\.s\);", script).groups()[0] + + codes = [] + extract_function(codes, script, func_name) + + p = subprocess.Popen( + "js", + shell=True, + close_fds=True, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE ) + for code in codes: + p.stdin.write(code + "\n") + p.stdin.write("console.log(%s('%s'));\n" % (func_name, s)) + p.stdin.close() + + signature = p.stdout.read().strip() + if p.wait() != 0: + raise Exception("js failed to execute: %d" % p.returncode) + + return signature + +def get_best_video(player_config): + url_data_list = player_config["args"]["url_encoded_fmt_stream_map"].split(",") + js_url = player_config["assets"]["js"] + best_url = None best_quality = None best_extension = None - for video_url, mimetype, quality, signature in url_data: - mimetype = mimetype.split(";")[0] + for url_data in url_data_list: + url_data = urlparse.parse_qs(url_data) + mimetype = url_data["type"][0].split(";")[0] + quality = url_data["quality"][0] + + if quality not in QUALITIES: + continue if mimetype not in MIMETYPES: continue + extension = MIMETYPES[mimetype] - quality = QUALITIES.get(quality.split(",")[0], -1) - if best_quality is None or quality > best_quality: - if signature: - video_url = append_to_qs(video_url, {"signature": signature}) - best_url = video_url - best_quality = quality - best_extension = extension + quality = QUALITIES.get(quality, -1) + + if best_quality is not None and quality < best_quality: + continue + + video_url = url_data["url"][0] + if "sig" in url_data: + signature = url_data["sig"][0] + else: + signature = decode_signature(js_url, url_data["s"][0]) + video_url = append_to_qs(video_url, {"signature": signature}) + + best_url = video_url + best_quality = quality + best_extension = extension return best_url, best_extension @@ -215,37 +278,46 @@ def cgimain(): ) return -def copy_with_progress(total_size, infile, outfile): - def pp_size(size): - suffixes = ["", "KiB", "MiB", "GiB"] - for i, suffix in enumerate(suffixes): - if size < 1024: - break - size /= 1024 - return "%.2f %s" % (size, suffix) +def pp_size(size): + suffixes = ["", "KiB", "MiB", "GiB"] + for i, suffix in enumerate(suffixes): + if size < 1024: + break + size /= 1024 + return "%.2f %s" % (size, suffix) + +def copy_with_progress(content_length, infile, outfile): + def print_status(): + rate = 0 + if now != last_ts: + rate = last_bytes_read / (now - last_ts) + sys.stdout.write("\33[2K\r") + sys.stdout.write("%s / %s (%s/sec)" % ( + pp_size(bytes_read), + pp_size(content_length), + pp_size(rate), + )) + sys.stdout.flush() - start_ts = time.time() last_ts = 0 + last_bytes_read = 0 bytes_read = 0 while True: now = time.time() if now - last_ts > 0.5: + print_status() last_ts = now - sys.stdout.write("\33[2K\r") - sys.stdout.write("%s / %s (%s/sec)" % ( - pp_size(bytes_read), - pp_size(total_size), - pp_size(bytes_read / (now - start_ts)), - )) - sys.stdout.flush() + last_bytes_read = 0 buf = infile.read(32768) if not buf: break outfile.write(buf) + last_bytes_read += len(buf) bytes_read += len(buf) # Newline at the end + print_status() print def main(): @@ -254,19 +326,51 @@ def main(): except: print >>sys.stderr, "Usage: %s http://youtube.com/watch?v=FOOBAR" % sys.argv[0] sys.exit(1) + doc = parse_url(url) video_url, filename = get_video_url(doc) - video_data = urlopen(video_url) - outfile = open(filename, "w") - total_size = int(video_data.info().getheader("Content-Length")) print "Downloading", filename.encode("utf-8") - copy_with_progress(total_size, video_data, outfile) - video_data.close() + + outfile = open(filename, "a") + offset = outfile.tell() + if offset > 0: + print "Resuming download from", pp_size(offset) + total_size = None + + while True: + try: + video_data = urlopen(video_url, offset) + except urllib2.HTTPError, e: + if e.code == 416: + print "File is complete!" + break + else: + raise + + content_length = int(video_data.info().getheader("Content-Length")) + if total_size is None: + total_size = content_length + + try: + copy_with_progress(content_length, video_data, outfile) + except IOError, e: + print + + video_data.close() + if outfile.tell() != total_size: + old_offset = offset + offset = outfile.tell() + if old_offset == offset: + time.sleep(1) + print "Restarting download from", pp_size(offset) + else: + break + outfile.close() if __name__ == "__main__": - resource.setrlimit(resource.RLIMIT_AS, (MAX_MEMORY_BYTES, MAX_MEMORY_BYTES)) +### resource.setrlimit(resource.RLIMIT_AS, (MAX_MEMORY_BYTES, MAX_MEMORY_BYTES)) if os.environ.has_key("SCRIPT_NAME"): cgimain() else: