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