import requests
import datetime

from bs4 import BeautifulSoup
from .base import APIError


BASE_URL = "https://apps.wharton.upenn.edu/gsr"


class Wharton(object):
    """Used for interacting with the Wharton GSR site.

    Usage::

      >>> from penn import Wharton
      >>> s = Wharton()
    """

    def get_reservations(self, sessionid):
        """Returns a list of location IDs and names."""
        url = "{}{}".format(BASE_URL, "/reservations")
        cookies = dict(sessionid=sessionid)
        resp = requests.get(url, cookies=cookies)
        if "error" in resp:
            raise APIError("Server Error: {}, {}".format(resp["error"], resp.get("error_description")))

        html = resp.content.decode("utf8")

        if "https://weblogin.pennkey.upenn.edu" in html:
            raise APIError("Wharton Auth Failed. Session ID is not valid.")

        soup = BeautifulSoup(html, "html5lib")
        reservations = []
        media = soup.find_all("div", {'class': "Media-body"})
        for res in media:
            times = res.find_all("span", {'class': "list-view-item__end-time"})
            reservation = {
                "date": res.find("span", {'class': "list-view-item__start-time u-display-block"}).get_text(),
                "startTime": times[0].get_text(),
                "endTime": times[1].get_text(),
                "location": res.find("span", {'class': "list-view-item-building"}).get_text(),
                "booking_id": int(res.find("a")['href'].split("delete/")[1][:-1])
            }
            reservations.append(reservation)
        return reservations

@@ -1595,8 +1595,1299 @@
 vations%0A
+%0A    def delete_booking(self, sessionid, booking_id):%0A        %22%22%22Returns a list of location IDs and names.%22%22%22%0A        url = %22%7B%7D%7B%7D%7B%7D%22.format(BASE_URL, %22/delete/%22, booking_id)%0A        print(url)%0A        cookies = dict(sessionid=sessionid)%0A        %0A        try:%0A            resp = requests.get(url, cookies=cookies)%0A        except resp.exceptions.HTTPError as error:%0A            raise APIError(%22Server Error: %7B%7D%22.format(error))%0A%0A        if resp.status_code == 404:%0A            raise APIError(%22Booking could not be found on server.%22)%0A%0A        html = resp.content.decode(%22utf8%22)%0A        if %22https://weblogin.pennkey.upenn.edu%22 in html:%0A            raise APIError(%22Wharton Auth Failed. Session ID is not valid.%22)%0A%0A        soup = BeautifulSoup(html, %22html5lib%22)%0A        print(resp.cookies)%0A        csrftoken = resp.cookies%5B'csrftoken'%5D%0A        middleware_token = soup.find(%22input%22, %7B'name': %22csrfmiddlewaretoken%22%7D).get('value')%0A        cookies2 = %7B'sessionid': sessionid, 'csrftoken': csrftoken%7D%0A%0A        try:%0A            resp2 = requests.post(url, cookies=cookies2, data=%7B'csrfmiddlewaretoken': middleware_token%7D)%0A        except resp.exceptions.HTTPError as error:%0A            raise APIError(%22Server Error: %7B%7D%22.format(error))%0A%0A        print(resp2.content.decode(%22utf8%22))%0A%0A        return %22success%22%0A

delete booking unfinished