This repository has been archived by the owner on May 3, 2023. It is now read-only.
/
evenite.py
251 lines (219 loc) · 7.89 KB
/
evenite.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import sys
import os.path
import urlparse
import urllib
import urllib2
import webbrowser
import json
import BaseHTTPServer
import facebook
import settings
ACCESS_TOKEN = None
class FBAuth(object):
"""
Authenticates FB account
"""
# Enter FB app credentials
APP_ID = settings.APP_ID
APP_SECRET = settings.APP_SECRET
REDIRECT_URI = 'http://127.0.0.1:8080/'
LOCAL_FILE = '.fb_access_token'
PERMS = ['create_event', 'user_events', 'read_friendlists']
def authenticate(self):
"""
Facebook authentication. Opens a browser window for authentication if
there isn't a LOCAL_FILE with the access_token, else it uses the
access_token in LOCAL_FILE.
"""
global ACCESS_TOKEN
self.access_token = None
if not os.path.exists(self.LOCAL_FILE):
print "Facebook authentication..."
webbrowser.open(facebook.auth_url(self.APP_ID, self.REDIRECT_URI,
self.PERMS))
# Creates a temp HTTPServer
httpd = BaseHTTPServer.HTTPServer(('127.0.0.1', 8080),
FBAuth.RequestHandler)
while ACCESS_TOKEN is None:
httpd.handle_request()
self.access_token = ACCESS_TOKEN
else:
self.access_token = open(self.LOCAL_FILE).read()
def init_graph(self):
"""
Constructs a Facebook GraphAPI object for query uses.
"""
self.graph = facebook.GraphAPI(access_token=self.access_token)
me = self.graph.request('/me')
self.username = me['name']
self.id = me['id']
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
"""
Simple HTTPServer that handles the URL parameters passed back to the
REDIRECT_URI by the Facebook API.
"""
def set_token(self, access_token=None):
self.access_token = access_token
def do_GET(self):
global ACCESS_TOKEN
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
code = urlparse.parse_qs(urlparse.urlparse(self.path).query).get('code')
code = code[0] if code else None
if code is None:
self.wfile.write("Sorry, authentication failed.")
sys.exit(1)
ACCESS_TOKEN = facebook.get_access_token_from_code(code,
FBAuth.REDIRECT_URI, FBAuth.APP_ID, FBAuth.APP_SECRET)['access_token']
open(FBAuth.LOCAL_FILE,'w').write(ACCESS_TOKEN)
self.wfile.write("You have successfully logged in to facebook. "
"You can close this window now.")
def print_title(num, title):
# unicode() prevents ASCII/uncode conflicts
print unicode("\t{} - {}").format(num, title)
def print_param(name, key, data):
event_format = "\t\t{}: {}"
if key in data:
print event_format.format(name, data[key])
def is_number(s):
try:
int(s)
return True
except ValueError:
return False
def is_char(s):
try:
ord(s)
return True
except TypeError:
return False
class FBMenu(object):
"""
Controls the logic for the terminal usage.
"""
def __init__(self):
self.auth = FBAuth()
self.auth.authenticate()
self.auth.init_graph()
self.event_list = []
self.friendlist_list = []
self.PROMPT = self.auth.username + "> "
def API_request(self, *args):
return "/".join(args)
def show_events(self):
"""
Displays recent events that includes you as the owner.
"""
print "Showing your recent events..."
# Reset event history
self.event_list = []
event_num = 0
events = self.auth.graph.request(
self.API_request('me', 'events'))['data']
for event in events:
print_title(chr(ord('A') + event_num), event['name'])
print_param("Start time", 'start_time', event)
print_param("End time", 'end_time', event)
print_param("Timezone", 'timezone', event)
print_param("Location", 'location', event)
# Build event history
self.event_list.append(event['id'])
event_num += 1
def show_lists(self):
print "Displaying your friend lists and networks..."
# Reset friendlist history
self.friendlist_list = []
friendlist_num = 0
friendlists = self.auth.graph.request(
self.API_request('me', 'friendlists'))['data']
for f in friendlists:
friend_count = len(self.auth.graph.request(
self.API_request(f['id'], 'members'))['data'])
print_title(friendlist_num, f['name'] + " - " + str(friend_count))
# Build friendlist history
self.friendlist_list.append(f['id'])
friendlist_num += 1
def show_list_friends(self, list_id):
"""
Displays all the people within a certain friend list
"""
friends = self.auth.graph.request(
self.API_request(self.friendlist_list[list_id], 'members'))['data']
for f in friends:
print "\t" + f['name']
def invite(self, event_index, list_index):
"""
Invites all the people in a certain list to a certain event
"""
# TODO: Currently will throw exception and fail if user doesn't
# have invite permissions
friendlist_id = self.friendlist_list[list_index]
event_id = self.event_list[event_index]
friends = self.auth.graph.request(
self.API_request(friendlist_id, 'members'))['data']
friend_ids = []
for f in friends:
friend_ids.append(f['id'])
# Not used -- if already invited, has response error
friends_query = ",".join(map(str, friend_ids))
for i in friend_ids:
post = {}
post['users'] = str(i)
try:
if self.auth.graph.request(
'{}/invited'.format(event_id),post_args=post):
print "Invitations succeeded"
else:
print "Invitations failed"
except facebook.GraphAPIError:
# GraphAPI #200 error thrown when a person who is already
# invited is invited again.
print "Duplicate"
def help(self):
print """
show events
show lists
show lists <list-num>
show all
invite <event-letter> <list-num>
exit
"""
def select(self, command, args):
"""
Controller
"""
if command == "show" and args[0] == "events":
self.show_events()
elif command == "show" and args[0] == "lists" and len(args) == 1:
self.show_lists()
elif (command == "show" and args[0] == "lists" and is_number(args[1]) \
and int(args[1]) < len(self.friendlist_list)):
self.show_list_friends(int(args[1]))
elif (command == "invite" and is_char(args[0]) and \
ord(args[0]) - ord('A') < len(self.event_list) and \
is_number(args[1]) and int(args[1]) < len(self.friendlist_list)):
self.invite(ord(args[0]) - ord('A'), int(args[1]))
elif command == "show" and args[0] == "all":
self.show_events()
self.show_lists()
elif command == "help":
self.help()
elif command == "exit":
sys.exit()
else:
print "Invalid command"
if __name__ == "__main__":
menu = FBMenu()
print """
Welcome to Facebook Evenite!
Type help for instructions
"""
try:
while 1:
params = raw_input(menu.PROMPT).split(' ')
command = params[0]
args = params[1:]
menu.select(command, args)
except KeyboardInterrupt:
print