Skip to content

Commit

Permalink
Add methods to photo obj. and improve response
Browse files Browse the repository at this point in the history
Add the following methods:
* size: get different photo sizes available.
* originalSize: get the original photo size.
* geoData: get the location info available. If there’s no such info,
return a location object with None values in his attributes.

Also, the modification in parse_response allows to return an empty
location.
  • Loading branch information
Esteban Zapata Rojas committed May 2, 2014
1 parent 368d58c commit 034c975
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions flipy.py
Expand Up @@ -166,6 +166,22 @@ def info(self):
if self.secret: args['secret'] = self.secret
return self.flickr.photos.getInfo(**args)

def size(self):
'''get sizes of this photo'''
return self.flickr.photos.getSizes(photo_id=self.id)

def originalSize(self):
'''get the original size of this photo'''
return self.flickr.photos.getSizes(photo_id=self.id)[-1]

def geoData(self):
''' Returns the location info available for this photo.
If the photo has no info, returns a location object
with None values for each attribute '''
args = {'photo_id': self.id}
if self.secret: args['secret'] = self.secret
return self.flickr.photos.geo.getLocation(**args)

def people(self):
'''return all of the people in this photo'''
return self.flickr.photos.people.getList(photo_id = self.id)
Expand Down Expand Up @@ -287,12 +303,24 @@ def parse_response(self, string):
'''Parse a response string into objects'''
node = ET.fromstring(string)
if node.get('stat') != 'ok':
raise FlipyFlickrError(node)
children = node.getchildren()
if len(children) == 1:
return Response.get(self, children[0])
else:
return [Response.get(self, c) for c in children]
'''
If the error is about no existence of geolocation data, create a new response
Else, raise FlipyFlickrError.
'''
if node.getchildren()[0].tag == 'err': # If error tag exists
resp = node.getchildren()[0] # Obtain the error

if resp.get('code') == "2" and resp.get('msg') == 'Photo has no location information.':
geoEmpty = ET.fromstring('<location latitude="None" longitude="None" accuracy="None" />') # Create response
return Response.get(self, geoEmpty)
else:
raise FlipyFlickrError(node)
else:
children = node.getchildren()
if len(children) == 1:
return Response.get(self, children[0])
else:
return [Response.get(self, c) for c in children]


if __name__ == '__main__':
Expand All @@ -301,4 +329,4 @@ def parse_response(self, string):
me_info = flickr.people.getInfo(user_id=me.nsid)
print 'My name is %s. I have %s photos at %s.' % (
me_info.realname, me_info.photos.count, me_info.photosurl
)
)

0 comments on commit 034c975

Please sign in to comment.