如何列出我的用户帐户的所有照片?

时间:2014-06-15 04:51:30

标签: python flickr

我正在尝试编写执行以下操作的脚本:

  • 从我的flickr帐户
  • 获取相册(photoset)ID列表
  • 将每张专辑(photoset)中的图像标题列入名为专辑标题的文本文件

这是我到目前为止所拥有的:

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

以上简单地将结果输出到屏幕:

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

从那里开始,我已经得到以下内容,我认为会列出上述相册中的所有图片标题:

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

不幸的是,它只是将索引超出范围索引错误。

2 个答案:

答案 0 :(得分:1)

我坚持使用它并从朋友手中拿出以下按计划运作的手:

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

答案 1 :(得分:0)

如果使用python-flickr-api,这很容易。复杂的部分是从flickr获得授权以访问私人信息。

以下是您可以使用的一些(未经测试的)代码:

import os

import flickr_api as flickr

# If all you want to do is get public information,
# then you need to set the api key and secret

flickr.set_keys(api_key='key', api_secret='sekret')

# If you want to fetch private/hidden information
# then in addition to the api key and secret,
# you also need to authorize your application.

# To do that, we request the authorization URL
# to get the value of `oauth_verifier`, which
# is what we need.

# This step is done only once, and we save
# the token. So naturally, we first check
# if the token exists or not:

if os.path.isfile('token.key'):
    flickr.set_auth_handler('token.key')
else:
    # This is the first time we are running,
    # so get the token and save it
    auth = flickr.auth.AuthHandler()
    url = auth.get_authorization_url('read')  # Get read permissions
    session_key = raw_input('''
                 Please visit {} and then copy the value of oauth_verifier:'''.format(url))

    if len(session_key.strip()):
        auth.set_verifier(session_key.strip())
        flickr.set_auth_handler(auth)

        # Save this token for next time
        auth.save('token.key')
    else:
        raise Exception("No authorization token provided, quitting.")

# If we reached this point, we are good to go!
# First thing we want to do is enable the cache, so
# we don't hit the API when not needed

flickr.enable_cache()

# Fetching a user, by their username

user = flickr.Person.findByUserName('username')

# Or, we don't know the username:

user = flickr.Person.findByEmail('some@user.com')

# Or, if we want to use the authenticated user

user = flickr.test.login()

# Next, fetch the photosets and their corresponding photos:

photo_sets = user.getPhotosets()
for pset in photo_sets:
    print("Getting pictures for {}".format(pset.title))
    photos = pset.getPhotos()
    for photo in photos:
        print('{}'.format(photo.info.title))

# Or, just get me _all_ the photos:

photos = user.getPhotos()

# If you haven't logged in,
# photos = user.getPublicPhotos()

for photo in photos:
    print('{}'.format(photo.info.title))