-
Notifications
You must be signed in to change notification settings - Fork 0
/
oxford_indies_lunch.py
230 lines (161 loc) · 6.6 KB
/
oxford_indies_lunch.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import twitter
import urlparse
import oauth2 as oauth
import time
from twython import Twython
import re
import app_keys as keys
import os.path
import person_manager
import restaurant_manager
from lunch_setup import runSetup
saved_tokens_path = "saved_tokens.txt"
consumer_key = keys.consumer_key
consumer_secret = keys.consumer_secret
def firstTimeLogin():
print("Performing first time login (interactive)")
oauthRequestUrl = "https://api.twitter.com/oauth/request_token"
twitter = Twython(consumer_key, consumer_secret)
auth = twitter.get_authentication_tokens(callback_url='oob')
OAUTH_TOKEN = auth['oauth_token']
OAUTH_TOKEN_SECRET = auth['oauth_token_secret']
print("Go to the following URL and authorise the app (Kim and Toms app)")
print(auth['auth_url'])
print("Once done, enter the PIN")
oauth_verifier = ""
while oauth_verifier == "":
oauth_verifier = raw_input('What is the PIN? ')
twitter = Twython(consumer_key, consumer_secret, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
final_step = twitter.get_authorized_tokens(oauth_verifier)
OAUTH_TOKEN = final_step['oauth_token']
OAUTH_TOKEN_SECRET = final_step['oauth_token_secret']
print("Saving tokens")
with open(saved_tokens_path, "w") as f:
print("Token: %s Secret: %s", OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
f.write(OAUTH_TOKEN + "\n" + OAUTH_TOKEN_SECRET)
return Twython(consumer_key, consumer_secret, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
def getTwitter():
if os.path.isfile(saved_tokens_path):
with open(saved_tokens_path, "r") as f:
print("Reading saved tokens")
saved_token = f.readline().rstrip('\n')
saved_secret = f.readline().rstrip('\n')
twitter = Twython(consumer_key, consumer_secret, saved_token, saved_secret)
# twitter.verify_credentials()
return twitter
else:
return firstTimeLogin()
# Do request
def replyToTweet(tweetToReplyTo, contentsOfTweet, twitter):
sender = tweetToReplyTo['user']['screen_name']
tweetId = tweetToReplyTo['id']
status = "@" + sender + " " + contentsOfTweet
print("Tweeting: " + status)
# twitter.update_status(status = status, in_reply_to_status_id = tweetId)
def processAddTweet(tweet, twitter):
contents = tweet['text']
m = re.search('add @(\w+)$', contents)
if m:
print("Adding user: " + m.group(1))
did_add_person = person_manager.addPerson(m.group(1))
if did_add_person:
person_manager.savePeople()
replyToTweet(tweet, "Welcome on board @" + m.group(1) + "!", twitter)
else:
replyToTweet(tweet, "I already know about you, silly!", twitter)
else:
print("Error passsing match" + contents)
replyToTweet(tweet, "Sorry, that isn't a valid \'add\' command", twitter)
def processRemoveTweet(tweet, twitter):
contents = tweet['text']
m = re.search('remove @(\w+)$', contents)
if m:
print("Removig user: " + m.group(1))
did_remove_person = person_manager.removePerson(m.group(1))
if did_remove_person :
person_manager.savePeople()
replyToTweet(tweet, "Goodbye!", twitter)
else:
replyToTweet(tweet, "I litterally don't know who you are...", twitter)
else:
print("Error passsing match " + contents)
replyToTweet(tweet, "Sorry, that isn't a valid \'remove\' command", twitter)
def processAddRestaurant(tweet, twitter):
contents = tweet['text']
m = re.search('restaurant ([\w\s\',]+)$', contents)
if m:
print("adding restaurant: " + m.group(1))
new_restaurant = restaurant.Restaurant.fromName(m.group(1))
did_add_restaurant = restaurant_manager.addRestaurant(new_restaurant)
if did_add_restaurant:
restaurant_manager.saveRestaurants()
replyToTweet(tweet, "Ooo, good choice, I'd be salvating if I was a human", twitter)
else:
replyToTweet(tweet, "So good you want to add it twice?", twitter)
else:
print("Error passsing match " + contents)
replyToTweet(tweet, "Sorry, that isn't a valid restaurant command", twitter)
def processUnknownTweet(tweet, twitter):
replyToTweet(tweet, "Sorry, I don't recognise that command, use add, remove or restaurant, cc @thk123", twitter)
def composeRestaurantTweet(restaurant, people_manager, twitter):
fixed_message = "We're going to " + restaurant.restaurant_name + " at 1pm"
remaining_characters = 140 - len(fixed_message)
tweets = []
current_tweet = ""
for person in people_manager.people:
length_required_for_handle = len(person) + 1 + 1
if len(current_tweet) + length_required_for_handle < remaining_characters:
current_tweet = current_tweet + "@" + person + " "
else:
current_tweet = current_tweet + fixed_message
tweets.append(current_tweet)
current_tweet = "@" + person + " "
tweets.append(current_tweet + fixed_message)
id_to_reply_to = 0
for tweet in tweets:
if id_to_reply_to > 0:
result = twitter.update_status(status = tweet, in_reply_to_status_id = id_to_reply_to)
else:
result = twitter.update_status(status = tweet)
id_to_reply_to = long(result['id'])
twitter = getTwitter()
person_manager = person_manager.PersonManager()
restaurant_manager = restaurant_manager.RestaurantManager()
runSetup(restaurant_manager, person_manager)
latest_status_id_file = "last_status.txt"
latest_status_id = 0
if os.path.isfile(latest_status_id_file):
with open(latest_status_id_file, 'r') as f:
latest_status_id = long(f.read())
print("Getting all tweets after " + str(latest_status_id))
# check for new mentions
if latest_status_id > 0:
mentions = twitter.get_mentions_timeline(since_id = str(latest_status_id))
else:
mentions = twitter.get_mentions_timeline()
for mention in reversed(mentions):
tweetContent = mention['text'].lower()
print(tweetContent)
if 'add' in tweetContent:
print("Adding user")
processAddTweet(mention, twitter)
elif 'remove' in tweetContent:
processRemoveTweet(mention, twitter)
elif 'restaurant' in tweetContent:
processAddRestaurant(mention, twitter)
else:
processUnknownTweet(mention, twitter)
status_id = long(mention['id'])
if status_id > latest_status_id:
latest_status_id = status_id
with open(latest_status_id_file, 'w') as f:
f.write(str(latest_status_id))
chosen_restaurant = restaurant_manager.pickRestaurant()
print("Chosen restaurant: " + str(chosen_restaurant))
chosen_restaurant.visit()
restaurant_manager.saveRestaurants()
composeRestaurantTweet(chosen_restaurant, person_manager, twitter)
# TODO: Respond to requests to organise pub
# TODO: Allow people to opt-in/out of lunch/pub individually
#t = Twitter(auth = OAuth(token, token_key, con_secret, con_secret_key))
#t.statuses.update(status = "Test status")