From c44874cfa48bf00b83057033fc7800b35e157702 Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Sat, 8 Jun 2019 14:19:35 -0700 Subject: Initial commit --- email_assistant/plugins/__init__.py | 29 ++++++++++ email_assistant/plugins/eventbrite.py | 90 +++++++++++++++++++++++++++++++ email_assistant/plugins/general.py | 58 ++++++++++++++++++++ email_assistant/plugins/marriott.py | 67 ++++++++++++++++++++++++ email_assistant/plugins/united.py | 99 +++++++++++++++++++++++++++++++++++ 5 files changed, 343 insertions(+) create mode 100644 email_assistant/plugins/__init__.py create mode 100644 email_assistant/plugins/eventbrite.py create mode 100644 email_assistant/plugins/general.py create mode 100644 email_assistant/plugins/marriott.py create mode 100644 email_assistant/plugins/united.py (limited to 'email_assistant/plugins') 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 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2019 James E. Blair +# +# This file is part of Email-assistant. +# +# Email-assistant is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License +# as published by the Free Software Foundation, either version 3 of +# the License, or (at your option) any later version. +# +# Email-assistant is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Email-assistant. If not, see +# . + +from . import eventbrite +from . import marriott +from . import united + +plugins = [ + eventbrite.Plugin, + marriott.Plugin, + united.Plugin, +] 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 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2019 James E. Blair +# +# This file is part of Email-assistant. +# +# Email-assistant is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License +# as published by the Free Software Foundation, either version 3 of +# the License, or (at your option) any later version. +# +# Email-assistant is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Email-assistant. If not, see +# . + +import re +import logging +import hashlib +import datetime +import json + +from bs4 import BeautifulSoup +import dateutil.parser +import dateutil.tz +import inscriptis +import vobject + +from email_assistant import plugin + +class Plugin(plugin.Plugin): + name = 'eventbrite' + + def match(self, msg): + if ('orders@eventbrite.com' in msg['From'] and + 'Your Tickets for' in msg['Subject']): + return True + + def get_events(self, msg): + events = [] + for part in msg.walk(): + if part.get_content_type() == 'text/html': + soup = BeautifulSoup(part.get_payload(decode=True).decode('utf8'), 'html.parser') + data = json.loads(soup.find('script', type="application/ld+json").string) + + address = data['reservationFor']['location']['address'] + location = ' '.join((address['streetAddress'], + address['addressLocality'], + address['addressRegion'], + address['postalCode'], + address['addressCountry'])) + summary = data['reservationFor']['name'] + + start = dateutil.parser.parse(data['reservationFor']['startDate']) + end = dateutil.parser.parse(data['reservationFor']['endDate']) + if start.date() != end.date(): + # If it is a multi-day event, don't schedule a time. + start = start.date() + end = end.date()+datetime.timedelta(days=1) + else: + # We do have a TZ offset, and could schedule this as a + # UTC event. It would generally display correctly, + # but it would make it difficult to view the event + # when the viewer is in a different timezone (as they + # would have to mentally perform the UTC conversion, + # instead of being able to see the event's own "local" + # time. To mitigate this, perform a location lookup. + tzinfo = self.assistant.get_tzinfo(location) + if tzinfo is not None: + start = start.replace(tzinfo=tzinfo) + end = end.replace(tzinfo=tzinfo) + + cal = vobject.iCalendar() + event = cal.add('vevent') + event.add('dtstart').value = start + event.add('dtend').value = end + event.add('summary').value = summary + text = inscriptis.get_text(str(soup)) + text = re.sub(r'([^ ]+)\s*\n', '\\1\n', text) + event.add('description').value = text + event.add('location').value = location + uid = hashlib.sha1((str(start) + summary).encode('utf8')).hexdigest() + event.add('uid').value = uid + events.append(cal) + + 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 @@ + +# An unused sketch of a generalized plugin that uses commonregex to +# find dates and addresses. + +import re +import logging +import hashlib +import datetime + +from bs4 import BeautifulSoup +import commonregex +import dateutil.parser +import dateutil.tz +import inscriptis +import vobject + +def match(msg): + if ('reservations@res-marriott.com' in msg['From'] and + 'Reservation Confirmation' in msg['Subject']): + return True + +def get_events(msg): + events = [] + log = logging.getLogger('assistant.marriott') + for part in msg.walk(): + if part.get_content_type() == 'text/html': + soup = BeautifulSoup(part.get_payload(decode=True).decode('utf8'), 'html.parser') + + start = end = location = None + for element in soup.descendants: + if not hasattr(element, 'children') and element.string: + text = element.string.strip() + if not text: + continue + found = commonregex.CommonRegex(text) + for date in found.dates: + if not start: + start = dateutil.parser.parse(date).date() + continue + if not end: + end = dateutil.parser.parse(date).date()+datetime.timedelta(days=1) + if found.street_addresses and not location: + location = text + if not (start and end and location): + continue + cal = vobject.iCalendar() + event = cal.add('vevent') + event.add('dtstart').value = start + event.add('dtend').value = end + summary = re.match('Reservation Confirmation .*? for (.*)', msg['Subject']).group(1).strip() + event.add('summary').value = summary + event.add('description').value = inscriptis.get_text(str(soup)) + event.add('location').value = location + uid = hashlib.sha1((str(start) + summary).encode('utf8')).hexdigest() + event.add('uid').value = uid + events.append(cal) + print(repr(cal.serialize())) + 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 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2019 James E. Blair +# +# This file is part of Email-assistant. +# +# Email-assistant is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License +# as published by the Free Software Foundation, either version 3 of +# the License, or (at your option) any later version. +# +# Email-assistant is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Email-assistant. If not, see +# . + +import re +import logging +import hashlib +import datetime + +from bs4 import BeautifulSoup +import dateutil.parser +import dateutil.tz +import inscriptis +import vobject + +from email_assistant import plugin + +class Plugin(plugin.Plugin): + name = 'marriott' + + def match(self, msg): + if ('reservations@res-marriott.com' in msg['From'] and + 'Reservation Confirmation' in msg['Subject']): + return True + + def get_events(self, msg): + events = [] + for part in msg.walk(): + if part.get_content_type() == 'text/html': + soup = BeautifulSoup(part.get_payload(decode=True).decode('utf8'), 'html.parser') + summary = soup.find_all('table')[7].a.string.strip() + location = soup.find_all('table')[9].a.string.strip() + start = (soup.find('th', string=re.compile('Check-In:')). + find_next_sibling('th').string.strip()) + end = (soup.find('td', string=re.compile('Check-Out:')). + find_next_sibling('td').string.strip()) + start = dateutil.parser.parse(start).date() + end = dateutil.parser.parse(end).date()+datetime.timedelta(days=1) + cal = vobject.iCalendar() + event = cal.add('vevent') + event.add('dtstart').value = start + event.add('dtend').value = end + event.add('summary').value = summary + text = inscriptis.get_text(str(soup)) + text = re.sub(r'([^ ]+)\s*\n', '\\1\n', text) + event.add('description').value = text + event.add('location').value = location + uid = hashlib.sha1((str(start) + summary).encode('utf8')).hexdigest() + event.add('uid').value = uid + events.append(cal) + 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 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2019 James E. Blair +# +# This file is part of Email-assistant. +# +# Email-assistant is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License +# as published by the Free Software Foundation, either version 3 of +# the License, or (at your option) any later version. +# +# Email-assistant is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Email-assistant. If not, see +# . + +import re +import logging +import hashlib + +from bs4 import BeautifulSoup +import dateutil.parser +import dateutil.tz +import inscriptis +import vobject + +from email_assistant import iata +from email_assistant import plugin + +def parse_dep_arr(flight_date, dep_arr): + flight_year = dateutil.parser.parse(flight_date).year + city, br, code, flight_time = dep_arr.span.children + city = city.strip() + code = code.strip()[1:-1] + code = code.split()[0] + tz = iata.tzmap[code] + flight_time = flight_time.get_text().strip() + m = re.match(r'(.*) \((\d+[A-Z]+)\)', flight_time) + if m: + s = '%s%s %s' % (m.group(2), flight_year, m.group(1)) + flight_time = dateutil.parser.parse(s) + else: + flight_time = dateutil.parser.parse(flight_date +' '+ flight_time) + flight_time = flight_time.replace(tzinfo=dateutil.tz.gettz(tz)) + return (city, code, flight_time) + +class Plugin(plugin.Plugin): + name = 'united' + + def match(self, msg): + if ('unitedairlines@united.com' in msg['From'] and + 'Itinerary and Receipt' in msg['Subject']): + return True + + def get_events(self, msg): + events = [] + for part in msg.walk(): + if part.get_content_type() == 'text/html': + soup = BeautifulSoup(part.get_payload(decode=True).decode('utf8'), 'html.parser') + # confirmation_number = soup.find(class_="eTicketConfirmation").string + + index = 0 + while True: + info = soup.find(id="ShowSegments_ShowSegment_ctl%02i_Flight" % index) + if not info: + break + for row in info.parents: + if row.name == 'tr': + break + cols = row.find_all('td') + flight_date, flight_num, flight_class, dep, arr, ac, meal = cols + flight_date = flight_date.get_text().strip() + flight_num = flight_num.get_text().strip() + flight_class = flight_class.get_text().strip() + + dep_city, dep_code, dep_time = parse_dep_arr(flight_date, dep) + arr_city, arr_code, arr_time = parse_dep_arr(flight_date, arr) + self.log.debug("dep: %s %s %s", dep_city, dep_code, dep_time) + self.log.debug("arr: %s %s %s", arr_city, arr_code, arr_time) + + cal = vobject.iCalendar() + event = cal.add('vevent') + event.add('dtstart').value = dep_time + event.add('dtend').value = arr_time + summary = "Flight from %s to %s" % (dep_code, arr_code) + event.add('summary').value = summary + text = inscriptis.get_text(str(soup)) + text = re.sub(r'([^ ]+)\s*\n', '\\1\n', text) + event.add('description').value = text + event.add('location').value = "%s airport" % (dep_code,) + uid = hashlib.sha1((str(dep_time) + summary).encode('utf8')).hexdigest() + event.add('uid').value = uid + events.append(cal) + index += 1 + return events -- cgit v1.2.3