-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcollect_data.py
More file actions
61 lines (49 loc) · 1.83 KB
/
collect_data.py
File metadata and controls
61 lines (49 loc) · 1.83 KB
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
import requests
import json
from make_string import next_booking, epoch, is_after_today
"""Fetches all the bookings for societies from UCL API and stores appropriate messages in tweets.json.
These are used when responding to tweets."""
def collect_data():
with open("secret.json", 'r') as f:
secret = json.load(f)
with open("weird_socs.json", 'r') as f:
weird_socs = json.load(f)
bookings = []
for search_term in weird_socs + ["Society", "Club"]:
params = {
"token": secret['uclapikey'],
"results_per_page": "1000",
"contact": search_term
}
req = requests.get(
"https://uclapi.com/roombookings/bookings",
params=params
)
resp = req.json()
print(resp)
bookings += resp["bookings"]
next_page = resp["next_page_exists"]
counter = 0
while next_page:
page_token = resp["page_token"]
params = {
"token": secret['uclapikey'],
"page_token": page_token
}
pagination_req = requests.get(
"https://uclapi.com/roombookings/bookings",
params=params
)
pagination_resp = pagination_req.json()
bookings += pagination_resp["bookings"]
if pagination_resp["next_page_exists"] and counter < 11:
next_page = True
counter += 1
else:
next_page = False
with open('tweets.json', 'w') as f:
f.write(
json.dumps([{"society": x["contact"].split("- ")[1], "booking": next_booking(x),
"time": epoch(x["start_time"])} for x in bookings if is_after_today(x["start_time"])],
sort_keys=True, indent=4)
)