diff options
Diffstat (limited to 'email_assistant/plugins')
| -rw-r--r-- | email_assistant/plugins/__init__.py | 29 | ||||
| -rw-r--r-- | email_assistant/plugins/eventbrite.py | 90 | ||||
| -rw-r--r-- | email_assistant/plugins/general.py | 58 | ||||
| -rw-r--r-- | email_assistant/plugins/marriott.py | 67 | ||||
| -rw-r--r-- | email_assistant/plugins/united.py | 99 |
5 files changed, 343 insertions, 0 deletions
diff --git a/email_assistant/plugins/__init__.py b/email_assistant/plugins/__init__.py new file mode 100644 index 0000000..10b28f0 --- /dev/null +++ b/email_assistant/plugins/__init__.py | |||
| @@ -0,0 +1,29 @@ | |||
| 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 | from . import eventbrite | ||
| 22 | from . import marriott | ||
| 23 | from . import united | ||
| 24 | |||
| 25 | plugins = [ | ||
| 26 | eventbrite.Plugin, | ||
| 27 | marriott.Plugin, | ||
| 28 | united.Plugin, | ||
| 29 | ] | ||
diff --git a/email_assistant/plugins/eventbrite.py b/email_assistant/plugins/eventbrite.py new file mode 100644 index 0000000..9ef073a --- /dev/null +++ b/email_assistant/plugins/eventbrite.py | |||
| @@ -0,0 +1,90 @@ | |||
| 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 | import datetime | ||
| 25 | import json | ||
| 26 | |||
| 27 | from bs4 import BeautifulSoup | ||
| 28 | import dateutil.parser | ||
| 29 | import dateutil.tz | ||
| 30 | import inscriptis | ||
| 31 | import vobject | ||
| 32 | |||
| 33 | from email_assistant import plugin | ||
| 34 | |||
| 35 | class Plugin(plugin.Plugin): | ||
| 36 | name = 'eventbrite' | ||
| 37 | |||
| 38 | def match(self, msg): | ||
| 39 | if ('orders@eventbrite.com' in msg['From'] and | ||
| 40 | 'Your Tickets for' in msg['Subject']): | ||
| 41 | return True | ||
| 42 | |||
| 43 | def get_events(self, msg): | ||
| 44 | events = [] | ||
| 45 | for part in msg.walk(): | ||
| 46 | if part.get_content_type() == 'text/html': | ||
| 47 | soup = BeautifulSoup(part.get_payload(decode=True).decode('utf8'), 'html.parser') | ||
| 48 | data = json.loads(soup.find('script', type="application/ld+json").string) | ||
| 49 | |||
| 50 | address = data['reservationFor']['location']['address'] | ||
| 51 | location = ' '.join((address['streetAddress'], | ||
| 52 | address['addressLocality'], | ||
| 53 | address['addressRegion'], | ||
| 54 | address['postalCode'], | ||
| 55 | address['addressCountry'])) | ||
| 56 | summary = data['reservationFor']['name'] | ||
| 57 | |||
| 58 | start = dateutil.parser.parse(data['reservationFor']['startDate']) | ||
| 59 | end = dateutil.parser.parse(data['reservationFor']['endDate']) | ||
| 60 | if start.date() != end.date(): | ||
| 61 | # If it is a multi-day event, don't schedule a time. | ||
| 62 | start = start.date() | ||
| 63 | end = end.date()+datetime.timedelta(days=1) | ||
| 64 | else: | ||
| 65 | # We do have a TZ offset, and could schedule this as a | ||
| 66 | # UTC event. It would generally display correctly, | ||
| 67 | # but it would make it difficult to view the event | ||
| 68 | # when the viewer is in a different timezone (as they | ||
| 69 | # would have to mentally perform the UTC conversion, | ||
| 70 | # instead of being able to see the event's own "local" | ||
| 71 | # time. To mitigate this, perform a location lookup. | ||
| 72 | tzinfo = self.assistant.get_tzinfo(location) | ||
| 73 | if tzinfo is not None: | ||
| 74 | start = start.replace(tzinfo=tzinfo) | ||
| 75 | end = end.replace(tzinfo=tzinfo) | ||
| 76 | |||
| 77 | cal = vobject.iCalendar() | ||
| 78 | event = cal.add('vevent') | ||
| 79 | event.add('dtstart').value = start | ||
| 80 | event.add('dtend').value = end | ||
| 81 | event.add('summary').value = summary | ||
| 82 | text = inscriptis.get_text(str(soup)) | ||
| 83 | text = re.sub(r'([^ ]+)\s*\n', '\\1\n', text) | ||
| 84 | event.add('description').value = text | ||
| 85 | event.add('location').value = location | ||
| 86 | uid = hashlib.sha1((str(start) + summary).encode('utf8')).hexdigest() | ||
| 87 | event.add('uid').value = uid | ||
| 88 | events.append(cal) | ||
| 89 | |||
| 90 | return events | ||
diff --git a/email_assistant/plugins/general.py b/email_assistant/plugins/general.py new file mode 100644 index 0000000..c0517b8 --- /dev/null +++ b/email_assistant/plugins/general.py | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | |||
| 2 | # An unused sketch of a generalized plugin that uses commonregex to | ||
| 3 | # find dates and addresses. | ||
| 4 | |||
| 5 | import re | ||
| 6 | import logging | ||
| 7 | import hashlib | ||
| 8 | import datetime | ||
| 9 | |||
| 10 | from bs4 import BeautifulSoup | ||
| 11 | import commonregex | ||
| 12 | import dateutil.parser | ||
| 13 | import dateutil.tz | ||
| 14 | import inscriptis | ||
| 15 | import vobject | ||
| 16 | |||
| 17 | def match(msg): | ||
| 18 | if ('reservations@res-marriott.com' in msg['From'] and | ||
| 19 | 'Reservation Confirmation' in msg['Subject']): | ||
| 20 | return True | ||
| 21 | |||
| 22 | def get_events(msg): | ||
| 23 | events = [] | ||
| 24 | log = logging.getLogger('assistant.marriott') | ||
| 25 | for part in msg.walk(): | ||
| 26 | if part.get_content_type() == 'text/html': | ||
| 27 | soup = BeautifulSoup(part.get_payload(decode=True).decode('utf8'), 'html.parser') | ||
| 28 | |||
| 29 | start = end = location = None | ||
| 30 | for element in soup.descendants: | ||
| 31 | if not hasattr(element, 'children') and element.string: | ||
| 32 | text = element.string.strip() | ||
| 33 | if not text: | ||
| 34 | continue | ||
| 35 | found = commonregex.CommonRegex(text) | ||
| 36 | for date in found.dates: | ||
| 37 | if not start: | ||
| 38 | start = dateutil.parser.parse(date).date() | ||
| 39 | continue | ||
| 40 | if not end: | ||
| 41 | end = dateutil.parser.parse(date).date()+datetime.timedelta(days=1) | ||
| 42 | if found.street_addresses and not location: | ||
| 43 | location = text | ||
| 44 | if not (start and end and location): | ||
| 45 | continue | ||
| 46 | cal = vobject.iCalendar() | ||
| 47 | event = cal.add('vevent') | ||
| 48 | event.add('dtstart').value = start | ||
| 49 | event.add('dtend').value = end | ||
| 50 | summary = re.match('Reservation Confirmation .*? for (.*)', msg['Subject']).group(1).strip() | ||
| 51 | event.add('summary').value = summary | ||
| 52 | event.add('description').value = inscriptis.get_text(str(soup)) | ||
| 53 | event.add('location').value = location | ||
| 54 | uid = hashlib.sha1((str(start) + summary).encode('utf8')).hexdigest() | ||
| 55 | event.add('uid').value = uid | ||
| 56 | events.append(cal) | ||
| 57 | print(repr(cal.serialize())) | ||
| 58 | return events | ||
diff --git a/email_assistant/plugins/marriott.py b/email_assistant/plugins/marriott.py new file mode 100644 index 0000000..52597cd --- /dev/null +++ b/email_assistant/plugins/marriott.py | |||
| @@ -0,0 +1,67 @@ | |||
| 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 | import datetime | ||
| 25 | |||
| 26 | from bs4 import BeautifulSoup | ||
| 27 | import dateutil.parser | ||
| 28 | import dateutil.tz | ||
| 29 | import inscriptis | ||
| 30 | import vobject | ||
| 31 | |||
| 32 | from email_assistant import plugin | ||
| 33 | |||
| 34 | class Plugin(plugin.Plugin): | ||
| 35 | name = 'marriott' | ||
| 36 | |||
| 37 | def match(self, msg): | ||
| 38 | if ('reservations@res-marriott.com' in msg['From'] and | ||
| 39 | 'Reservation Confirmation' in msg['Subject']): | ||
| 40 | return True | ||
| 41 | |||
| 42 | def get_events(self, msg): | ||
| 43 | events = [] | ||
| 44 | for part in msg.walk(): | ||
| 45 | if part.get_content_type() == 'text/html': | ||
| 46 | soup = BeautifulSoup(part.get_payload(decode=True).decode('utf8'), 'html.parser') | ||
| 47 | summary = soup.find_all('table')[7].a.string.strip() | ||
| 48 | location = soup.find_all('table')[9].a.string.strip() | ||
| 49 | start = (soup.find('th', string=re.compile('Check-In:')). | ||
| 50 | find_next_sibling('th').string.strip()) | ||
| 51 | end = (soup.find('td', string=re.compile('Check-Out:')). | ||
| 52 | find_next_sibling('td').string.strip()) | ||
| 53 | start = dateutil.parser.parse(start).date() | ||
| 54 | end = dateutil.parser.parse(end).date()+datetime.timedelta(days=1) | ||
| 55 | cal = vobject.iCalendar() | ||
| 56 | event = cal.add('vevent') | ||
| 57 | event.add('dtstart').value = start | ||
| 58 | event.add('dtend').value = end | ||
| 59 | event.add('summary').value = summary | ||
| 60 | text = inscriptis.get_text(str(soup)) | ||
| 61 | text = re.sub(r'([^ ]+)\s*\n', '\\1\n', text) | ||
| 62 | event.add('description').value = text | ||
| 63 | event.add('location').value = location | ||
| 64 | uid = hashlib.sha1((str(start) + summary).encode('utf8')).hexdigest() | ||
| 65 | event.add('uid').value = uid | ||
| 66 | events.append(cal) | ||
| 67 | return events | ||
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 | ||
