]> code.delx.au - youtube-cgi/blobdiff - youtube.cgi
fixes for recent changes
[youtube-cgi] / youtube.cgi
index 263259cd5ab1c71aea18c449d977a330660755fb..56b89371a968318a7851f0209785097039192df5 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:
@@ -114,50 +117,46 @@ def append_to_qs(url, params):
        url = urlparse.urlunsplit(r)
        return url
 
-def convert_from_old_itag(player_config):
-       url_data = urlparse.parse_qs(player_config["args"]["url_encoded_fmt_stream_map"])
-       url_data["url"] = []
-       for itag_url in url_data["itag"]:
-               pos = itag_url.find("url=")
-               url_data["url"].append(itag_url[pos+4:])
-       player_config["args"]["url_encoded_fmt_stream_map"] = urllib.urlencode(url_data, True)
-
 def get_player_config(doc):
        player_config = None
        for script in doc.xpath("//script"):
                if not script.text:
                        continue
                for line in script.text.split("\n"):
-                       if "yt.playerConfig =" in line:
-                               p1 = line.find("=")
-                               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(";")
+                       s = "ytplayer.config = {"
+                       if s in line:
+                               p1 = line.find(s) + len(s) - 1
+                               p2 = line.find("};", p1) + 1
                                if p1 >= 0 and p2 > 0:
-                                       return json.loads(line[p1+18:p2])
-                       if "'PLAYER_CONFIG': " in line:
-                               p1 = line.find(":")
-                               if p1 >= 0:
-                                       player_config = json.loads(line[p1+1:])
-                                       convert_from_old_itag(player_config)
-                                       return player_config
+                                       return json.loads(line[p1:p2])
+
+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()
 
@@ -179,6 +178,8 @@ def get_best_video(player_config):
                mimetype = url_data["type"][0].split(";")[0]
                quality = url_data["quality"][0]
 
+               if url_data.has_key("stereo3d"):
+                       continue
                if quality not in QUALITIES:
                        continue
                if mimetype not in MIMETYPES:
@@ -193,9 +194,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