Announcement

#1 2011-06-22 15:42:03

sbyke
Member
2011-02-26
13

Python http uploader

I'm attempting to get digikam -> piwigo syncing working.  I have a python program to identify which subset of the digikam photos I want to publish and figure out which of the published need re-syncing.

I've not been able to get a python equivalent to the perl example for pwg.images.addSimple working.

I'm able to auth, get a cookie, but when I do a second post to upload a file (the first post is for auth) I get "Unknown request format".

The source for my test case is at http://broadley.org/bill/piwiup.py it takes the URL for your ws.php script, username, and password.  It's currently hard coded to upload DSC_1810.jpg from your current directory but that can easily be changed.

Anyone have similar working?

Offline

 

#2 2011-06-22 23:04:50

plg
Piwigo Team
Nantes, France, Europe
2002-04-05
13791

Re: Python http uploader

I'm really a newbee on Python, but when I looked at your code, I wondered if the way you try to define the "image" field in the form is correct.

I've found some tutorials on how to upload file through HTTP with Python like on Http client to POST using multipart/form-data (Python recipe) and it looks much more complicated than your piece of code.

Offline

 

#3 2011-06-23 03:18:18

sbyke
Member
2011-02-26
13

Re: Python http uploader

Ah, turns it's a problem with all python versions up to and including 2.7.  Basically a nice/clean solution for multipart form data encoding didn't make it in time for the python 2.7 release.  More details at http://bugs.python.org/issue3244

So for anyone in need with python  2.7 or older here is an example.  Download the source from http://peerit.blogspot.com/2007/07/mult … k-for.html and save it at as MultipartPostHandler.py

Then save this as a piwigo-post-example.py.  Hopefully folks don't mind 30 ish lines of code here:
#!/usr/bin/python

import MultipartPostHandler, urllib2, cookielib
import sys

(foo, url, user, password) = sys.argv

# setup cookie jar to store session cookie
cookies = cookielib.CookieJar()

#setup login
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies),
                                MultipartPostHandler.MultipartPostHandler)
params = { "username" : user,  "password" : password,
            "method":"pwg.session.login"}
#actually login
response = opener.open(url, params)

# when working it should look like rsp stat="ok"
print response.read()
# when working should look like Cookie pwg_id=<big hex number> for URL
print "cj=",cookies

#setup image post
params = { "method":"pwg.images.addSimple",
            "image" :open("D.jpg", "rb"),
            "category":"1",
            "tags": "testTag, Testing" }
#post image
response = opener.open(url, params)
#print response, should look like rsp stat="ok" <image_id>288</image_id><url>http://...</url>
print response.read()

Then chmod +x ./piwigo-post-example.py
./piwigo-post-example.py http://mysite.com/MyPhotos/ws.php joeuser password

Works for me, now to finish the rest of the command line sync functionality.  Thanks for the help.

Offline

 

#4 2011-06-23 09:29:55

plg
Piwigo Team
Nantes, France, Europe
2002-04-05
13791

Re: Python http uploader

I confirm it worked for me too.

Offline

 

Board footer

Powered by FluxBB

github twitter newsletter Donate Piwigo.org © 2002-2024 · Contact