2

Pythonスクリプトで写真をフォトバケットにアップロードしてから、そのURLを取得できますか?どうですか?

このリンクでスクリプトを見つけました:http ://www.democraticunderground.com/discuss/duboard.php?az = view_all&address = 240x677

しかし、私はそれが混乱していることに気づきました。

どうもありがとう、

フィル

4

2 に答える 2

8

はい、できます。Photobucketには十分に文書化されたAPIがあり、誰かがその周りにラッパーを作成しました。

それをダウンロードしてPythonパスに入れ、httplib2をダウンロードします(これにはeasy_installまたはpipを使用できます)。

次に、PhotobucketAPIのキーをリクエストする必要があります。

すべてが正しく行われた場合は、今すぐスクリプトを作成できます。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)
于 2010-08-24T03:06:08.647 に答える
0

これを行うために作成されたRonWhiteによるPythonAPIを使用します

于 2010-08-23T23:20:34.730 に答える