fixing routing.py bug, if time is 24:00
[pywienerlinien] / gotovienna / routing.py
index e70b757..d82fb3f 100644 (file)
@@ -2,7 +2,8 @@
 # -*- coding: UTF-8 -*-
 
 from gotovienna.BeautifulSoup import BeautifulSoup, NavigableString
 # -*- coding: UTF-8 -*-
 
 from gotovienna.BeautifulSoup import BeautifulSoup, NavigableString
-from urllib2 import urlopen
+#from urllib2 import urlopen
+from UrlOpener import urlopen
 from urllib import urlencode
 from datetime import datetime, time, timedelta
 from textwrap import wrap
 from urllib import urlencode
 from datetime import datetime, time, timedelta
 from textwrap import wrap
@@ -36,7 +37,7 @@ def extract_city(station):
         return station.split(',')[-1].strip()
     else:
         return 'Wien'
         return station.split(',')[-1].strip()
     else:
         return 'Wien'
-        
+
 def extract_station(station):
     """ Remove city from string
     
 def extract_station(station):
     """ Remove city from string
     
@@ -47,7 +48,7 @@ def extract_station(station):
         return station[:station.rindex(',')].strip()
     else:
         return station
         return station[:station.rindex(',')].strip()
     else:
         return station
-    
+
 def split_station(station):
     """ >>> split_station('Karlsplatz, Wien')
     ('Karlsplatz', 'Wien')
 def split_station(station):
     """ >>> split_station('Karlsplatz, Wien')
     ('Karlsplatz', 'Wien')
@@ -99,7 +100,7 @@ def search(origin_tuple, destination_tuple, dtime=None):
 
     origin, origin_type = origin_tuple
     origin, origin_city = split_station(origin)
 
     origin, origin_type = origin_tuple
     origin, origin_city = split_station(origin)
-    
+
     destination, destination_type = destination_tuple
     destination, destination_city = split_station(destination)
 
     destination, destination_type = destination_tuple
     destination, destination_city = split_station(destination)
 
@@ -127,13 +128,7 @@ def search(origin_tuple, destination_tuple, dtime=None):
     post['place_destination'] = destination_city
     params = urlencode(post)
     url = '%s?%s' % (defaults.action, params)
     post['place_destination'] = destination_city
     params = urlencode(post)
     url = '%s?%s' % (defaults.action, params)
-
-    try:
-        f = open(DEBUGLOG, 'a')
-        f.write(url + '\n')
-        f.close()
-    except:
-        print 'Unable to write to DEBUGLOG: %s' % DEBUGLOG
+    #print url
 
     return urlopen(url)
 
 
     return urlopen(url)
 
@@ -161,27 +156,27 @@ class sParser:
         names_destination = self.soup.find('select', {'id': 'nameList_destination'})
         places_origin = self.soup.find('select', {'id': 'placeList_origin'})
         places_destination = self.soup.find('select', {'id': 'placeList_destination'})
         names_destination = self.soup.find('select', {'id': 'nameList_destination'})
         places_origin = self.soup.find('select', {'id': 'placeList_origin'})
         places_destination = self.soup.find('select', {'id': 'placeList_destination'})
-        
+
 
         if any([names_origin, names_destination, places_origin, places_destination]):
             dict = {}
 
         if any([names_origin, names_destination, places_origin, places_destination]):
             dict = {}
-            
+
             if names_origin:
             if names_origin:
-                dict['origin'] = map(lambda x: x.text, 
+                dict['origin'] = map(lambda x: x.text,
                                      names_origin.findAll('option'))
             if names_destination:
                                      names_origin.findAll('option'))
             if names_destination:
-                dict['destination'] = map(lambda x: x.text, 
+                dict['destination'] = map(lambda x: x.text,
                                           names_destination.findAll('option'))
                                           names_destination.findAll('option'))
-                
+
             if places_origin:
             if places_origin:
-                dict['place_origin'] = map(lambda x: x.text, 
+                dict['place_origin'] = map(lambda x: x.text,
                                            names_origin.findAll('option'))
             if names_destination:
                                            names_origin.findAll('option'))
             if names_destination:
-                dict['place_destination'] = map(lambda x: x.text, 
+                dict['place_destination'] = map(lambda x: x.text,
                                                 names_destination.findAll('option'))
                                                 names_destination.findAll('option'))
-    
+
             return dict
             return dict
-        
+
         else:
             raise ParserError('Unable to parse html')
 
         else:
             raise ParserError('Unable to parse html')
 
@@ -243,35 +238,42 @@ class rParser:
                     to_dtime = datetime.combine(d + timedelta(1), times[1])
                 else:
                     to_dtime = datetime.combine(d, times[1])
                     to_dtime = datetime.combine(d + timedelta(1), times[1])
                 else:
                     to_dtime = datetime.combine(d, times[1])
-                    
+
                 return [from_dtime, to_dtime]
                 return [from_dtime, to_dtime]
-            
+
             else:
                 dtregex = {'date' : '\d\d\.\d\d',
                            'time': '\d\d:\d\d'}
             else:
                 dtregex = {'date' : '\d\d\.\d\d',
                            'time': '\d\d:\d\d'}
-                
+
                 regex = "\s*(?P<date1>{date})?\s*(?P<time1>{time})\s*(?P<date2>{date})?\s*(?P<time2>{time})\s*".format(**dtregex)
                 ma = re.match(regex, y)
                 regex = "\s*(?P<date1>{date})?\s*(?P<time1>{time})\s*(?P<date2>{date})?\s*(?P<time2>{time})\s*".format(**dtregex)
                 ma = re.match(regex, y)
-                
+
                 if not ma:
                     return []
                 if not ma:
                     return []
-                
+
                 gr = ma.groupdict()
                 gr = ma.groupdict()
-                
+
                 def extract_datetime(gr, n):
                     if 'date%d' % n in gr and gr['date%d' % n]:
                 def extract_datetime(gr, n):
                     if 'date%d' % n in gr and gr['date%d' % n]:
+                        if gr['time%d' % n] == '24:00':
+                            gr['time%d' % n] = '0:00'
                         from_dtime = datetime.strptime(str(datetime.today().year) + gr['date%d' % n] + gr['time%d' % n], '%Y%d.%m.%H:%M')
                     else:
                         from_dtime = datetime.strptime(str(datetime.today().year) + gr['date%d' % n] + gr['time%d' % n], '%Y%d.%m.%H:%M')
                     else:
-                        t = datetime.strptime(gr['time%d' % n], '%H:%M').time()
                         d = datetime.today().date()
                         d = datetime.today().date()
+                        # Strange times possible at wienerlinien
+                        if gr['time%d' % n] == '24:00':
+                            gr['time%d' % n] = '0:00'
+                            d += timedelta(days=1)
+                        t = datetime.strptime(gr['time%d' % n], '%H:%M').time()
+                        
                         return datetime.combine(d, t)
                         return datetime.combine(d, t)
-                
+
                 # detail mode
                 from_dtime = extract_datetime(gr, 1)
                 to_dtime = extract_datetime(gr, 2)
                 # detail mode
                 from_dtime = extract_datetime(gr, 1)
                 to_dtime = extract_datetime(gr, 2)
-                
+
                 return [from_dtime, to_dtime]
                 return [from_dtime, to_dtime]
-                
+
         else:
             return []
 
         else:
             return []