-
Notifications
You must be signed in to change notification settings - Fork 2
/
attendees.py
executable file
·124 lines (107 loc) · 4.2 KB
/
attendees.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
"""Print a list of attendees for a meetup event."""
# http://www.meetup.com/meetup_api/
import argparse, pprint, random, sys, time
from collections import defaultdict
from meetup import meetup
from keys import GROUP_ID
the_encoding = sys.stdout.encoding or 'utf8'
def list_events(**kwargs):
"""Return a list of events, from the 2/events Meetup API call."""
kwargs.setdefault('group_id', GROUP_ID)
events = meetup("2/events", **kwargs)
for event in events:
event['when'] = time.strftime("%d %b %Y", time.localtime(event['time']//1000))
return events
def event_answers(event_id):
"""Return a dict mapping {"yes","no","maybe"} onto (username, num_guests) pairs."""
answers = defaultdict(list)
rsvps = meetup("2/rsvps", event_id=event_id)
for rsvp in rsvps:
answers[rsvp['response']].append((rsvp['member']['name'], rsvp['guests']))
return answers
def event_attendees(event_id):
"""Return the set of attendees in the yes or waiting list."""
rsvps = meetup("2/rsvps", event_id=event_id)
attendees = set()
for rsvp in rsvps:
if rsvp['response'] in ['yes', 'waitlist']:
member = rsvp['member']
attendees.add((member['member_id'], member['name']))
return attendees
def show_attendees(event_id):
answers = event_answers(event_id)
for ans in answers.keys():
members = sorted(answers[ans])
count = len(members) + sum(g for n,g in members)
print "== %d %s:" % (count, ans)
for m, g in members:
x = " "+m+(" +%d"%g if g else "")
try:
print x
except:
print repr(x)
def create_arg_parser():
parser = argparse.ArgumentParser()
parser.add_argument('-g', '--global')
subparsers = parser.add_subparsers(dest="command")
events_parser = subparsers.add_parser('events')
events_parser.add_argument('--csv', action='store_true')
attendees_parser = subparsers.add_parser('attendees')
attendees_parser.add_argument("event_id", type=int, nargs='?')
tutorials_parser = subparsers.add_parser('overlap')
return parser
def yes(event_id):
"""Return a list of names of people who RSVP'd yes to an event."""
answers = event_answers(event_id)
yesses = [a[0].title() for a in answers['yes']]
return sorted(yesses)
def main(argv):
parser = create_arg_parser()
args = parser.parse_args(argv)
if args.command == "attendees":
if args.event_id is None:
events = list_events(status="upcoming")
for event in events:
print "\n\n#%(id)s: %(when)s %(name)s, %(yes_rsvp_count)d" % event
show_attendees(event['id'])
else:
show_attendees(args.event_id)
elif args.command == "events":
events = list_events(status="upcoming,past")
if args.csv:
import csv
with open("events.csv", "wb") as fcsv:
writer = csv.writer(fcsv)
events = list_events(status="past")
for event in events:
writer.writerow((
event['when'],
event['name'],
event['event_url'],
event['yes_rsvp_count'],
))
else:
for event in events:
print "#%(id)s: %(when)s %(name)s, %(yes_rsvp_count)d" % event
elif args.command == "overlap":
events = {
"76032852": "PyCon 1",
"76034482": "PyCon 2",
#"100277162": "Intro Python",
#"100333402": "Bayesian",
#"100330612": "Open Source",
#"100333602": "Intermediate Twisted",
}
attending = defaultdict(list)
for event_id in events:
for attendee in event_attendees(event_id):
attending[attendee].append(event_id)
overlaps = (
(attendee[1], [events[e] for e in eventlist])
for attendee, eventlist in attending.items()
if len(eventlist) > 1
)
for i, (who, what) in enumerate(overlaps, 1):
print "%3d %s: %s" % (i, who.encode(the_encoding), what)
if __name__ == '__main__':
main(sys.argv[1:])