-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.py
39 lines (35 loc) · 1.44 KB
/
post.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
#!coding:utf-8
import configparser
from requests_oauthlib import OAuth1Session
class Post:
def __init__(self):
conf = configparser.ConfigParser()
conf.read('./config.ini')
self.oauth_consumer_key = conf.get('hatena', 'oauth_consumer_key')
self.oauth_consumer_secret = conf.get('hatena', 'oauth_consumer_secret')
self.blog_api_key = conf.get('hatena', 'api_key')
self.rootendpoint = conf.get('hatena', 'rootendpoint')
self.access_token = conf.get('hatena', 'access_token')
self.access_token_secret = conf.get('hatena', 'access_token_secret')
def send(self, title, body, updated, blog_id=None):
xmlbody = '<?xml version="1.0" encoding="utf-8"?>\
<entry xmlns="http://www.w3.org/2005/Atom"\
xmlns:app="http://www.w3.org/2007/app">\
<title>%s</title>\
<author><name>name</name></author>\
<content type="text/plain">\
%s \
</content>\
<updated>%s</updated>\
<app:control>\
<app:draft>yes</app:draft>\
</app:control>\
</entry>' % (title, body, updated.strftime("%Y-%m-%dT%H:%M:%S"))
auth = OAuth1Session(self.oauth_consumer_key, self.oauth_consumer_secret, self.access_token, self.access_token_secret)
if blog_id:
auth.put(self.rootendpoint + '/entry/' + blog_id, data=xmlbody.encode('utf-8'))
else:
auth.post(self.rootendpoint + '/entry', data=xmlbody.encode('utf-8'))
if __name__ == '__main__':
post = Post()
post.new('tes', 'tesss')