-
Notifications
You must be signed in to change notification settings - Fork 123
/
tumble.py
executable file
·167 lines (148 loc) · 5.47 KB
/
tumble.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
"""Read a feed from stdin and post its entries to tumblr.
Options:
-b sub-blog Post to a sub-blog of your account.
-c cred-file The name of the credentials file.
-e post-id Edit the existing post with the given ID.
This only looks at the first entry of the feed.
-d Debug mode: print the raw post data instead
of posting it to tumblr.com.
"""
"""Authorization is handled via OAuth. Prepare the file ~/.config/tumblr
with 5 lines:
- your default blog name
- the consumer key
- the consumer secret
- the access token
- the access secret
You get these values by registering for a new application on the tumblr
developer site and running the included oauth.py with the consumer key and
secret as arguments to get an access token and secret.
Non-standard Python dependencies:
- simplejson (http://pypi.python.org/pypi/simplejson/; for Python <= 2.6)
- oauth2 (http://pypi.python.org/pypi/oauth2/)
- httplib2 (http://pypi.python.org/pypi/httplib2/)
"""
import sys
import os
import getopt
from urllib.parse import urlencode
from datetime import datetime
from calendar import timegm
import json
import oauth2 as oauth
import feedparser
URL_FMT = 'http://api.tumblr.com/v2/blog/%s/post'
CONFIG = '~/.config/tumblr'
class Tumble:
def __init__(self):
self.blog = self.consumer_token = self.consumer_secret = \
self.access_token = self.access_secret = None
self.post_id = None
self.debug = False
def set_credentials(self, cred_file):
(
self.blog,
self.consumer_token, self.consumer_secret,
self.access_token, self.access_secret
) = (s.strip() for s in open(cred_file))
def tumble(self, feed):
feed = feedparser.parse(feed)
if self.post_id:
return [self.post(feed.entries[0])]
else:
return [self.post(e) for e in feed.entries]
def post(self, entry):
# the first enclosure determines the media type
enc = entry.get('enclosures', [])
if enc:
enc = enc[0]
if enc and enc.type.startswith('image/'):
data = {
'type': 'photo', 'source': enc.href,
'caption': entry.title, 'link': entry.link
}
elif enc and enc.type.startswith('audio/'):
data = {
'type': 'audio', 'caption': entry.title, 'external-url': enc.href
}
elif 'link' in entry and entry.link:
data = {'type': 'link', 'url': entry.link, 'title': entry.title}
if 'content' in entry:
data['description'] = entry.content[0].value
elif 'summary' in entry:
data['description'] = entry.summary
elif 'content' in entry:
data = {'type': 'text', 'title': entry.title, 'body': entry.content[0].value}
elif 'summary' in entry:
data = {'type': 'text', 'title': entry.title, 'body': entry.summary}
else:
return 'unknown', entry
if 'tags' in entry:
data['tags'] = ','.join('"%s"' % t.term for t in entry.tags)
for d in ('published_parsed', 'updated_parsed'):
if d in entry:
pub = datetime.fromtimestamp(timegm(entry.get(d)))
data['date'] = pub.isoformat(' ')
break
if '.' not in self.blog:
self.blog += '.tumblr.com'
url = URL_FMT % self.blog
if self.post_id:
data['id'] = self.post_id
op = 'edit'
url += '/edit'
else:
op = 'post'
if self.debug:
return dict(url=url, entry=entry, data=data)
for k in data:
if type(data[k]) is str:
data[k] = data[k].encode('utf-8')
# do the OAuth thing
consumer = oauth.Consumer(self.consumer_token, self.consumer_secret)
token = oauth.Token(self.access_token, self.access_secret)
client = oauth.Client(consumer, token)
try:
headers, resp = client.request(url, method='POST', body=urlencode(data))
resp = json.loads(resp)
except ValueError as e:
return 'error', 'json', resp
except EnvironmentError as e:
return 'error', str(e)
if resp['meta']['status'] in (200, 201):
return op, str(resp['response']['id'])
else:
return 'error', headers, resp
if __name__ == '__main__':
t = Tumble()
try:
opts, args = getopt.getopt(sys.argv[1:], 'hb:c:e:d')
except getopt.GetoptError:
print("Usage: %s [-b blog-name] [-c cred-file] [-e post-id] [-d]" %
sys.argv[0].split(os.sep)[-1])
sys.exit(1)
for o, v in opts:
if o == '-h':
print(__doc__.strip())
sys.exit(0)
if o == '-b':
t.blog = v
elif o == '-c':
CONFIG = v
elif o == '-e':
t.post_id = v
elif o == '-d':
t.debug = True
try:
t.set_credentials(os.path.expanduser(CONFIG))
except EnvironmentError:
sys.stderr.write('Credentials file %s not found or not readable\n' % CONFIG)
sys.exit(1)
result = t.tumble(sys.stdin.buffer) # read stdin in binary mode
if result:
import pprint
pprint.pprint(result)
if not t.debug and 'error' in [r[0] for r in result]:
sys.exit(2)
sys.exit(0)