Follow and download user's favorites photos on Flickr: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "<code lang="python"> #!/usr/bin/python import flickrapi, random, urllib2 useragent = "Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101" api_key = 'xxxxxxxxxxxxxx...")
 
No edit summary
Line 1: Line 1:
<code lang="python">
<source lang="python">
#!/usr/bin/python
#!/usr/bin/python
import flickrapi, random, urllib2
import flickrapi, random, urllib2
Line 33: Line 33:
         t_pool.append(photo.attrib['owner'])
         t_pool.append(photo.attrib['owner'])


</code>
</source>

Revision as of 18:27, 14 January 2011

#!/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_id = photo.attrib['id']
        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'])