summaryrefslogtreecommitdiff
path: root/email_assistant/plugins/general.py
diff options
context:
space:
mode:
Diffstat (limited to 'email_assistant/plugins/general.py')
-rw-r--r--email_assistant/plugins/general.py58
1 files changed, 58 insertions, 0 deletions
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
5import re
6import logging
7import hashlib
8import datetime
9
10from bs4 import BeautifulSoup
11import commonregex
12import dateutil.parser
13import dateutil.tz
14import inscriptis
15import vobject
16
17def match(msg):
18 if ('reservations@res-marriott.com' in msg['From'] and
19 'Reservation Confirmation' in msg['Subject']):
20 return True
21
22def 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