forked from wangxiaomo/SmartWall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Poster.py
61 lines (52 loc) · 1.63 KB
/
Poster.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
#-*- coding: utf-8 -*-
"""
Poster: 发布微博。
"""
import sys
sys.path.insert(0, "./lib")
import pickle
import config
from lib.weibopy.auth import OAuthHandler
from lib.weibopy.api import API
from Helper import log
class Poster():
def __init__(self):
self.hdl = OAuthHandler(config.APP_KEY, config.APP_SECRET)
self.api = None
self.token = {}
try:
with open(config.TOKEN_FILE) as f:
self.token = pickle.load(f)
log("token init success!")
log("Access Token is: " + str(self.token))
self.hdl.setToken(
self.token["key"],
self.token["secret"]
)
self.api = API(self.hdl)
except:
log("Haven't Authorizationed!")
print "Authorization URL: %s" % self.get_auth_url()
pin = raw_input("PIN: ")
self.auth(pin)
def get_auth_url(self):
return self.hdl.get_authorization_url()
def auth(self, pin):
try:
token = self.hdl.get_access_token(pin)
self.api = API(self.hdl)
except:
raise Exception("OAuth Failed!")
self.token.update(dict(key=token.key, secret=token.secret))
log("Access Token is: " + str(self.token))
with open(config.TOKEN_FILE, "w") as f:
pickle.dump(self.token, f)
log("pickle saved success!")
def post(self, status):
try:
self.api.update_status(status=status)
except:
raise Exception("Post Status Failed!")
if __name__ == '__main__':
poster = Poster()
poster.post("This is a Test!")