Add iCalendar generation support
This commit is contained in:
parent
a4b65df21a
commit
bbba4c5b93
1
Pipfile
1
Pipfile
@ -10,6 +10,7 @@ pylint = "*"
|
|||||||
requests = "*"
|
requests = "*"
|
||||||
beautifulsoup4 = "*"
|
beautifulsoup4 = "*"
|
||||||
pendulum = "*"
|
pendulum = "*"
|
||||||
|
vobject = "*"
|
||||||
|
|
||||||
[requires]
|
[requires]
|
||||||
python_version = "3.7"
|
python_version = "3.7"
|
||||||
|
|||||||
9
Pipfile.lock
generated
9
Pipfile.lock
generated
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"_meta": {
|
"_meta": {
|
||||||
"hash": {
|
"hash": {
|
||||||
"sha256": "c4123f8147ddb51b8a343ecb90f3c00907f83ef681abfb8f678f7d1edb58dd9b"
|
"sha256": "3d57d6ab35e47c3823c967b0f56f5c15270487b2b3fbd79e5f3c8305577e1b43"
|
||||||
},
|
},
|
||||||
"pipfile-spec": 6,
|
"pipfile-spec": 6,
|
||||||
"requires": {
|
"requires": {
|
||||||
@ -101,6 +101,13 @@
|
|||||||
"sha256:de9529817c93f27c8ccbfead6985011db27bd0ddfcdb2d86f3f663385c6a9c22"
|
"sha256:de9529817c93f27c8ccbfead6985011db27bd0ddfcdb2d86f3f663385c6a9c22"
|
||||||
],
|
],
|
||||||
"version": "==1.24.1"
|
"version": "==1.24.1"
|
||||||
|
},
|
||||||
|
"vobject": {
|
||||||
|
"hashes": [
|
||||||
|
"sha256:96512aec74b90abb71f6b53898dd7fe47300cc940104c4f79148f0671f790101"
|
||||||
|
],
|
||||||
|
"index": "pypi",
|
||||||
|
"version": "==0.9.6.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"develop": {
|
"develop": {
|
||||||
|
|||||||
@ -19,6 +19,6 @@ $EDITOR .env # add your config
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
source .env
|
source .env
|
||||||
python -m autopilot.scrape
|
python -m autopilot.app
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
11
autopilot/app.py
Normal file
11
autopilot/app.py
Normal 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
23
autopilot/ical.py
Normal 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
|
||||||
@ -14,3 +14,4 @@ class Reservation(object):
|
|||||||
start: pendulum.DateTime
|
start: pendulum.DateTime
|
||||||
end: pendulum.DateTime
|
end: pendulum.DateTime
|
||||||
comments: str
|
comments: str
|
||||||
|
summary: str
|
||||||
|
|||||||
@ -90,24 +90,17 @@ def make_reservation_from_tag(tag: Tag) -> Reservation:
|
|||||||
strict=False,
|
strict=False,
|
||||||
tz=config.TIME_ZONE)
|
tz=config.TIME_ZONE)
|
||||||
attributes[datetime_attr] = parsed_datetime
|
attributes[datetime_attr] = parsed_datetime
|
||||||
|
attributes['summary'] = raw
|
||||||
return Reservation(**attributes)
|
return Reservation(**attributes)
|
||||||
|
|
||||||
|
|
||||||
def get_reservations(session: requests.Session) -> List[Reservation]:
|
def get_reservations() -> 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():
|
|
||||||
logging.config.dictConfig(config.LOGGING_CONFIG)
|
logging.config.dictConfig(config.LOGGING_CONFIG)
|
||||||
session = init_session()
|
session = init_session()
|
||||||
authenticate(session)
|
authenticate(session)
|
||||||
LOGGER.info("Authentication successful")
|
LOGGER.info("Authentication successful")
|
||||||
reservations = get_reservations(session)
|
html = raw_schedule_html(session)
|
||||||
import pdb; pdb.set_trace()
|
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))
|
LOGGER.info("Got %d reservations", len(reservations))
|
||||||
|
return reservations
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user