diff options
Diffstat (limited to 'email_assistant/plugins/united.py')
-rw-r--r-- | email_assistant/plugins/united.py | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/email_assistant/plugins/united.py b/email_assistant/plugins/united.py new file mode 100644 index 0000000..0b33cd9 --- /dev/null +++ b/email_assistant/plugins/united.py | |||
@@ -0,0 +1,99 @@ | |||
1 | #!/usr/bin/env python3 | ||
2 | |||
3 | # Copyright (C) 2019 James E. Blair <corvus@gnu.org> | ||
4 | # | ||
5 | # This file is part of Email-assistant. | ||
6 | # | ||
7 | # Email-assistant is free software: you can redistribute it and/or | ||
8 | # modify it under the terms of the GNU Affero General Public License | ||
9 | # as published by the Free Software Foundation, either version 3 of | ||
10 | # the License, or (at your option) any later version. | ||
11 | # | ||
12 | # Email-assistant is distributed in the hope that it will be useful, | ||
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | # General Public License for more details. | ||
16 | # | ||
17 | # You should have received a copy of the GNU General Public License | ||
18 | # along with Email-assistant. If not, see | ||
19 | # <https://www.gnu.org/licenses/>. | ||
20 | |||
21 | import re | ||
22 | import logging | ||
23 | import hashlib | ||
24 | |||
25 | from bs4 import BeautifulSoup | ||
26 | import dateutil.parser | ||
27 | import dateutil.tz | ||
28 | import inscriptis | ||
29 | import vobject | ||
30 | |||
31 | from email_assistant import iata | ||
32 | from email_assistant import plugin | ||
33 | |||
34 | def parse_dep_arr(flight_date, dep_arr): | ||
35 | flight_year = dateutil.parser.parse(flight_date).year | ||
36 | city, br, code, flight_time = dep_arr.span.children | ||
37 | city = city.strip() | ||
38 | code = code.strip()[1:-1] | ||
39 | code = code.split()[0] | ||
40 | tz = iata.tzmap[code] | ||
41 | flight_time = flight_time.get_text().strip() | ||
42 | m = re.match(r'(.*) \((\d+[A-Z]+)\)', flight_time) | ||
43 | if m: | ||
44 | s = '%s%s %s' % (m.group(2), flight_year, m.group(1)) | ||
45 | flight_time = dateutil.parser.parse(s) | ||
46 | else: | ||
47 | flight_time = dateutil.parser.parse(flight_date +' '+ flight_time) | ||
48 | flight_time = flight_time.replace(tzinfo=dateutil.tz.gettz(tz)) | ||
49 | return (city, code, flight_time) | ||
50 | |||
51 | class Plugin(plugin.Plugin): | ||
52 | name = 'united' | ||
53 | |||
54 | def match(self, msg): | ||
55 | if ('unitedairlines@united.com' in msg['From'] and | ||
56 | 'Itinerary and Receipt' in msg['Subject']): | ||
57 | return True | ||
58 | |||
59 | def get_events(self, msg): | ||
60 | events = [] | ||
61 | for part in msg.walk(): | ||
62 | if part.get_content_type() == 'text/html': | ||
63 | soup = BeautifulSoup(part.get_payload(decode=True).decode('utf8'), 'html.parser') | ||
64 | # confirmation_number = soup.find(class_="eTicketConfirmation").string | ||
65 | |||
66 | index = 0 | ||
67 | while True: | ||
68 | info = soup.find(id="ShowSegments_ShowSegment_ctl%02i_Flight" % index) | ||
69 | if not info: | ||
70 | break | ||
71 | for row in info.parents: | ||
72 | if row.name == 'tr': | ||
73 | break | ||
74 | cols = row.find_all('td') | ||
75 | flight_date, flight_num, flight_class, dep, arr, ac, meal = cols | ||
76 | flight_date = flight_date.get_text().strip() | ||
77 | flight_num = flight_num.get_text().strip() | ||
78 | flight_class = flight_class.get_text().strip() | ||
79 | |||
80 | dep_city, dep_code, dep_time = parse_dep_arr(flight_date, dep) | ||
81 | arr_city, arr_code, arr_time = parse_dep_arr(flight_date, arr) | ||
82 | self.log.debug("dep: %s %s %s", dep_city, dep_code, dep_time) | ||
83 | self.log.debug("arr: %s %s %s", arr_city, arr_code, arr_time) | ||
84 | |||
85 | cal = vobject.iCalendar() | ||
86 | event = cal.add('vevent') | ||
87 | event.add('dtstart').value = dep_time | ||
88 | event.add('dtend').value = arr_time | ||
89 | summary = "Flight from %s to %s" % (dep_code, arr_code) | ||
90 | event.add('summary').value = summary | ||
91 | text = inscriptis.get_text(str(soup)) | ||
92 | text = re.sub(r'([^ ]+)\s*\n', '\\1\n', text) | ||
93 | event.add('description').value = text | ||
94 | event.add('location').value = "%s airport" % (dep_code,) | ||
95 | uid = hashlib.sha1((str(dep_time) + summary).encode('utf8')).hexdigest() | ||
96 | event.add('uid').value = uid | ||
97 | events.append(cal) | ||
98 | index += 1 | ||
99 | return events | ||