Search code examples
pythonflickr

How do I list all photos for my user account?


I'm trying to write a script that does the following:

  • obtains a list of album (photoset) ID's from my flickr account
  • list the image titles from each album (photoset) into a text file named as the album title

Here's what I have so far:

import flickrapi

from xml.etree import ElementTree

api_key = 'xxxx'
api_secret = 'xxxx'

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))

sets = flickr.photosets_getList(user_id='xxxx')

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

The above simply outputs the result to the screen like this:

id: 12453463463252553 setname: 2006-08 photos: 371
id: 23523523523532523 setname: 2006-07 photos: 507
id: 53253253253255532 setname: 2006-06 photos: 20
... etc ...

From there, I've got the following which I assumed would list all the image titles in the above album:

import flickrapi

from xml.etree import ElementTree

api_key = 'xxxx'
api_secret = 'xxxx'

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))

photos = flickr.photosets_getPhotos(photoset_id='12453463463252553')

for elm in photos.getchildren()[0]:
    title = elm.getchildren()[0].text
    print ("%s") %(elm.get('title'))

Unfortunately it just spits out a index out of range index error.


Solution

  • I stuck with it and had a hand from a friend to come up with the following which works as planned:

    import flickrapi
    import os
    from xml.etree import ElementTree
    
    api_key = 'xxxx'
    api_secret = 'xxxx'
    
    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))
    
    
    sets = flickr.photosets_getList(user_id='xxxx')
    
    for set in sets.getchildren()[0]:
        title = set.getchildren()[0].text
    
        filename = "%s.txt" % (title)
        f = open(filename,'w')
    
        print ("Getting Photos from set: %s") % (title)
    
        for photo in flickr.walk_set(set.get('id')):
                    f.write("%s" % (photo.get('title')))
    
    
        f.close()