views:

104

answers:

2

Hi there,

Can a Python script upload a photo to photo bucket and then retrieve the URL for it? Is so how?

I did find one script www.democraticunderground.com/discuss/duboard.php?az=view_all address=240x677

But i just found that confusing.

many thanks,

Phil

A: 

Use the python API by Ron White that was written to do just this

JiminyCricket
It is the worst API wrapper I've ever seen, but I finally got it working, see my response.
leoluk
sorry, didnt write it myself, but glad it helped. it probably would have taken more than the 5 hours you spent debugging it to write your own. consider including your comments or a link to this ticket to the owners
JiminyCricket
Once you understand it, it's great, but there's no help at all and so it's pretty frustrating to use it.
leoluk
+3  A: 

Yes, you can. Photobucket has a well-documented API, and someone wrote a wrapper around it.

Download the it and puth it into your Python path, then download httplib2 (you can use easy_install or pip for this one).

Then, you have to request a key for the Photobucket API.

If you did everything right, you can write your script now. The Python wrapper is great, but is not documented at all which makes it very difficult to use it. I spent hours on understanding it (compare the question and response time here). As example, the script even has form/multipart support, but I had to read the code to find out how to use it. I had to prefix the filename with a @.

This library is a great example how you should NOT document your code!

I finally got it working, enjoy the script: (it even has oAuth handling!)

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)
leoluk
Hey, thx for the answer just got one little problem. I cant import pbapi i get the "no module named pbapi". I copied the files http://photobucket-api-py.googlecode.com/svn/trunk/PbApi/pbapi/ in this folder and appended my python path to this folder. Is that the correct way of installing pbapi?
Phil
Better is to copy the folders pbapi and oauth to C:\PythonXX\Lib\site-packages\
leoluk
ah, im on Fedora not windows my bad
Phil
/usr/lib/pythonXX/site-packages/
leoluk
Your a legend :)
Phil
I updated the code and fixed a problem with the token
leoluk