-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathdining.py
161 lines (131 loc) · 5.35 KB
/
dining.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""
The Pitt API, to access workable data of the University of Pittsburgh
Copyright (C) 2015 Ritwik Gupta
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
from __future__ import annotations
import requests
from datetime import datetime
from typing import Any
JSON = dict[str, Any]
REQUEST_HEADERS = {"User-Agent": "Chrome/103.0.5026.0"}
LOCATIONS = {
"ETHEL'S",
"THE EATERY",
"PANERA BREAD",
"TRUE BURGER",
"THE PERCH",
"FORBES STREET MARKET",
"BUNSEN BREWER",
"WICKED PIE",
"SMOKELAND BBQ AT THE PETERSEN EVENTS CENTER",
"THE MARKET AT TOWERS",
"THE DELICATESSEN",
"CAMPUS COFFEE & TEA CO - TOWERS",
"PA TACO CO.",
"FT. PITT SUBS",
"CREATE",
"POM & HONEY",
"THE ROOST",
"CATHEDRAL SUSHI",
"BURRITO BOWL",
"CHICK-FIL-A",
"SHAKE SMART",
"STEEL CITY KITCHEN",
"SMOKELAND BBQ FOOD TRUCK",
"CAMPUS COFFEE & TEA CO - SUTHERLAND",
"THE MARKET AT SUTHERLAND",
"PLATE TO PLATE AT SUTHERLAND MARKET",
"EINSTEIN BROS. BAGELS - POSVAR",
"EINSTEIN BROS. BAGELS - BENEDUM",
"BOTTOM LINE BISTRO",
"CAFE VICTORIA",
"CAFE 1787",
"CAMPUS COFFEE & TEA CO - PUBLIC HEALTH",
"RXPRESSO",
"SIDEBAR CAFE",
"CAFE 1923",
}
LOCATIONS_URL = "https://api.dineoncampus.com/v1/locations/status?site_id=5e6fcc641ca48e0cacd93b04&platform="
HOURS_URL = "https://api.dineoncampus.com/v1/locations/weekly_schedule?site_id=5e6fcc641ca48e0cacd93b04&date=%22{date_str}%22"
PERIODS_URL = "https://api.dineoncampus.com/v1/location/{location_id}/periods?platform=0&date={date_str}"
MENU_URL = "https://api.dineoncampus.com/v1/location/{location_id}/periods/{period_id}?platform=0&date={date_str}"
def get_locations() -> dict[str, JSON]:
"""Gets data about all dining locations"""
resp = requests.get(LOCATIONS_URL, headers=REQUEST_HEADERS)
locations = resp.json()["locations"]
dining_locations = {location["name"].upper(): location for location in locations}
return dining_locations
def get_location_hours(location_name: str | None = None, date: datetime | None = None) -> dict[str, list[dict[str, int]]]:
"""Returns dictionary containing Opening and Closing times of locations open on date.
- Ex:{'The Eatery': [{'start_hour': 7, 'start_minutes': 0, 'end_hour': 0, 'end_minutes': 0}]}
- if location_name is None, returns times for all locations
- date must be in YYYY,MM,DD format, will return data on current day if None
"""
if location_name is not None:
location_name = location_name.upper()
if location_name not in LOCATIONS:
raise ValueError("Invalid Dining Location")
if date is None:
date = datetime.now()
date_str = date.strftime("%Y-%m-%d")
resp = requests.get(
HOURS_URL.format(date_str=date_str),
headers=REQUEST_HEADERS,
)
if resp.status_code == 502:
raise ValueError("Invalid Date")
locations = resp.json()["the_locations"]
if location_name is None:
hours = {
location["name"]: day["hours"] for location in locations for day in location["week"] if day["date"] == date_str
}
return hours
for location in locations:
if location["name"].upper() == location_name:
hours = {location["name"]: day["hours"] for day in location["week"] if day["date"] == date_str}
return hours
return {}
def get_location_menu(location: str, date: datetime | None = None, period_name: str | None = None) -> JSON:
"""Returns menu data for given dining location on given day/period
- period_name used for locations with different serving periods(i.e. 'Breakfast','Lunch','Dinner','Late Night')
- None -> Returns menu for first(or only) period at location
"""
location = location.upper()
if location not in LOCATIONS:
raise ValueError("Invalid Dining Location")
if date is None:
date = datetime.today()
if period_name is not None:
period_name = period_name.lower()
date_str = date.strftime("%y-%m-%d")
location_id = get_locations()[location]["id"]
periods_resp = requests.get(
PERIODS_URL.format(location_id=location_id, date_str=date_str),
headers=REQUEST_HEADERS,
)
if periods_resp.status_code == 502:
raise ValueError("Invalid Date")
periods = periods_resp.json()["periods"]
if period_name is None or len(periods) == 1:
period_id = periods[0]["id"]
else:
for period in periods:
if period["name"].lower() == period_name:
period_id = period["id"]
menu_resp = requests.get(
MENU_URL.format(location_id=location_id, period_id=period_id, date_str=date_str),
headers=REQUEST_HEADERS,
)
menu: JSON = menu_resp.json()["menu"]
return menu