Created
July 15, 2013 16:03
-
-
Save eigenn/6001148 to your computer and use it in GitHub Desktop.
get back all the photos in a given photo set from Flickr.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from google.appengine.api import urlfetch | |
from bs4 import BeautifulSoup | |
class FlickrPhoto(object): | |
""" | |
Construct the base url for flickr photo. | |
""" | |
base_url = 'http://farm%s.staticflickr.com/%s/%s_%s_%s.jpg' | |
def __init__(self, farm, server, photo_id, secret, resolution): | |
self.farm = farm | |
self.server = server | |
self.photo_id = photo_id | |
self.secret = secret | |
self.resolution = resolution | |
def get_url(self): | |
return self.base_url % (self.farm, | |
self.server, | |
self.photo_id, | |
self.secret, | |
self.resolution) | |
class FlickrPhotoSet(object): | |
""" | |
get back all the images in the given flickr set. | |
""" | |
base_url = 'http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=%s&photoset_id=%s&format=rest' | |
def __init__(self, api_key, photoset_id, photo_size='c'): | |
self.api_key = api_key | |
self.photoset_id = photoset_id | |
self.photo_size = photo_size | |
def get_set(self): | |
content = urlfetch.fetch(self.base_url % (self.api_key, self.photoset_id)) | |
return content.content | |
def __iter__(self): | |
photoset = self.get_set() | |
soup = BeautifulSoup(photoset) | |
for photo in soup.find_all('photo'): | |
yield FlickrPhoto(str(photo.get('farm')), | |
str(photo.get('server')), | |
str(photo.get('id')), | |
str(photo.get('secret')), | |
self.photo_size).get_url() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment