-#!/usr/bin/env python
+#!/usr/bin/python2
from __future__ import division
referrer = ""
def urlopen(url, offset=None):
+ if url.startswith("//"):
+ url = "http:" + url
+
global referrer
req = urllib2.Request(url)
if referrer:
convert_from_old_itag(player_config)
return player_config
+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"[^\.][= ]([\$0-9a-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-z]+)\([a-z]+\.s\);", script).groups()[0]
- p1 = script.find("function " + func_name)
- p2 = script.find("}", p1)
- func_block = script[p1:p2+1]
+ 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"],
+ "js",
+ shell=True,
+ close_fds=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE
)
- p.stdin.write(func_block + "\n")
+ for code in codes:
+ p.stdin.write(code + "\n")
p.stdin.write("console.log(%s('%s'));\n" % (func_name, s))
p.stdin.close()
video_url = url_data["url"][0]
if "sig" in url_data:
signature = url_data["sig"][0]
- else:
+ elif "s" in url_data:
signature = decode_signature(js_url, url_data["s"][0])
- video_url = append_to_qs(video_url, {"signature": signature})
+ else:
+ signature = None
+
+ if signature:
+ video_url = append_to_qs(video_url, {"signature": signature})
best_url = video_url
best_quality = quality