-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweet_status.py
70 lines (59 loc) · 1.86 KB
/
tweet_status.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
import requests
import json
from time import time, sleep
import tweepy
import credentials
status = "https://hub.57north.org.uk/api/status/current"
consumer_key = credentials.API_key
consumer_secret_key = credentials.API_secret_key
access_token = credentials.access_token
access_token_secret = credentials.access_token_secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret_key)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
def get_status():
# polls the spi
try:
r = requests.get(status)
except:
print("Couldn't speak to API")
res = json.loads(r.text)
return res['result']
def state_change(state, lastState):
# checks if the state of the space has changed
print ("s: " + str(state) + "ls: " + str(lastState))
if state == lastState:
return False
else:
return True
def send_tweet(status_track):
state_array = get_status()
state_bool = state_array['open']
print("st :" + str(status_track))
status_changed = state_change(state_bool, status_track)
print("sc: " + str(status_changed))
if status_changed == True:
status_track = state_bool
print("track: " + str(status_track))
if state_bool==True:
state = "opened"
else:
state = "closed"
if status_changed==True:
tweet = "Space " + state + " by " + state_array['trigger_person']+ " with message: " + state_array['message']
print(tweet)
try:
api.update_status(tweet)
except tweepy.TweepError as error:
if error.api_code == 187:
print("Already posted")
else:
print("Hey it tweeted OK")
else:
print("no change")
return status_track
if __name__=="__main__":
status_track = False
while True:
status_track = send_tweet(status_track)
sleep(30)