Python脚本可以将照片上传到照片存储桶,然后检索它的URL吗?如此如此?Python:将照片上传到photobucket
我发现了一个脚本,在此链接:http://www.democraticunderground.com/discuss/duboard.php?az=view_all&address=240x677
但我只是发现混乱。
千恩万谢,
菲尔
Python脚本可以将照片上传到照片存储桶,然后检索它的URL吗?如此如此?Python:将照片上传到photobucket
我发现了一个脚本,在此链接:http://www.democraticunderground.com/discuss/duboard.php?az=view_all&address=240x677
但我只是发现混乱。
千恩万谢,
菲尔
是的,你可以在python API by Ron White。 Photobucket有一个很好的文档API,并且有人在它周围写了一个wrapper。
下载它并把它放到你的Python路径中,然后下载httplib2(你可以使用easy_install或pip来做这个)。
然后,您必须请求Photobucket API的密钥。
如果您所做的一切都正确,现在可以编写脚本。 Python包装很棒,但没有记录在文件中,这使得它很难使用它。我花了数小时的时间了解它(比较问题和响应时间)。例如,脚本甚至具有form/multipart支持,但我必须阅读代码以了解如何使用它。我必须在文件名前添加@
。
这个库是一个很好的例子,你不应该记录你的代码!
我终于得到了它的工作,享受脚本:(!它甚至有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)
使用写做只是这
这是我见过的最糟糕的API包装,但我终于得到它的工作,看到我的反应。 – leoluk 2010-08-24 03:08:58
对不起,没有自己写,但很高兴它帮助。它可能会花费超过你花5个小时来调试它来编写你自己的。考虑将你的意见或链接发送给业主 – JiminyCricket 2010-08-24 12:31:08
一旦你了解它,这很棒,但根本没有任何帮助,所以使用它非常令人沮丧。 – leoluk 2010-08-24 14:18:01
嘿,thx的答案只是有一个小问题。我不能导入pbapi我得到“没有模块名为pbapi”。我复制了这个文件夹中的文件http://photobucket-api-py.googlecode.com/svn/trunk/PbApi/pbapi/,并将我的Python路径附加到这个文件夹。这是安装pbapi的正确方法吗? – Phil 2010-08-24 18:59:17
更好的办法是将文件夹pbapi和oauth复制到C:\ PythonXX \ Lib \ site-packages \ – leoluk 2010-08-24 19:32:24
啊,在Fedora上im不是我的坏我的不好 – Phil 2010-08-24 19:36:42