diff --git a/penn/__init__.py b/penn/__init__.py index e4ecf3a..2ecb707 100644 --- a/penn/__init__.py +++ b/penn/__init__.py @@ -1,4 +1,4 @@ -__version__ = '1.8.3' +__version__ = '1.8.5' from .registrar import Registrar from .directory import Directory diff --git a/penn/fitness.py b/penn/fitness.py index 3fc1154..b4106b3 100644 --- a/penn/fitness.py +++ b/penn/fitness.py @@ -49,7 +49,9 @@ def get_schedule(self): def get_usage(self): """Get fitness locations and their current usage.""" - resp = requests.get(FITNESS_URL, timeout=30) + resp = requests.get(FITNESS_URL, headers={ + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36" + }, timeout=30) resp.raise_for_status() soup = BeautifulSoup(resp.text, "html5lib") diff --git a/penn/studyspaces.py b/penn/studyspaces.py index 9daa250..ac907dd 100644 --- a/penn/studyspaces.py +++ b/penn/studyspaces.py @@ -239,16 +239,18 @@ def cancel_room(self, booking_id): resp = self._request("POST", "/1.1/space/cancel/{}".format(booking_id)) return resp.json() - def get_reservations(self, email, date): + def get_reservations(self, email, date, timeout=None): """Gets reservations for a given email. :param email: the email of the user who's reservations are to be fetched :type email: str """ try: - resp = self._request("GET", "/1.1/space/bookings?email={}&date={}&limit=100".format(email, date)) - except resp.exceptions.HTTPError as error: + resp = self._request("GET", "/1.1/space/bookings?email={}&date={}&limit=100".format(email, date), timeout=timeout) + except requests.exceptions.HTTPError as error: raise APIError("Server Error: {}".format(error)) + except requests.exceptions.ConnectTimeout: + raise APIError("Timeout Error") return resp.json() def get_reservations_for_booking_ids(self, booking_ids): diff --git a/penn/wharton.py b/penn/wharton.py index 6f6f23d..6b2eb8c 100644 --- a/penn/wharton.py +++ b/penn/wharton.py @@ -18,15 +18,17 @@ class Wharton(object): >>> s = Wharton() """ - def get_reservations(self, sessionid): + def get_reservations(self, sessionid, timeout=None): """Returns a list of location IDs and names.""" url = "{}{}".format(BASE_URL, "/reservations/") cookies = dict(sessionid=sessionid) try: - resp = requests.get(url, cookies=cookies) - except resp.exceptions.HTTPError as error: + resp = requests.get(url, timeout=timeout, cookies=cookies) + except requests.exceptions.HTTPError as error: raise APIError("Server Error: {}".format(error)) + except requests.exceptions.ConnectTimeout: + raise APIError("Timeout Error") html = resp.content.decode("utf8") @@ -54,6 +56,10 @@ def book_reservation(self, sessionid, roomid, start, end): format = "%Y-%m-%dT%H:%M:%S-{}".format(self.get_dst_gmt_timezone()) booking_url = "{}/reserve/{}/{}/?d={}".format(BASE_URL, roomid, start.strftime(format), duration) resp = requests.get(booking_url, cookies={"sessionid": sessionid}) + + if resp.status_code == 403: + return {"success": False, "error": "Your account does not have permission to book Wharton GSRs!"} + resp.raise_for_status() csrfheader = re.search(r"csrftoken=(.*?);", resp.headers["Set-Cookie"]).group(1) diff --git a/setup.py b/setup.py index 3a6e6f0..f27a4f7 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ url='https://github.com/pennlabs/penn-sdk-python', author='Penn Labs', author_email='admin@pennlabs.org', - version='1.8.3', + version='1.8.5', packages=['penn'], license='MIT', package_data={