【问题标题】:Python: Upload a photo to photobucketPython:将照片上传到 photobucket
【发布时间】:2011-04-02 21:35:28
【问题描述】:

Python 脚本能否将照片上传到照片存储桶,然后检索其 URL?是这样吗?

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

但我发现这令人困惑。

非常感谢,

菲尔

【问题讨论】:

    标签: python photobucket


    【解决方案1】:

    是的,你可以。 Photobucket 有一个有据可查的API,有人在它周围写了一个wrapper

    下载它并将其放入您的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)
    

    【讨论】:

    • 嘿,谢谢你的回答,只是遇到了一个小问题。我无法导入 pbapi 我得到“没有名为 pbapi 的模块”。我在这个文件夹中复制了文件photobucket-api-py.googlecode.com/svn/trunk/PbApi/pbapi 并将我的python 路径附加到这个文件夹中。这是安装 pbapi 的正确方法吗?
    • 最好将文件夹 pbapi 和 oauth 复制到 C:\PythonXX\Lib\site-packages\
    • 啊,我在 Fedora 而不是 windows 我的坏
    • /usr/lib/pythonXX/site-packages/
    【解决方案2】:

    使用为此编写的python API by Ron White

    【讨论】:

    • 这是我见过的最糟糕的 API 包装器,但我终于让它工作了,看看我的回复。
    • 对不起,不是我自己写的,但很高兴它有帮助。编写自己的代码可能会花费超过 5 个小时的调试时间。考虑将您的 cmets 或指向此票证的链接提供给所有者
    • 一旦理解了就很好了,但是一点帮助都没有,用起来很郁闷。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    • 2013-07-15
    • 2012-08-19
    • 2019-04-18
    • 2012-05-26
    相关资源
    最近更新 更多