summaryrefslogtreecommitdiff
path: root/email_assistant
diff options
context:
space:
mode:
Diffstat (limited to 'email_assistant')
-rw-r--r--email_assistant/assistant.py1
-rw-r--r--email_assistant/plugins/united.py64
2 files changed, 35 insertions, 30 deletions
diff --git a/email_assistant/assistant.py b/email_assistant/assistant.py
index d39b016..e6bfc66 100644
--- a/email_assistant/assistant.py
+++ b/email_assistant/assistant.py
@@ -198,6 +198,7 @@ class Assistant:
198 try: 198 try:
199 for p in self.plugins: 199 for p in self.plugins:
200 if p.match(msg): 200 if p.match(msg):
201 self.log.debug("Matched %s", p.name)
201 events = p.get_events(msg) 202 events = p.get_events(msg)
202 break 203 break
203 except Exception: 204 except Exception:
diff --git a/email_assistant/plugins/united.py b/email_assistant/plugins/united.py
index 38de3e2..0ed5dd8 100644
--- a/email_assistant/plugins/united.py
+++ b/email_assistant/plugins/united.py
@@ -31,28 +31,18 @@ import vobject
31from email_assistant import iata 31from email_assistant import iata
32from email_assistant import plugin 32from email_assistant import plugin
33 33
34def parse_dep_arr(flight_date, dep_arr): 34def parse_dep_arr(flight_date, flight_time, flight_loc):
35 flight_year = dateutil.parser.parse(flight_date).year 35 code = re.compile('^.*\((...)\)$').match(flight_loc).group(1)
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] 36 tz = iata.tzmap[code]
41 flight_time = flight_time.get_text().strip() 37 flight_time = dateutil.parser.parse(flight_date + ' ' + flight_time)
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)) 38 flight_time = flight_time.replace(tzinfo=dateutil.tz.gettz(tz))
49 return (city, code, flight_time) 39 return (code, flight_time)
50 40
51class Plugin(plugin.Plugin): 41class Plugin(plugin.Plugin):
52 name = 'united' 42 name = 'united'
53 43
54 def match(self, msg): 44 def match(self, msg):
55 if ('unitedairlines@united.com' in msg['From'] and 45 if ('Receipts@united.com' in msg['From'] and
56 'Itinerary and Receipt' in msg['Subject']): 46 'Itinerary and Receipt' in msg['Subject']):
57 return True 47 return True
58 48
@@ -61,32 +51,47 @@ class Plugin(plugin.Plugin):
61 for part in msg.walk(): 51 for part in msg.walk():
62 if part.get_content_type() == 'text/html': 52 if part.get_content_type() == 'text/html':
63 soup = BeautifulSoup(part.get_payload(decode=True), 'html.parser') 53 soup = BeautifulSoup(part.get_payload(decode=True), 'html.parser')
64 # confirmation_number = soup.find(class_="eTicketConfirmation").string
65 54
66 index = 0 55 index = 0
67 while True: 56 while True:
68 info = soup.find(id="ShowSegments_ShowSegment_ctl%02i_Flight" % index) 57 index += 1
58 info = soup.find(string=re.compile('Flight %s of' % index))
69 if not info: 59 if not info:
70 break 60 break
71 for row in info.parents: 61 while info.name != 'table': info = info.parent
72 if row.name == 'tr': 62
73 break 63 row = info.find('tr')
64 cols = row.find_all('td')
65 cols = [x.strip() for x in row.strings if x.strip()]
66 flight_num, flight_class = cols
67
68 row = row.nextSibling
69 cols = row.find_all('td')
70 cols = [x.strip() for x in row.strings if x.strip()]
71 dep_date, arr_date = cols
72
73 row = row.nextSibling
74 cols = row.find_all('td')
75 cols = [x.strip() for x in row.strings if x.strip()]
76 dep_time, arr_time = cols
77
78 row = row.nextSibling
74 cols = row.find_all('td') 79 cols = row.find_all('td')
75 flight_date, flight_num, flight_class, dep, arr, ac, meal = cols 80 cols = [x.strip() for x in row.strings if x.strip()]
76 flight_date = flight_date.get_text().strip() 81 dep_loc, arr_loc = cols
77 flight_num = flight_num.get_text().strip()
78 flight_class = flight_class.get_text().strip()
79 82
80 dep_city, dep_code, dep_time = parse_dep_arr(flight_date, dep) 83 flight_num = flight_num.split()[-1]
81 arr_city, arr_code, arr_time = parse_dep_arr(flight_date, arr) 84 dep_code, dep_time = parse_dep_arr(dep_date, dep_time, dep_loc)
82 self.log.debug("dep: %s %s %s", dep_city, dep_code, dep_time) 85 arr_code, arr_time = parse_dep_arr(arr_date, arr_time, arr_loc)
83 self.log.debug("arr: %s %s %s", arr_city, arr_code, arr_time) 86
87 self.log.debug("dep: %s %s", dep_code, dep_time)
88 self.log.debug("arr: %s %s", arr_code, arr_time)
84 89
85 cal = vobject.iCalendar() 90 cal = vobject.iCalendar()
86 event = cal.add('vevent') 91 event = cal.add('vevent')
87 event.add('dtstart').value = dep_time 92 event.add('dtstart').value = dep_time
88 event.add('dtend').value = arr_time 93 event.add('dtend').value = arr_time
89 summary = "Flight from %s to %s" % (dep_code, arr_code) 94 summary = "Flight %s from %s to %s" % (flight_num, dep_code, arr_code)
90 event.add('summary').value = summary 95 event.add('summary').value = summary
91 text = inscriptis.get_text(str(soup)) 96 text = inscriptis.get_text(str(soup))
92 text = re.sub(r'([^ ]+)\s*\n', '\\1\n', text) 97 text = re.sub(r'([^ ]+)\s*\n', '\\1\n', text)
@@ -95,5 +100,4 @@ class Plugin(plugin.Plugin):
95 uid = hashlib.sha1((str(dep_time) + summary).encode('utf8')).hexdigest() 100 uid = hashlib.sha1((str(dep_time) + summary).encode('utf8')).hexdigest()
96 event.add('uid').value = uid 101 event.add('uid').value = uid
97 events.append(cal) 102 events.append(cal)
98 index += 1
99 return events 103 return events