Add iCalendar generation support

This commit is contained in:
Ian Adam Naval 2019-01-21 21:46:10 -05:00
parent a4b65df21a
commit bbba4c5b93
7 changed files with 51 additions and 15 deletions

View File

@ -10,6 +10,7 @@ pylint = "*"
requests = "*"
beautifulsoup4 = "*"
pendulum = "*"
vobject = "*"
[requires]
python_version = "3.7"

9
Pipfile.lock generated
View File

@ -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": {

View File

@ -19,6 +19,6 @@ $EDITOR .env # add your config
```bash
source .env
python -m autopilot.scrape
python -m autopilot.app
```

11
autopilot/app.py Normal file
View File

@ -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()

23
autopilot/ical.py Normal file
View File

@ -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

View File

@ -14,3 +14,4 @@ class Reservation(object):
start: pendulum.DateTime
end: pendulum.DateTime
comments: str
summary: str

View File

@ -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