-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscrape.py
82 lines (66 loc) · 2.07 KB
/
scrape.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
# imports
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from datetime import datetime, timedelta
from time import sleep
# config, used for both scraping methods
base = "https://uiuc.libcal.com/"
all = "allspaces" # base + all
id = { # base + "spaces/?lid=" + id of desired library
"chemistry": 5903,
"funk": 3604,
"grainger": 3606,
"international": 5766,
"main": 3608,
"music": 3153,
"studio": 16231
}
args = "&gid=0&c=0" # additional arguments
busyness = {
"chemistry": 0,
"funk": 0,
"grainger": 0,
"international": 0,
"main": 0,
"music": 0,
"studio": 0
}
rounded = datetime.now() - (datetime.now() - datetime.min) % timedelta(minutes=30)
meridian = ("am", "pm")[bool(int((datetime.now()).strftime('%H')) > 11)]
current_time = (rounded.strftime('%Y/%m/%d %I:%M:%S'))[11:16] + meridian
# # open selenium instance
options = Options()
options.add_argument("--headless=new")
driver = webdriver.Chrome(options=options)
for key in id:
print("_______________________")
print("Library: " + key)
# url to scrape
url = base + "spaces?lid=" + str(id[key]) + args
# # navigate to url
driver.get(url)
# wait for page load
sleep(.05)
# # extract data
events = [ev.get_attribute("title") for ev in driver.find_elements(By.CLASS_NAME, "fc-event-today")
if current_time in ("0" + ev.get_attribute("title"))]
if len(events) == 0:
busyness[key] = 0
else:
total_unavailable = 0
for ev in events:
print(ev)
if "Unavailable" in ev:
total_unavailable = total_unavailable + 1
if total_unavailable == 0:
busyness[key] = 1
else:
print("Total: " + str(len(events)))
print("Unavailable: " + str(total_unavailable))
ratio = float(total_unavailable)/len(events) * 100
busyness[key] = round((ratio)/20)
print("Busyness: " + str(busyness[key]))
# quit driver
driver.quit()
print(busyness)