/stuff/info/youtube.cgi

#!/usr/bin/env python

import cgitb
cgitb.enable()

import sys, os, re, urllib

class Grab(urllib.URLopener): 
   def __init__(self, *args): 
      self.version = 'Mozilla/5.0 (YouTube Info Script)'
      urllib.URLopener.__init__(self, *args)
   def http_error_default(self, url, fp, errcode, errmsg, headers): 
      return urllib.addinfourl(fp, [headers, errcode], "http:" + url)
urllib._urlopener = Grab()

gdata = 'http://gdata.youtube.com/feeds/api/videos/'

r_title = re.compile(r"<title type='text'>(.*?)</title>")
r_viewCount = re.compile(r"viewCount='(\d+)'")
r_numRaters = re.compile(r"numRaters='(\d+)'")
r_average = re.compile(r"average='([0-9.]+)'")
r_duration = re.compile(r"duration='(\d+)'")

def format(duration): 
   if duration < 60: return str(duration) + 's'
   return str(duration / 60) + ':' + ('%02i' % (duration % 60))

def formatnumber(n): 
   parts = list(str(n))
   for i in range((len(parts) - 3), 0, -3):
      parts.insert(i, ',')
   return ''.join(parts)

def data(vid): 
   u = urllib.urlopen(gdata + vid)
   bytes = u.read()
   u.close()

   title = r_title.search(bytes).group(1)
   views = int(r_viewCount.search(bytes).group(1))
   views = formatnumber(views)
   try: raters = int(r_numRaters.search(bytes).group(1))
   except: raters = 0
   # print gdata, vid, raters
   raters = formatnumber(raters)
   try: average = r_average.search(bytes).group(1)
   except: average = 0
   duration = format(int(r_duration.search(bytes).group(1)))

   return title, views, average, raters, duration

def text(vid): 
   print 'Content-Type: text/plain'
   print 
   uri = 'http://inamidst.com/stuff/youtube/%s' % vid
   title, views, average, raters, duration = data(vid)
   stats = '%s views, %s/%s' % (views, average, raters)
   stats = ('%s, ' % duration) + stats
   print uri + ' - ' + title[:50] + ' (' + stats + ')'

def html(vid): 
   print 'Content-Type: text/html'
   print 
   print '<!DOCTYPE html> <!-- Try using the full URI as an arg -->'

   title, views, average, raters, duration = data(vid)

   print '<title>' + title + '</title>'

   orig = '<a href="http://www.youtube.com/watch?v=%s">original</a>' % vid
   keep = '<a href="http://keepvid.com/?url=http%3A%2F%2Fwww.youtube.com%2F'
   keep += 'watch%3Fv%3D' + vid + '">download</a>'

   about = '%s &middot; ' % duration
   about += '%s/%s &middot; ' % (average, raters)
   about += '%s views &middot; ' % views
   about += '%s &middot; %s ' % (orig, keep)

   print """
   <style type="text/css">
   body { width: 425px; margin: 3em auto; text-align: center; }
   </style>

   <p>
   <object width="425" height="344">
     <param name="movie" value="http://www.youtube.com/v/%s&hl=en&fs=1"></param>
     <param name="allowFullScreen" value="true"></param>
     <embed src="http://www.youtube.com/v/%s&hl=en&fs=1" type="application/x-shockwave-flash" allowfullscreen="true" 
     width="425" height="344"></embed>
   </object>
   </p>
   <p>%s</p>
   """ % (vid, vid, about)

pathinfo = os.environ.get('PATH_INFO', '/')
arg = pathinfo[1:]
if (not arg) and sys.argv: 
   arg = sys.argv[1]

if '=' in arg: 
   text(arg.split('=').pop())
elif arg: 
   html(arg)
else: print 'A YouTube Video ID is required.'

# EOF