]> code.delx.au - youtube-cgi/blobdiff - youtube.cgi
handle videos with no signature
[youtube-cgi] / youtube.cgi
index 263259cd5ab1c71aea18c449d977a330660755fb..67153e260d9ab1b44628414c6a0fd0f83767ca43 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/python2
 
 from __future__ import division
 
@@ -79,6 +79,9 @@ urlopener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
 referrer = ""
 
 def urlopen(url, offset=None):
+       if url.startswith("//"):
+               url = "http:" + url
+
        global referrer
        req = urllib2.Request(url)
        if referrer:
@@ -145,19 +148,33 @@ def get_player_config(doc):
                                        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()
 
@@ -193,9 +210,13 @@ def get_best_video(player_config):
                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