-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.py
145 lines (101 loc) · 3.67 KB
/
library.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
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 19 16:58:43 2024
@author: Adam
"""
#Requests imports
import json
from bs4 import BeautifulSoup
#Selenium imports
from selenium import webdriver
from fake_useragent import UserAgent
from datetime import datetime
url = 'https://cal.lib.rpi.edu/reserve/folsomlibrary/groupstudyrooms'
header = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) '
'AppleWebKit/537.11 (KHTML, like Gecko) '
'Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'}
def add_block(hours):
hour = int(hours[0:2])
minutes = hours[2:4]
assert(minutes == '00' or minutes == '15' or minutes=='30' or minutes == '45')
if int(minutes) == 45:
minutes = '00'
hour = hour+1
else:
minutes = str(int(minutes)+15)
if hour < 10:
hour = "0{}".format(hour)
if hour == 24:
hour = '00'
return "{}{}".format(hour, minutes)
def convert_mil(hours):
times = hours.split(':')
hour = int(times[0])
minutes = times[1][0:2]
if 'pm' in hours:
hour += 12
else:
if hour < 10:
hour = "0{}".format(hour)
result = "{}{}".format(hour, minutes)
return result
def get_bookings():
today = datetime.today().weekday()
day_number = (today + 1) % 7
#req = urllib.request.Request(url, headers=header)
#page = urllib.request.urlopen(req)
#page = page.read().decode("utf8")
data = dict()
options = webdriver.ChromeOptions()
options.add_argument("--headless")
UA = UserAgent().random # Create a random user agent
options.add_argument(f'--user-agent={UA}')
# Install appropriate webdriver for platform
driver = webdriver.Chrome(options=options)
driver.implicitly_wait(10)
driver.get(url)
page = driver.page_source
soup = BeautifulSoup(page, 'html.parser')
driver.close()
#For testing, use this to view scraped html
with open('test.html', 'w', encoding = 'utf-8') as f:
f.write(soup.prettify())
rooms = soup.findAll("span", {"class": "fc-datagrid-cell-main"})
table = soup.findAll("table", {"class": "fc-scrollgrid-sync-table table-bordered"})
assert(len(table) == 1)
table = table[0]
slots = table.findAll("div", {"class": "fc-timeline-event-harness"})
rooms = set()
for slot in slots:
description = slot.find("a").attrs['title']
description = description.split('-')
time = description[0].strip()
room = description[1].strip()
status = description[2].strip()
if room not in rooms:
rooms.add(room)
print(time, room, status)
if room not in data:
data[room] = dict()
data[room]["{}:{}-{}:{}".format(day_number, convert_mil(time), day_number, add_block(convert_mil(time)))] = [status, 0, []]
with open('data/data.json', 'r') as file:
roomsData = json.load(file)
for room in rooms:
print(room)
roomsData['Folsom'][room] = data[room]
# All study rooms but 353B have capacity 6, so hard code this
if room == '353B':
roomsData['Folsom'][room]['meta'] = {"max": 8}
else:
roomsData['Folsom'][room]['meta'] = {"max": 6}
#print(data[room])
# with open('data/library_data', 'w') as convert_file:
# convert_file.write(json.dumps(data))
with open('data/data.json', 'w') as convert_file:
convert_file.write(json.dumps(roomsData, indent = 4))
get_bookings()