Valkertown Home

2008-12-02-setting my desktop backgrounds from flickr part 2

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

#!/usr/bin/python

import simplejson
import urllib
import random

API_KEY="0fe810d8d66645f25e0e239bf095a105"
FLICKR_REST="http://api.flickr.com/services/rest/"

methods={
    "getList":"flickr.interestingness.getList",
    "getSizes":"flickr.photos.getSizes",
    }

def build_query(method,data=None):
    retval = FLICKR_REST + "?"
    base_params = urllib.urlencode({
            "format":"json",
            "method":methods[method],
            "api_key":API_KEY,
            })
    if data is not None:
        extra_params = urllib.urlencode(data)
    else:
        extra_params = ""
    retval += base_params + "&" + extra_params
    return retval

def do_query(method,data=None):
    conn = urllib.urlopen(build_query(method,data))
    data = conn.read()
    data =data[:-1].replace("jsonFlickrApi(","")
    try:
        retval = simplejson.loads(data)
    except:
        print data
        retval = {}
    return retval
photofeed = do_query("getList")
photo = random.choice(photofeed['photos']['photo'])
photosizes = do_query("getSizes",(("photo_id",photo['id']),))

## photo['url'] =
print  photosizes['sizes']['size'][-1]['source']
## print "%(title)s %(url)s" % photo

It goes well with the previous shells script.

EOP

comments powered by Disqus

© 2008-2012 Carlos A. Perilla deepspawn at valkertown dot org | Based on original design by Andreas Viklund