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
-
#!/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







Add New Comment
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Add New Comment
Trackbacks
(Trackback URL)