Follow and download user's favorites photos on Flickr

From XPUB & Lens-Based wiki
#!/usr/bin/python
import flickrapi, random, urllib2

useragent = "Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101"

api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
flickr = flickrapi.FlickrAPI(api_key)

t_pool = ['0000000']

while True:
    t_id = random.choice(t_pool)
    t_username = flickr.people_getInfo(user_id=t_id)[0].find('username').text
    print '*** id: '+t_id, 'username: '+t_username+' ***'

    t_fav = flickr.favorites_getPublicList(user_id=t_id)

    t_pool = []

    for photo in t_fav[0]:
        p = flickr.photos_getInfo(photo_id=photo.attrib['id'])
        # http://farm{farm-id}.static.flickr.com/{server-id}/{id}_{secret}.jpg
        href = 'http://farm'+p[0].attrib['farm']+'.static.flickr.com/'+p[0].attrib['server']+'/'+p[0].attrib['id']+'_'+p[0].attrib['secret']+'.jpg'
        request = urllib2.Request(href, None, {'User-Agent': useragent})
        remotefile = urllib2.urlopen(request)
        print 'downloading ' + href
        localfile = open('dump/'+href.split('/')[-1], "wb")
        localfile.write(remotefile.read())
        localfile.close()

        t_pool.append(photo.attrib['owner'])