-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_rsvp.py
156 lines (114 loc) · 4.46 KB
/
auto_rsvp.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
from bs4 import BeautifulSoup
import json
import argparse
import utils.urls as urls
import utils.authentication as authentication
import utils.base_headers as base_headers
from utils.group import Group
from utils.event import Event
"""This module performs the automatic RSVP of selected groups. It containes the helper funtion as well as the driver code needed to implement auto_rsvp of selected groups"""
"""
Parsing Command line arguements to check for --dry_run
"""
parser = argparse.ArgumentParser(
description='This module performs the automatic RSVP of selected groups. It containes the helper funtion as well as the driver code needed to implement auto_rsvp of selected groups')
parser.add_argument(
'--dry_run', help='an integer for the accumulator', action='store_true', default=False)
args = parser.parse_args()
def get_upcoming_events_url(group):
"""
Used to get the events page url of a group.
Parameters:
group (Group object) : Instance of a class Group
Returns:
str : URL to fetch 10 events of this group.
"""
return urls.BASE_URL + group.group_url + '/events/'
def get_rsvped_events_string():
"""
A funtion which reads the rsvped_events.json file and parses the list of Groups
Parameters:
None
Returns:
list of Events : A list containing multiple Event objects
"""
with open('rsvped_events.json', "r") as f:
f.seek(0)
jsonStr = f.read()
return jsonStr
def rsvp_events(sess, token, group):
"""
Used to RSVP the events of a particular group
Parameters:
sess (requests.Session object): A Session object of the logged in session
group (Group object) : A object of Group class, denoting selected group.
Returns:
None
Writes:
logs the RSVP-ed events into a 'rsvped_events.json' file.
"""
url = get_upcoming_events_url(group)
rsvped_events_string = get_rsvped_events_string()
res = sess.get(url)
soup = BeautifulSoup(res.text, 'html.parser')
all_events = soup.find_all('a', {'id': 'attendButton'})
rsvped_events = []
for event in all_events:
sess.headers = base_headers.RSVP_GET
id = event.get('href').split('/')[-2]
rsvp_endpoint = urls.BASE_URL + \
event.get('href')+'action=rsvp&response=yes'
if id in rsvped_events_string:
continue
if not args.dry_run:
params = (
('action', 'rsvp'),
('response', 'yes'),
)
res = sess.get(urls.BASE_URL + event.get('href'), params=params)
sess.headers = base_headers.RSVP_POST
sess.headers['x-mwp-csrf'] = sess.cookies['x-mwp-csrf-header']
sess.headers['referer'] = rsvp_endpoint
query = '(endpoint:' + group.group_url + "/events/" + id + "/rsvps" + ',meta:(method:post),' + 'params:(eventId:' + id + \
',fields:rsvp_counts,' + 'response:yes,' + 'urlname:' + group.group_url + \
')' + ',ref:rsvpAction' + "_" + group.group_url + '_' + id + ')'
data = {
'queries': query
}
res = sess.post(
'https://www.meetup.com/mu_api/urlname/events/eventId', data=data)
if res.status_code == 200:
rsvp_event = Event(group.group_name, id, True)
rsvped_events.append(rsvp_event)
old_rsvped_events = [Event.fromJson(x)
for x in json.loads(rsvped_events_string)]
rsvped_events += old_rsvped_events
jsonStr = json.dumps([x.__dict__ for x in rsvped_events])
with open('rsvped_events.json', "w") as f:
f.seek(0)
f.write(jsonStr)
f.truncate()
def get_selected_groups():
"""
A funtion which reads the selected_groups.json file created by setup_configs and parses the list of Groups
Parameters:
None
Returns:
list of Group : A list containing multiple Group objects
"""
with open('selected_groups.json', "r") as f:
f.seek(0)
jsonStr = f.read()
selected_groups = [Group.fromJson(x) for x in json.loads(jsonStr)]
print(selected_groups)
return selected_groups
def main():
"""
Driver Funtion, used to orchestrate and link various helper functions
"""
selected_groups = get_selected_groups()
logged_in_session, token = authentication.signin(verbose=False)
for group in selected_groups:
rsvp_events(logged_in_session, token, group)
if __name__ == "__main__":
main()