-
Notifications
You must be signed in to change notification settings - Fork 0
/
photo2tumblr.py
executable file
·60 lines (47 loc) · 2.09 KB
/
photo2tumblr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
# TumblrAPIv2 - https://gist.github.com/velocityzen/1242662
import glob
import json
import os
import time
import urllib2
import urlparse
import oauth2
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
class APIError(StandardError):
def __init__(self, msg, response=None):
StandardError.__init__(self, msg)
class TumblrAPIv2:
def __init__(self, consumer_key, consumer_secret, oauth_token, oauth_token_secret):
self.consumer = oauth2.Consumer(consumer_key, consumer_secret)
self.token = oauth2.Token(oauth_token, oauth_token_secret)
self.url = "http://api.tumblr.com"
def parse_response(self, result):
content = json.loads(result)
if 400 <= int(content["meta"]["status"]) <= 600:
raise APIError(content["meta"]["msg"], result)
return content["response"]
def createPhotoPost(self, id, post):
url = self.url + "/v2/blog/%s/post" %id
img_file = post['data']
del(post['data'])
req = oauth2.Request.from_consumer_and_token(self.consumer,
token=self.token,
http_method="POST",
http_url=url,
parameters=post)
req.sign_request(oauth2.SignatureMethod_HMAC_SHA1(), self.consumer, self.token)
compiled_postdata = req.to_postdata()
all_upload_params = urlparse.parse_qs(compiled_postdata, keep_blank_values=True)
for key, val in all_upload_params.iteritems():
all_upload_params[key] = val[0]
all_upload_params['data'] = open(img_file, 'rb')
datagen, headers = multipart_encode(all_upload_params)
request = urllib2.Request(url, datagen, headers)
try:
respdata = urllib2.urlopen(request).read()
except urllib2.HTTPError, ex:
return 'Received error : %s ( code: %s )' % (ex.msg, ex.code)
return self.parse_response(respdata)
register_openers()