document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #!/usr/bin/env python
  2. #
  3. # "blogava" - blogger avatar fetch - MS-potilas 2.11.2011
  4. #
  5. # fetch profile image from blogger user id. usage:
  6. # http://avafavico.appspot.com/?userid=01234567890
  7. #
  8. # This is like my first python app, so it may not be too shiny... but it works
  9. #
  10.  
  11. import cgi
  12. import re
  13.  
  14. from google.appengine.api import images
  15. from google.appengine.api import urlfetch
  16. from google.appengine.api import memcache
  17.  
  18. def getFavico(domain):
  19. # first check cache
  20.   result = memcache.get(key=domain)
  21.   if result is None:
  22.     result = urlfetch.fetch("http://www.google.com/s2/favicons?domain=" + domain)
  23.     memcache.add(key=domain, value=result, time=14400)
  24.   return result
  25.  
  26. def main():
  27.   form = cgi.FieldStorage(keep_blank_values = True)
  28.   userid = form.getfirst("userid")
  29.   if( userid == None ):
  30.     print "Content-Type: html"
  31.     print "Cache-Control: max-age=14400"
  32.     print
  33.     print \'<html><head><link rel="shortcut icon" href="/favicon.ico" /><title>BlogAva</title></head></body>Application to return small icon from Blogger profile, by MS-potilas 2011. Usage: ?userid=USERID. See <a href="http://yabtb.blogspot.com/2011/11/python-tool-to-get-blogger-avatar.html">yabtb.blogspot.com</a>.</body></html>\'
  34.     return 0
  35.  
  36.   if not re.match(\'^\\d+$\', userid):
  37.     print "Content-Type: text"
  38.     print
  39.     print "invalid userid"
  40.     return 1
  41.  
  42. # first check cache
  43.   thedata = memcache.get(key=userid)
  44.   if thedata is None:
  45.     domain = "www.blogger.com" # fallback favico
  46.     url = "http://www.blogger.com/profile/"+userid
  47.     result = urlfetch.fetch(url)
  48.     if result.status_code == 200:
  49.       found = re.search(\'<img class="photo" src="(.+?)"\', result.content)
  50.  
  51.       if found is None:
  52.         found = re.search(\'<a href="http://(.+?)/" rel="contributor-to\', result.content)
  53.         if found is not None:
  54.           domain = found.group(1)
  55.           found = None
  56.  
  57.       if found is None:
  58.         result = getFavico(domain)
  59.       else:
  60.         result = urlfetch.fetch(found.group(1))
  61. # if loading has failed, fallback to domain\'s favico
  62.     if result.status_code != 200:
  63.       result = getFavico(domain)
  64.     if result.status_code == 200:
  65.       img = images.Image(result.content)
  66.  
  67.       if img.width > img.height*1.67:
  68.         img.crop(0.2,0.0,0.8,1.0)
  69.       elif img.height > img.width*1.67:
  70.         img.crop(0.0,0.2,1.0,0.8)
  71.       elif img.width > img.height*1.25:
  72.         img.crop(0.1,0.0,0.9,1.0)
  73.       elif img.height > img.width*1.25:
  74.         img.crop(0.0,0.1,1.0,0.9)
  75.  
  76.       img.resize(32,32)
  77.       thedata=img.execute_transforms(output_encoding=images.PNG)
  78.       memcache.add(key=userid, value=thedata, time=14400)
  79.     else:
  80.       return 1 #all fetches failed
  81.  
  82.   print "Content-Type: Image/png"
  83.   print "Cache-Control: max-age=14400"
  84.   print
  85.   print thedata
  86.   return 0
  87.  
  88. if __name__ == \'__main__\':
  89.   main()
');