Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #
- # "blogava" - blogger avatar fetch - MS-potilas 2.11.2011
- #
- # fetch profile image from blogger user id. usage:
- # http://avafavico.appspot.com/?userid=01234567890
- #
- # This is like my first python app, so it may not be too shiny... but it works
- #
- import cgi
- import re
- from google.appengine.api import images
- from google.appengine.api import urlfetch
- from google.appengine.api import memcache
- def getFavico(domain):
- # first check cache
- result = memcache.get(key=domain)
- if result is None:
- result = urlfetch.fetch("http://www.google.com/s2/favicons?domain=" + domain)
- memcache.add(key=domain, value=result, time=14400)
- return result
- def main():
- form = cgi.FieldStorage(keep_blank_values = True)
- userid = form.getfirst("userid")
- if( userid == None ):
- print "Content-Type: html"
- print "Cache-Control: max-age=14400"
- print
- 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>'
- return 0
- if not re.match('^\d+$', userid):
- print "Content-Type: text"
- print
- print "invalid userid"
- return 1
- # first check cache
- thedata = memcache.get(key=userid)
- if thedata is None:
- domain = "www.blogger.com" # fallback favico
- url = "http://www.blogger.com/profile/"+userid
- result = urlfetch.fetch(url)
- if result.status_code == 200:
- found = re.search('<img class="photo" src="(.+?)"', result.content)
- if found is None:
- found = re.search('<a href="http://(.+?)/" rel="contributor-to', result.content)
- if found is not None:
- domain = found.group(1)
- found = None
- if found is None:
- result = getFavico(domain)
- else:
- result = urlfetch.fetch(found.group(1))
- # if loading has failed, fallback to domain's favico
- if result.status_code != 200:
- result = getFavico(domain)
- if result.status_code == 200:
- img = images.Image(result.content)
- if img.width > img.height*1.67:
- img.crop(0.2,0.0,0.8,1.0)
- elif img.height > img.width*1.67:
- img.crop(0.0,0.2,1.0,0.8)
- elif img.width > img.height*1.25:
- img.crop(0.1,0.0,0.9,1.0)
- elif img.height > img.width*1.25:
- img.crop(0.0,0.1,1.0,0.9)
- img.resize(32,32)
- thedata=img.execute_transforms(output_encoding=images.PNG)
- memcache.add(key=userid, value=thedata, time=14400)
- else:
- return 1 #all fetches failed
- print "Content-Type: Image/png"
- print "Cache-Control: max-age=14400"
- print
- print thedata
- return 0
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement