Browse Source

added support for wienerlinien semester ticket

Fabian Peter Hammerle 7 years ago
parent
commit
8a1d93872d
5 changed files with 34 additions and 6 deletions
  1. 9 0
      dingguo/__init__.py
  2. 2 0
      dingguo/parser/__init__.py
  3. 15 5
      dingguo/parser/wienerlinien.py
  4. 1 0
      tests/test_parser.py
  5. 7 1
      tests/test_yaml.py

+ 9 - 0
dingguo/__init__.py

@@ -373,7 +373,9 @@ class Transportation(Item):
             distance = None,
             passenger = None,
             route_map = None,
+            ticket_url = None,
             valid_from = None,
+            valid_until = None,
             **kwargs
             ):
         super(Transportation, self).__init__(**kwargs)
@@ -396,6 +398,13 @@ class Transportation(Item):
             assert type(valid_from) is datetime.datetime
             assert not valid_from.tzinfo is None
             self.valid_from = valid_from
+        if valid_until is not None:
+            assert type(valid_until) is datetime.datetime
+            assert not valid_until.tzinfo is None
+            self.valid_until = valid_until
+        if ticket_url is not None:
+            assert type(ticket_url) is unicode
+            self.ticket_url = ticket_url
 
     def dict_repr(self):
         attr = super(Transportation, self).dict_repr()

+ 2 - 0
dingguo/parser/__init__.py

@@ -13,6 +13,7 @@ import mytaxi
 import oebb
 import thomann
 import uber
+import wienerlinien
 import yipbee
 
 order_confirmation_parsers = [
@@ -25,6 +26,7 @@ order_confirmation_parsers = [
     oebb.parse_order_confirmation_mail,
     thomann.parse_order_confirmation_mail,
     uber.parse_order_confirmation_mail,
+    wienerlinien.parse_order_confirmation_mail,
     yipbee.parse_order_confirmation_mail,
     ]
 

+ 15 - 5
dingguo/parser/wienerlinien.py

@@ -31,13 +31,14 @@ def parse_order_confirmation_mail(mail):
         raise Exception()
 
     order_match = re.search(
-        r'(?P<name>Single-Ticket)\s+'
+        r'(?P<name>.*Ticket.*)\s+'
             + r'Valid from: (?P<valid_from>\d{1,2} \w+ \d{4} \d{1,2}:\d{2})\s+'
-            + r'Type: (?P<type>Mobile ticket)\s+'
+            + r'(Valid until: (?P<valid_until>\d{1,2} \w+ \d{4} \d{1,2}:\d{2})\s+)?'
+            + r'Type: (?P<type>.* ticket)\s+'
             + r'For\:\s+'
             + r'(?P<passenger_first_name>Fabian Peter)\s+'
             + r'(?P<passenger_last_name>Hammerle)\s+'
-            + ur'Price: (?P<currency>€)(?P<price>2.80)\s+'
+            + ur'Price: (?P<currency>€)(?P<price>\d+.\d{2})\s+'
             + r'Quantity: (?P<quantity>1)\s+'
             ,
         msg_text,
@@ -61,8 +62,14 @@ def parse_order_confirmation_mail(mail):
 
     assert int(order_match_groups['quantity']) == 1
 
+    ticket_url_match = re.search(
+        ur'following link:\s+(http[^\s]+)',
+        msg_text,
+        re.MULTILINE | re.UNICODE,
+        )
+
     with ioex.setlocale('en_US.UTF-8'):
-        import pytz
+        timezone = UtcOffsetTimezone(email.utils.parsedate_tz(mail['date'])[9])
         order.items.append(dingguo.Transportation(
             name = order_match_groups['name'],
             price_brutto = dingguo.Sum(
@@ -74,7 +81,10 @@ def parse_order_confirmation_mail(mail):
                 last_name = order_match_groups['passenger_last_name'],
                 ),
             valid_from = datetime.datetime.strptime(order_match_groups['valid_from'], '%d %B %Y %H:%M')
-                .replace(tzinfo = UtcOffsetTimezone(email.utils.parsedate_tz(mail['date'])[9]))
+                .replace(tzinfo = timezone),
+            valid_until = datetime.datetime.strptime(order_match_groups['valid_until'], '%d %B %Y %H:%M')
+                .replace(tzinfo = timezone) if order_match_groups['valid_until'] else None,
+            ticket_url = ticket_url_match.group(1) if ticket_url_match else None,
             ))
 
     return [order]

+ 1 - 0
tests/test_parser.py

@@ -19,6 +19,7 @@ test_data_path = os.path.join(project_root_path, 'tests', 'data')
     ('oebb', os.path.join(test_data_path, 'oebb', 'mail_1.eml')),
     ('thomann', os.path.join(test_data_path, 'thomann', '1.eml')),
     ('uber', os.path.join(test_data_path, 'uber', 'mail_1.eml')),
+    ('wiener_linien', os.path.join(test_data_path, 'wienerlinien', 'mail_single_ticket.eml')),
     ('yipbee', os.path.join(test_data_path, 'yipbee', 'mail_1.eml')),
     ])
 def test_parse_confirmation_mail_platform(platform, mail_path):

+ 7 - 1
tests/test_yaml.py

@@ -70,7 +70,9 @@ def get_transportation():
             destination_point = u'city',
             distance = dingguo.Distance(3.21, u'km'),
             passenger = get_person_a(),
-            valid_from = datetime.datetime(2016, 7, 14, 13, 50, 4, 0, tzinfo = pytz.timezone('Europe/Vienna')), 
+            valid_from = datetime.datetime(2016, 7, 14, 13, 50, 4, 0, tzinfo = pytz.timezone('Europe/Vienna')),
+            valid_until = datetime.datetime(2016, 7, 14, 18, 50, 4, 0, tzinfo = pytz.utc),
+            ticket_url = u'https://www.example.com',
             )
 
 def get_taxi_ride():
@@ -211,7 +213,9 @@ passenger: !person
   first_name: Fabian Peter
   last_name: Hammerle
 price_brutto: !sum '1.23 EUR'
+ticket_url: https://www.example.com
 valid_from: 2016-07-14 13:50:04+01:05
+valid_until: 2016-07-14 18:50:04+00:00
 """],
     [get_taxi_ride(), u"""!taxi-ride
 arrival_time: 2016-05-02 18:10:00+01:05
@@ -293,7 +297,9 @@ route_map: null
 passenger: !person
   first_name: Fabian Peter
   last_name: Hammerle
+ticket_url: https://www.example.com
 valid_from: 2016-07-14 13:50:04+01:05
+valid_until: 2016-07-14 18:50:04+00:00
 """],
     ])
 def test_from_yaml(expected_object, source_yaml):