From 4e6d4f4367fab2af730481a39482fa09b339df00 Mon Sep 17 00:00:00 2001 From: Tony Duckles Date: Sat, 31 Jul 2010 10:43:58 -0500 Subject: [PATCH] Optimize getphoto() by using "&extra" param in API requests to get photo URL's during original queries rather than doing an extra API request each call to getphoto() --- flickrtouchr.py | 62 ++++++++++++++++--------------------------------- 1 file changed, 20 insertions(+), 42 deletions(-) diff --git a/flickrtouchr.py b/flickrtouchr.py index 4638f8e..a29859d 100755 --- a/flickrtouchr.py +++ b/flickrtouchr.py @@ -153,46 +153,17 @@ def flickrsign(url, token): # # Grab the photo from the server # -def getphoto(id, token, filename): - try: - # Contruct a request to find the sizes - url = "http://api.flickr.com/services/rest/?method=flickr.photos.getSizes" - url += "&photo_id=" + id - - # Sign the request - url = flickrsign(url, token) - - # Make the request - response = urllib2.urlopen(url) - - # Parse the XML - dom = xml.dom.minidom.parse(response) - - # Get the list of sizes - sizes = dom.getElementsByTagName("size") - - # Grab the original if it exists - if (sizes[-1].getAttribute("label") == "Original"): - imgurl = sizes[-1].getAttribute("source") - else: - print "Failed to get original for photo id " + id - - - # Free the DOM memory - dom.unlink() - - # Grab the image file - response = urllib2.urlopen(imgurl) - data = response.read() +def getphoto(imgurl, filename): + # Grab the image file + response = urllib2.urlopen(imgurl) + data = response.read() - # Save the file! - fh = open(filename, "w") - fh.write(data) - fh.close() + # Save the file! + fh = open(filename, "w") + fh.write(data) + fh.close() - return filename - except: - print "Failed to retrieve photo id " + id + return filename ######## Main Application ########## if __name__ == '__main__': @@ -244,7 +215,6 @@ if __name__ == '__main__': # Build the list of photos url = "http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos" url += "&photoset_id=" + pid - url += "&extras=date_taken" # Append to our list of urls urls.append( (url , dir) ) @@ -254,12 +224,10 @@ if __name__ == '__main__': # Add the photos which are not in any set url = "http://api.flickr.com/services/rest/?method=flickr.photos.getNotInSet" - url += "&extras=date_taken" urls.append( (url, "No Set") ) # Add the user's Favourites url = "http://api.flickr.com/services/rest/?method=flickr.favorites.getList" - url += "&extras=date_taken" urls.append( (url, "Favourites") ) # Time to get the photos @@ -275,6 +243,9 @@ if __name__ == '__main__': url += "&per_page=500" pages = page = 1 + # Get Date-Taken and Original-size URL for each result photo + url += "&extras=date_taken,url_o" + while page <= pages: request = url + "&page=" + str(page) @@ -315,7 +286,14 @@ if __name__ == '__main__': # woo, we have it already, use a hard-link os.link(inodes[photoid], target) else: - inodes[photoid] = getphoto(photo.getAttribute("id"), config["token"], target) + # Get URL to the "Original" size of the photo + imgurl = photo.getAttribute("url_o") + + # Grab image and save to local file + if imgurl: + inodes[photoid] = getphoto(imgurl, target) + else: + print "Failed to retrieve URL for photo id " + id # Move on the next page page = page + 1 -- 2.45.2