From bbba4c5b93c55ad1a9c8b810b2b6485ff7bf1da5 Mon Sep 17 00:00:00 2001 From: Ian Adam Naval Date: Mon, 21 Jan 2019 21:46:10 -0500 Subject: [PATCH] Add iCalendar generation support --- Pipfile | 1 + Pipfile.lock | 9 ++++++++- README.md | 2 +- autopilot/app.py | 11 +++++++++++ autopilot/ical.py | 23 +++++++++++++++++++++++ autopilot/reservation.py | 1 + autopilot/scrape.py | 19 ++++++------------- 7 files changed, 51 insertions(+), 15 deletions(-) create mode 100644 autopilot/app.py create mode 100644 autopilot/ical.py diff --git a/Pipfile b/Pipfile index f9fff9f..1253bfb 100644 --- a/Pipfile +++ b/Pipfile @@ -10,6 +10,7 @@ pylint = "*" requests = "*" beautifulsoup4 = "*" pendulum = "*" +vobject = "*" [requires] python_version = "3.7" diff --git a/Pipfile.lock b/Pipfile.lock index e8056f2..89a9ac2 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "c4123f8147ddb51b8a343ecb90f3c00907f83ef681abfb8f678f7d1edb58dd9b" + "sha256": "3d57d6ab35e47c3823c967b0f56f5c15270487b2b3fbd79e5f3c8305577e1b43" }, "pipfile-spec": 6, "requires": { @@ -101,6 +101,13 @@ "sha256:de9529817c93f27c8ccbfead6985011db27bd0ddfcdb2d86f3f663385c6a9c22" ], "version": "==1.24.1" + }, + "vobject": { + "hashes": [ + "sha256:96512aec74b90abb71f6b53898dd7fe47300cc940104c4f79148f0671f790101" + ], + "index": "pypi", + "version": "==0.9.6.1" } }, "develop": { diff --git a/README.md b/README.md index 1bbb57e..f7dbd5a 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,6 @@ $EDITOR .env # add your config ```bash source .env -python -m autopilot.scrape +python -m autopilot.app ``` diff --git a/autopilot/app.py b/autopilot/app.py new file mode 100644 index 0000000..323177e --- /dev/null +++ b/autopilot/app.py @@ -0,0 +1,11 @@ +from autopilot.ical import generate_ical +from autopilot.scrape import get_reservations + + +def main(): + reservations = get_reservations() + ical = generate_ical(reservations) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/autopilot/ical.py b/autopilot/ical.py new file mode 100644 index 0000000..9b42278 --- /dev/null +++ b/autopilot/ical.py @@ -0,0 +1,23 @@ +from typing import List + +import vobject + +from autopilot.reservation import Reservation + + +VCalendar2_0 = vobject.icalendar.VCalendar2_0 + + +def add_vevent(cal: VCalendar2_0, reservation: Reservation): + vevent = cal.add('vevent') + vevent.add('dtstart').value = reservation.start + vevent.add('dtend').value = reservation.end + vevent.add('summary').value = reservation.summary + return cal + + +def generate_ical(reservations: List[Reservation]) -> VCalendar2_0: + cal = vobject.iCalendar() + for reservation in reservations: + add_vevent(cal, reservation) + return cal diff --git a/autopilot/reservation.py b/autopilot/reservation.py index 86e378d..d232344 100644 --- a/autopilot/reservation.py +++ b/autopilot/reservation.py @@ -14,3 +14,4 @@ class Reservation(object): start: pendulum.DateTime end: pendulum.DateTime comments: str + summary: str diff --git a/autopilot/scrape.py b/autopilot/scrape.py index 23abc04..00a58a8 100644 --- a/autopilot/scrape.py +++ b/autopilot/scrape.py @@ -90,24 +90,17 @@ def make_reservation_from_tag(tag: Tag) -> Reservation: strict=False, tz=config.TIME_ZONE) attributes[datetime_attr] = parsed_datetime + attributes['summary'] = raw return Reservation(**attributes) -def get_reservations(session: requests.Session) -> List[Reservation]: - html = raw_schedule_html(session) - soup = BeautifulSoup(html, 'html.parser') - return [make_reservation_from_tag(tag) for tag in soup.select('td.cR')] - - -def main(): +def get_reservations() -> List[Reservation]: logging.config.dictConfig(config.LOGGING_CONFIG) session = init_session() authenticate(session) LOGGER.info("Authentication successful") - reservations = get_reservations(session) - import pdb; pdb.set_trace() + html = raw_schedule_html(session) + soup = BeautifulSoup(html, 'html.parser') + reservations = [make_reservation_from_tag(tag) for tag in soup.select('td.cR')] LOGGER.info("Got %d reservations", len(reservations)) - - -if __name__ == "__main__": - main() + return reservations