Loading image from Python or cURL in Tumblr?

I am working on a project that involves loading an image in tumblr from Python. I was fortunate enough to use the Tumblr API ( http://www.tumblr.com/docs/en/api ) with regular text messages, but loading the images gave me problems. The error messages received by their servers were limited to the fact that I simply said that there was a "Photo upload error", which was less useful.

Since their API seems to be based on using standard HTTP POST operations, I know there should be a way to do this. Unfortunately, I did not achieve any success for a couple of days, and I decided to resort to bothering you guys about this.

I tried using curl and python libraries: httplib, urllib, urllib2 and a third-party library called urllib2_file ( http://fabien.seisen.org/python/urllib2_file/ ). I am upset that I was not able to get them to work, but I am ready to try other additional terminal applications that you can come up with.

Each method works fine with simple text messages, but each of them does not seem to correctly load the photos.

Here is my syntax for this with urllib2_file. Since urllib2 does not support the "multipart / form-data" methods for loading data, I use urllib2_file to add this functionality, but I could not get it to work. Tumblr api says their servers accept multipart / form-data, as well as a “regular message” method for uploading files. I would be happy if it worked.

import urllib, urllib2, urllib2_file

url = "http://www.tumblr.com/api/write"
values1 = {     'email':'EMAIL',
                'password':'PASSWORD',
                'type':'regular',
                'title':'Pythons urllib2',
                'body':'its pretty nice.  Not sure how to make it upload stuff yet, though. Still getting some "error uploading photo" errors...  So unhelpful.'}
values2 = {     'email':'EMAIL',
                'password':'PASSWORD',
                'type':'photo',
                'data': open('../data/media/pics/2009/05-14/100_1167.JPG'),
                'caption':'Caption'}
data = urllib.urlencode(values2)
print "just before defining the request"
req = urllib2.Request(url,data)
print "just before doing the urlopen."
#response = urllib2.urlopen(req)
try:
        response = urllib2.urlopen(req)
except urllib2.URLError, e:
        print e.code
        print e.read()
print "figure out how to handle .read() properly"
#the_page = response.read()
#print the_page
print "done"

It would be an ideal way if it had been working ever since using dictionaries to define fields is very simple, and I could make it much cleaner in the future.

Any troubleshooting tips that might go wrong will be appreciated. At the moment, I don’t know how to find out what could go wrong. I wish I had any attention for the RFC http.

, HTTP- .

!

+3
2
                 'data': open('../data/media/pics/2009/05-14/100_1167.JPG'),

, .. .read() there

+2

Tumblr API v2, Python. GitHub PyTumblr.

tumblr, teblr. : https://github.com/vijaykumarhackr/teblr/

0

Source: https://habr.com/ru/post/1755007/


All Articles