17
17
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
18
"""
19
19
20
+ from __future__ import annotations
21
+
20
22
import requests
21
- import json
22
23
from datetime import datetime
23
24
from typing import Any
24
25
26
+ JSON = dict [str , Any ]
27
+
25
28
REQUEST_HEADERS = {"User-Agent" : "Chrome/103.0.5026.0" }
26
29
27
30
LOCATIONS = {
68
71
MENU_URL = "https://api.dineoncampus.com/v1/location/{location_id}/periods/{period_id}?platform=0&date={date_str}"
69
72
70
73
71
- def get_locations () -> dict [str , Any ]:
74
+ def get_locations () -> dict [str , JSON ]:
72
75
"""Gets data about all dining locations"""
73
76
resp = requests .get (LOCATIONS_URL , headers = REQUEST_HEADERS )
74
- locations = json . loads ( resp .content )["locations" ]
77
+ locations = resp .json ( )["locations" ]
75
78
dining_locations = {location ["name" ].upper (): location for location in locations }
76
79
77
80
return dining_locations
78
81
79
82
80
- def get_location_hours (location_name : str , date : datetime ) -> dict [str , Any ]:
83
+ def get_location_hours (location_name : str | None = None , date : datetime | None = None ) -> dict [str , list [ dict [ str , int ]] ]:
81
84
"""Returns dictionary containing Opening and Closing times of locations open on date.
82
- -Ex:{'The Eatery': [{'start_hour': 7, 'start_minutes': 0, 'end_hour': 0, 'end_minutes': 0}]}
85
+ - Ex:{'The Eatery': [{'start_hour': 7, 'start_minutes': 0, 'end_hour': 0, 'end_minutes': 0}]}
83
86
- if location_name is None, returns times for all locations
84
87
- date must be in YYYY,MM,DD format, will return data on current day if None
85
88
"""
86
89
87
- if location_name is not None and location_name .upper () not in LOCATIONS :
88
- raise ValueError ("Invalid Dining Location" )
90
+ if location_name is not None :
91
+ location_name = location_name .upper ()
92
+ if location_name not in LOCATIONS :
93
+ raise ValueError ("Invalid Dining Location" )
89
94
90
95
if date is None :
91
96
date = datetime .now ()
@@ -99,7 +104,7 @@ def get_location_hours(location_name: str, date: datetime) -> dict[str, Any]:
99
104
if resp .status_code == 502 :
100
105
raise ValueError ("Invalid Date" )
101
106
102
- locations = json . loads ( resp .content )["the_locations" ]
107
+ locations = resp .json ( )["the_locations" ]
103
108
104
109
if location_name is None :
105
110
hours = {
@@ -108,27 +113,29 @@ def get_location_hours(location_name: str, date: datetime) -> dict[str, Any]:
108
113
return hours
109
114
110
115
for location in locations :
111
- if location ["name" ].upper () == location_name . upper () :
116
+ if location ["name" ].upper () == location_name :
112
117
hours = {location ["name" ]: day ["hours" ] for day in location ["week" ] if day ["date" ] == date_str }
113
118
return hours
114
119
115
120
return {}
116
121
117
122
118
- def get_location_menu (location : str , date : datetime , period_name : str ) :
123
+ def get_location_menu (location : str , date : datetime | None = None , period_name : str | None = None ) -> JSON :
119
124
"""Returns menu data for given dining location on given day/period
120
125
- period_name used for locations with different serving periods(i.e. 'Breakfast','Lunch','Dinner','Late Night')
121
126
- None -> Returns menu for first(or only) period at location
122
127
"""
123
-
124
- if location . upper () not in LOCATIONS :
128
+ location = location . upper ()
129
+ if location not in LOCATIONS :
125
130
raise ValueError ("Invalid Dining Location" )
126
131
127
132
if date is None :
128
133
date = datetime .today ()
134
+ if period_name is not None :
135
+ period_name = period_name .lower ()
129
136
130
137
date_str = date .strftime ("%y-%m-%d" )
131
- location_id = get_locations ()[location . upper () ]["id" ]
138
+ location_id = get_locations ()[location ]["id" ]
132
139
periods_resp = requests .get (
133
140
PERIODS_URL .format (location_id = location_id , date_str = date_str ),
134
141
headers = REQUEST_HEADERS ,
@@ -137,18 +144,18 @@ def get_location_menu(location: str, date: datetime, period_name: str):
137
144
if periods_resp .status_code == 502 :
138
145
raise ValueError ("Invalid Date" )
139
146
140
- periods = json . loads ( periods_resp .content )["periods" ]
147
+ periods = periods_resp .json ( )["periods" ]
141
148
if period_name is None or len (periods ) == 1 :
142
149
period_id = periods [0 ]["id" ]
143
150
else :
144
151
for period in periods :
145
- if period ["name" ].lower () == period_name . lower () :
152
+ if period ["name" ].lower () == period_name :
146
153
period_id = period ["id" ]
147
154
148
155
menu_resp = requests .get (
149
156
MENU_URL .format (location_id = location_id , period_id = period_id , date_str = date_str ),
150
157
headers = REQUEST_HEADERS ,
151
158
)
152
- menu = json . loads ( menu_resp .content )["menu" ]
159
+ menu : JSON = menu_resp .json ( )["menu" ]
153
160
154
161
return menu
0 commit comments