diff options
Diffstat (limited to 'email_assistant/plugins/marriott.py')
-rw-r--r-- | email_assistant/plugins/marriott.py | 67 |
1 files changed, 67 insertions, 0 deletions
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 | ||