-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_data.py
More file actions
49 lines (42 loc) · 1.02 KB
/
fetch_data.py
File metadata and controls
49 lines (42 loc) · 1.02 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
import requests
import os
import json
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv())
token = os.environ["UCLAPI_TOKEN"]
params = {
"token": token,
"results_per_page": "1000",
"contact": "Society"
}
req = requests.get(
"https://uclapi.com/roombookings/bookings",
params=params
)
resp = req.json()
bookings = resp["bookings"]
next_page = resp["next_page_exists"]
counter = 0
while next_page:
page_token = resp["page_token"]
params = {
"token": token,
"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('bookings.json', 'w') as f:
f.write(
json.dumps({
"bookings": bookings
}, sort_keys=True, indent=4)
)