Python: 上传照片到photobucket

2 投票
2 回答
1452 浏览
提问于 2025-04-16 03:12

一个Python脚本能否上传照片到照片存储桶,然后获取这个照片的链接?如果可以的话,怎么做呢?

我在这个链接找到了一个脚本:http://www.democraticunderground.com/discuss/duboard.php?az=view_all&address=240x677

但是我觉得这个有点让人困惑。

非常感谢,

Phil

2 个回答

0

使用Ron White写的Python接口,这个接口就是为了实现这个功能而创建的。

8

当然可以。Photobucket 有一个详细的 API,而且有人为它写了一个 封装库

你可以下载这个封装库,然后把它放到你的 Python 路径里。接着,你还需要下载 httplib2(可以用 easy_install 或 pip 来安装)。

然后,你需要申请一个 Photobucket API 的密钥。

如果你都做对了,现在就可以写你的脚本了。这个 Python 封装库很不错,但完全没有文档,这让使用起来非常困难。我花了好几个小时才弄明白它(可以看看这里的问题和回答的时间对比)。举个例子,这个脚本甚至支持表单/多部分上传,但我得看代码才能知道怎么用。我还得在文件名之前加个 @

这个库真是个反面教材,告诉你怎么不应该写文档!

我终于让它工作了,享受这个脚本吧:(它甚至支持 oAuth 处理!)

import pbapi

import webbrowser
import cPickle
import os
import re
import sys
from xml.etree import ElementTree

__author__ = "leoluk"

###############################################
##               CONFIGURATION               ##
###############################################

# File in which the oAuth token will be stored
TOKEN_FILE = "token.txt"

IMAGE_PATH = r"D:\Eigene Dateien\Bilder\SC\foo.png"

IMAGE_RECORD = {
    "type": 'image',
    "uploadfile": '@'+IMAGE_PATH,

    "title": "My title", # <---
    "description": "My description", # <---
}

ALBUM_NAME = None # default album if None


API_KEY = "149[..]"
API_SECRET = "528[...]"


###############################################
##                   SCRIPT                  ##
###############################################

api = pbapi.PbApi(API_KEY, API_SECRET)

api.pb_request.connection.cache = None

# Test if service online
api.reset().ping().post()

result = api.reset().ping().post().response_string

ET = ElementTree.fromstring(result)

if ET.find('status').text != 'OK':
    sys.stderr.write("error: Ping failed \n"+result)
    sys.exit(-1)

try:
    # If there is already a saved oAuth token, no need for a new one
    api.username, api.pb_request.oauth_token = cPickle.load(open(TOKEN_FILE))
except (ValueError, KeyError, IOError, TypeError):
    # If error, there's no valid oAuth token

    # Getting request token
    api.reset().login().request().post().load_token_from_response()

    # Requesting user permission (you have to login with your account)
    webbrowser.open_new_tab(api.login_url)

    raw_input("Press Enter when you finished access permission. ")

    #Getting oAuth token
    api.reset().login().access().post().load_token_from_response()


# This is needed for getting the right subdomain
infos = api.reset().album(api.username).url().get().response_string

ET = ElementTree.fromstring(infos)

if ET.find('status').text != 'OK':
    # Remove the invalid oAuth
    os.remove(TOKEN_FILE)
    # This happend is user deletes the oAuth permission online
    sys.stderr.write("error: Permission deleted. Please re-run.")
    sys.exit(-1)

# Fresh values for username and subdomain
api.username = ET.find('content/username').text
api.set_subdomain(ET.find('content/subdomain/api').text)

# Default album name
if not ALBUM_NAME:
    ALBUM_NAME = api.username

# Debug :-)
print "User: %s" % api.username

# Save the new, valid oAuth token
cPickle.dump((api.username, api.oauth_token), open(TOKEN_FILE, 'w'))

# Posting the image
result = (api.reset().album(ALBUM_NAME).
          upload(IMAGE_RECORD).post().response_string)

ET = ElementTree.fromstring(result)

if ET.find('status').text != 'OK':
    sys.stderr.write("error: File upload failed \n"+result)
    sys.exit(-1)


# Now, as an example what you could do now, open the image in the browser

webbrowser.open_new_tab(ET.find('content/browseurl').text)

撰写回答