]> code.delx.au - monosys/blobdiff - scripts/youtube.cgi
html encode stuff
[monosys] / scripts / youtube.cgi
index a5d63d294947157498e24bf1a6ce78f985472d98..8cde9795c9b3811c322765dc1bbaf6d58e32ea0d 100755 (executable)
@@ -10,6 +10,9 @@ import sys
 import urllib
 
 
+class VideoUnavailable(Exception):
+       pass
+
 urllib.URLopener.version = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"
 
 fmt_quality = [
@@ -27,6 +30,7 @@ fmt_quality = [
 
 
 def print_form(url="", msg=""):
+       script_url = "http://%s%s" % (os.environ["HTTP_HOST"], os.environ["REQUEST_URI"])
        print "Content-Type: application/xhtml+xml\r\n\r\n"
        print """
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
@@ -52,13 +56,13 @@ def print_form(url="", msg=""):
        <div><input type="text" name="url" value="{1}"/></div>
        <div><input type="submit" value="Download!"/></div>
        </form>
-       <p>Tip! Use this bookmarklet: <a href="javascript:(function(){window.location='http://delx.net.au/utils/youtube.cgi?url='+escape(location);})()">YouTube Download</a>
+       <p>Tip! Use this bookmarklet: <a href="javascript:(function(){window.location='{2}?url='+escape(location);})()">YouTube Download</a>
        to easily download videos. Right-click the link and add it to bookmarks,
        then when you're looking at a YouTube page select that bookmark from your
        browser's bookmarks menu to download the video straight away.</p>
 </body>
 </html>
-""".replace("{0}", msg).replace("{1}", url)
+""".replace("{0}", msg).replace("{1}", url).replace("{2}", script_url)
 
 def parse_url(url):
        f = urllib.urlopen(url)
@@ -67,6 +71,9 @@ def parse_url(url):
        return doc
 
 def get_video_url(doc):
+       unavailable = doc.xpath("//div[@id='unavailable-message']/text()")
+       if unavailable:
+               raise VideoUnavailable(unavailable[0].strip())
        embed = doc.xpath("//embed")[0]
        flashvars = embed.attrib["flashvars"]
        flashvars = cgi.parse_qs(flashvars)
@@ -110,6 +117,11 @@ def cgimain():
                sys.stdout.write("\r\n")
                shutil.copyfileobj(data, sys.stdout)
                data.close()
+       except VideoUnavailable, e:
+               print_form(
+                       url=url,
+                       msg="<p class='error'>Sorry, there was an error: %s</p>" % cgi.escape(e.message)
+               )
        except Exception, e:
                print_form(
                        url=url,