Search code examples
pythonapiflickr

flickAPI: How do I list the sets for my user account?


I wish to list all the sets created by this user along with the set id. I am using flickrApi and Python. Here's my code. The setName is returning "none" in the output. The elm.get('title') is returning "none".

    import flickrapi

api_key = 'APIKEY'
api_secret = 'APISECRET'

flickr = flickrapi.FlickrAPI(api_key, api_secret)

(token, frob) = flickr.get_token_part_one(perms='write')
if not token: raw_input("Press ENTER after you authorized this program")
flickr.get_token_part_two((token, frob))
try:
    favs = flickr.photosets_getList(user_id='51784048@N00')
    #favs = flickr.favorites_getPublicList(user_id = '51784048@N00')
    for elm in favs.getiterator():
        print ("id: %s secret: %s setname: %s") %(elm.get('id'), elm.get('secret'), elm.get('title')) 
except:
    raise Exception("Some error encountered!")

Solution

  • When you're working with flickrapi in Python, it's frequently useful to print the XML which is returned by the API calls in order to see what's going on. In your example, if you add:

     from xml.etree import ElementTree
    

    ... at the top, and then add:

    print ElementTree.tostring(favs)
    

    ... you'll see that the structure returned is:

    <rsp stat="ok">
    <photosets page="1" pages="1" perpage="80" total="80">
        <photoset .../>
        <photoset .../>
        <photoset .../>
        <photoset ...>
    </photosets>
    </rsp>
    

    In a real application, you'd want to check the return status and check the page attributes, but to quickly get to what you want, let's just look at the <photoset> elements. To iterate over them, you can change your loop to:

    for elm in favs.getchildren()[0]:
        print ElementTree.tostring(elm)
    

    Then you'll see the structure of each photoset element that you'll have to navigate. For example, one would be:

    <photoset can_comment="1"
              count_comments="0"
              count_views="34"
              date_create="1156703089"
              date_update="1297462539"
              farm="1"
              id="72157594253605858"
              needs_interstitial="0"
              photos="73" primary="226222345"
              secret="63fde66413"
              server="62"
              videos="0"
              visibility_can_see_set="1">
        <title>Birds</title>
        <description />
    </photoset>
    

    ... so the title is actually stored in a title sub-element. From that, you can see that to get the information that you want, you can do:

    for elm in favs.getchildren()[0]:
        title = elm.getchildren()[0].text
        print ("id: %s secret: %s setname: %s") %(elm.get('id'), elm.get('secret'), title) 
    

    ... which produces the output:

    id: 72157600139832705 secret: 4e884f3523 setname: French Creek State Park
    id: 72157600047937451 secret: d3c84ed8df setname: Las Vegas
    id: 72157594253605858 secret: 63fde66413 setname: Birds
    

    etc.