Search code examples
pythonflickr

How to parse Flickr using flickrapi in Python


I'm trying to create a simple app that lists recent photos posted to Flickr based on geography.

I've created the query using flickerapi, but struggling with the API notes as to how to actually return the results so I can actually parse the attributes I actually want.

This is my query:

import flickrapi
api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
flickr = flickrapi.FlickrAPI(api_key, format="etree")

flickr.photos_search(api_key = api_key, tags = "stoke", privacy_filter = 1, safe_search=1, lon="-2.1974224", lat="53.0232691", radius=32, sort= "date-posted-des")

It returns an object:

<Element 'rsp' at 0x1077a6b10>

All I want to do is examine what attributes are available so I can extract the bits I want - but I can't see a method which will return this. What can I try next?


Solution

  • In your case what you might want is:

     "flickr = flickrapi.FlickrAPI(api_key) 
      photos = flickr.photos_search(user_id='73509078@N00', per_page='10') 
      sets = flickr.photosets_getList(user_id='73509078@N00')"
    

    - flickrapi docs

    So what it does is gets the returned XML doc and gives to you as an ElementTree object so it's easier to handle. (this is the sets object). the photos object cannot do that unfortunately.

    ElementTree docs

    so to get a general list of attributes first use the .tag and .attrib methods of the root node of the tree that is passed to you.

    You can use sets as the root in the examples in the ElementTree docs :)

    an example use it gives is:

    sets = flickr.photosets_getList(user_id='73509078@N00')
    
    sets.attrib['stat'] => 'ok'
    sets.find('photosets').attrib['cancreate'] => '1'
    
    set0 = sets.find('photosets').findall('photoset')[0]
    
    +-------------------------------+-----------+
    | variable                      | value     |
    +-------------------------------+-----------+
    | set0.attrib['id']             | u'5'      |
    | set0.attrib['primary']        | u'2483'   |
    | set0.attrib['secret']         | u'abcdef' |
    | set0.attrib['server']         | u'8'      |
    | set0.attrib['photos']         | u'4'      |
    | set0.title[0].text            | u'Test'   |
    | set0.description[0].text      | u'foo'    |
    | set0.find('title').text       | 'Test'    |
    | set0.find('description').text | 'foo'     |
    +-------------------------------+-----------+
    
    ... and similar for set1 ...
    

    -flickrapi docs


    Another question you may have been indirectly asking:

    In general given a python class you can do:

    cls.__dict__
    

    to get some of the attributes available to it.

    Given a general python object you can use vars(obj) or dir(obj)

    e.g.:

    class meh():
        def __init__(self):
            self.cat = 'dinosaur'
            self.number = 1
        # some example methods - don't actually do this
        # this is not a good use of a method
        # or object-oriented programming in general
    
        def add_number(self, i):
            self.number+=i
    
    j = meh()
    print j.__dict__
    {'number': 1, 'cat': 'dinosaur'}
    

    this returns the namespace's dict that is used for the object:

    "Except for one thing. Module objects have a secret read-only attribute called dict which returns the dictionary used to implement the module’s namespace; the name dict is an attribute but not a global name. Obviously, using this violates the abstraction of namespace implementation, and should be restricted to things like post-mortem debuggers." - Python Docs

    dir returns

    "Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object." docs

    and

    vars just returns the dict attribute:

    "return the dict attribute for a module, class, instance, or any other object with a dict attribute.

    Objects such as modules and instances have an updateable dict attribute; however, other objects may have write restrictions on their dict attributes (for example, new-style classes use a dictproxy to prevent direct dictionary updates)." docs

    it should be noted that nothing can give you everything available to an object at run time due to crafty things you can do to modify an object.