-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_twitter.py
66 lines (56 loc) · 1.53 KB
/
post_twitter.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
#!/usr/bin/python3
# vim: set fileencoding=utf-8 :
# require `pip3 install twitter`
# require `export PYTHONIOENCODING=utf-8`
import twitter
import sys
import os
import configparser
import codecs
import traceback
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DEBUG = True
CONSUMER_KEY = None
CONSUMER_SECRET = None
TOKEN = None
TOKEN_SECRET = None
def log(msg):
if DEBUG:
f = open(BASE_DIR + "/run.log","a")
f.write(str(msg) + "\n")
f.close()
def post(word):
auth = twitter.OAuth(
consumer_key=CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET,
token=TOKEN,
token_secret=TOKEN_SECRET)
t = twitter.Twitter(auth=auth)
word = word.replace('\\n',"\n")
log(word)
ret = t.statuses.update(status=word)
log(ret)
if __name__ == '__main__':
# Get Credential Key
try:
parser = configparser.ConfigParser()
parser.readfp(codecs.open(BASE_DIR + '/config.ini', "r", "utf8"))
CONSUMER_KEY = parser['twitter-key']['consumer_key']
CONSUMER_SECRET = parser['twitter-key']['consumer_secret']
TOKEN = parser['twitter-key']['token']
TOKEN_SECRET = parser['twitter-key']['token_secret']
except:
log("Failed while parsing config")
log(traceback.format_exc())
exit(-1)
# Create PostMessage from args
argvs = sys.argv
argc = len(argvs)
if argc < 2:
log("No Message")
exit(-1)
argvs.pop(0)
argw = ""
for argv in argvs:
argw = argw + " " + argv
post(argw)