Home > Enlaces y noticias > Setting my desktop backgrounds from Flickr part 2

Setting my desktop backgrounds from Flickr part 2

December 2nd, 2008

Well after a while of testing my previous script I found that the overall quality of the images I was getting
was good but from time to time a really horrible image would popup, so I decided to try another approach.

I have heard about Flickr’s interestingness, and decided to see if I could grab images from there, since the overall quality appeared to be really good. I hat to acquire an API key to do it.

But well here’s what I got:

For this I dropped feedparser and had to use simplejson for python

  1. #!/usr/bin/python
  2.  
  3. import simplejson
  4. import urllib
  5. import random
  6.  
  7. API_KEY="0fe810d8d66645f25e0e239bf095a105"
  8. FLICKR_REST="http://api.flickr.com/services/rest/"
  9.  
  10. methods={
  11.     "getList":"flickr.interestingness.getList",
  12.     "getSizes":"flickr.photos.getSizes",
  13.     }
  14.  
  15. def build_query(method,data=None):
  16.     retval = FLICKR_REST + "?"
  17.     base_params = urllib.urlencode({
  18.             "format":"json",
  19.             "method":methods[method],
  20.             "api_key":API_KEY,
  21.             })
  22.     if data is not None:
  23.         extra_params = urllib.urlencode(data)
  24.     else:
  25.         extra_params = ""
  26.     retval += base_params + "&" + extra_params
  27.     return retval
  28.  
  29. def do_query(method,data=None):
  30.     conn = urllib.urlopen(build_query(method,data))
  31.     data = conn.read()
  32.     data =data[:-1].replace("jsonFlickrApi(","")
  33.     try:
  34.         retval = simplejson.loads(data)
  35.     except:
  36.         print data
  37.         retval = {}
  38.     return retval
  39. photofeed = do_query("getList")
  40. photo = random.choice(photofeed[‘photos’][‘photo’])
  41. photosizes = do_query("getSizes",(("photo_id",photo[‘id’]),))
  42.  
  43. ## photo['url'] =
  44. print  photosizes[’sizes’][’size’][-1][’source’]
  45. ## print "%(title)s %(url)s" % photo
  46.  

It goes well with the previous shells script.

EOP

, , , ,

 

Trackbacks

(Trackback URL)

close Reblog this comment
blog comments powered by Disqus