Skip to content

Commit 2e48091

Browse files
authored
Merge pull request #70 from pennlabs/laundryusage
Laundry Usage
2 parents 9eb7675 + e323c39 commit 2e48091

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

penn/laundry.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
ALL_URL = 'https://www.laundryalert.com/cgi-bin/penn6389/LMPage?Login=True'
66
HALL_BASE_URL = 'https://www.laundryalert.com/cgi-bin/penn6389/LMRoom?Halls='
7+
USAGE_BASE_URL = 'https://www.laundryalert.com/cgi-bin/penn6389/LMRoomUsage?CallingPage=LMRoom&Password=penn6389&Halls='
78

89

910
class Laundry(object):
@@ -16,7 +17,16 @@ class Laundry(object):
1617
"""
1718

1819
def __init__(self):
19-
pass
20+
self.busy_dict = {
21+
'LowBusyNightColor': 'Low',
22+
'LowBusyDayColor': 'Low',
23+
'MediumLowBusyColor': 'Medium',
24+
'MediumHighBusyColor': 'High',
25+
'HighBusyColor': 'Very High',
26+
'NoDataBusyColor': 'No Data'
27+
}
28+
self.days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
29+
'Saturday', 'Sunday']
2030

2131
@staticmethod
2232
def get_hall_no(href):
@@ -116,4 +126,39 @@ def to_dict(data_row):
116126
d['time_left'] = None
117127
return d
118128

119-
return {'machines': list(map(to_dict, data_improved)), 'hall_name': hall_name}
129+
return {
130+
'machines': list(map(to_dict, data_improved)),
131+
'hall_name': hall_name
132+
}
133+
134+
def machine_usage(self, hall_no):
135+
"""Returns the average usage of laundry machines every hour
136+
for a given hall.
137+
138+
The usages are returned in a dictionary, with the key being
139+
the day of the week, and the value being an array listing the usages
140+
per hour.
141+
142+
:param hall_no:
143+
integer corresponding to the id number for the hall. Thus number
144+
is returned as part of the all_status call.
145+
146+
>>> english_house = l.machine_usage(2)
147+
"""
148+
149+
try:
150+
num = int(hall_no)
151+
except ValueError:
152+
raise ValueError("Room Number must be integer")
153+
r = requests.get(USAGE_BASE_URL + str(num))
154+
parsed = BeautifulSoup(r.text, 'html5lib')
155+
usage_table = parsed.find_all('table', width='504px')[0]
156+
rows = usage_table.find_all('tr')
157+
usages = {}
158+
for i, row in enumerate(rows):
159+
day = []
160+
hours = row.find_all('td')
161+
for hour in hours:
162+
day.append(self.busy_dict[str(hour['class'][0])])
163+
usages[self.days[i]] = day
164+
return usages

tests/laundry_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,11 @@ def test_single_hall(self):
3030
ok_('available' in machine)
3131
ok_('machine_type' in machine)
3232
ok_('time_left' in machine)
33+
34+
def test_usage(self):
35+
for i in range(10):
36+
data = self.laundry.machine_usage(i)
37+
for j in data:
38+
ok_(j in self.laundry.days)
39+
for k in data[j]:
40+
ok_(k in self.laundry.busy_dict.values())

0 commit comments

Comments
 (0)