forked from Bytespeicher/twitterstatus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitterstatus.py
executable file
·104 lines (88 loc) · 2.71 KB
/
twitterstatus.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
#!bin/python
import json
from twitter import Twitter, OAuth, TwitterHTTPError
from time import sleep
from sys import exit
from random import choice
try:
from config import *
except ImportError as e:
print("ERROR: Could not import config. Make sure config.py exists.")
try:
from wordlist import *
except ImportError:
WORDLIST = [
['The space'],
['is'],
['open'],
['closed'],
[''],
['']
]
try:
twitter = Twitter(auth=OAuth(OAUTH_TOKEN, OAUTH_SECRET,
CONSUMER_KEY, CONSUMER_SECRET))
except Exception as e:
print('Error in twitter init: ' + e)
exit(255)
def write_status(status):
status_file = open(STATUS_FILE, 'w+')
status_file.write(json.dumps({'status': status}))
def generate_phrase(open_status=True):
phrase = choice(WORDLIST[0]) + " "
phrase += choice(WORDLIST[1]) + " "
if open_status:
phrase += choice(WORDLIST[2]) + ". "
else:
phrase += choice(WORDLIST[3]) + ". "
if choice([True, False]):
if open_status:
phrase += choice(WORDLIST[4]).title() + "!"
else:
phrase += choice(WORDLIST[5]).title() + "!"
return phrase
def update(status):
try:
status_file = open(STATUS_FILE, 'r')
except IOError as e:
print('WARN: problem with status file, writing new one')
write_status(status)
return
try:
last_status = json.loads(status_file.read())
except Exception as e:
print('WARN: problem with status file, writing new one')
write_status(status)
return
if status == True and last_status['status'] == False:
text = generate_phrase(True)
elif status == False and last_status['status'] == True:
text = generate_phrase(False)
else:
return
try:
twitter.statuses.update(status=text)
except TwitterHTTPError as e:
print('Error while updating status: ' + e)
return
try:
twitter.direct_messages.new(user=ADMIN_NAME', text='Twitter Bot Startup')
except Exception as e:
print('Error sending direct message: ' + e)
while 1:
try:
status = json.loads(open(CURRENT_STATUS, 'r').read())
print('status: ' + str(status['state']['open']))
except FileNotFoundError as e:
print("Could not find or open status file '" + CURRENT_STATUS + "'")
exit(255)
except Exception as e:
try:
twitter.direct_messages.new(user=ADMIN_NAME, text='Twitter Bot Error')
except Exception:
pass
print('Error loading current status: ' + e)
else:
update(status=status['state']['open'])
write_status(status=status['state']['open'])
sleep(60)