combined date and time, renamed to timespan
authorFlorian Schweikert <kelvan@logic.at>
Thu, 6 Oct 2011 20:26:01 +0000 (22:26 +0200)
committerFlorian Schweikert <kelvan@logic.at>
Thu, 6 Oct 2011 20:26:01 +0000 (22:26 +0200)
removed duration (use diff of timespan)

gotovienna/routing.py
scotty

index 6a89dee..279f70a 100644 (file)
@@ -4,11 +4,12 @@
 from BeautifulSoup import BeautifulSoup, NavigableString
 from urllib2 import urlopen
 from urllib import urlencode
-from datetime import datetime, time, combine, timedelta
+from datetime import datetime, time, timedelta
 from textwrap import wrap
 import argparse
 import sys
 import os.path
+import re
 
 from gotovienna import defaults
 
@@ -192,27 +193,44 @@ class rParser:
                 # overview mode
                 times = map(lambda z: time(*map(int, z.split(':'))), y.split('-'))
                 d = rParser.get_date(x)
-                from_dtime = combine(d, times[0])
+                from_dtime = datetime.combine(d, times[0])
                 if times[0] > times[1]:
                     # dateline crossing
-                    to_dtime = combine(d + timedelta(1), times[1])
+                    to_dtime = datetime.combine(d + timedelta(1), times[1])
                 else:
-                    to_dtime = combine(d, times[1])
-                return datetimes
+                    to_dtime = datetime.combine(d, times[1])
+                    
+                return [from_dtime, to_dtime]
+            
             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)
+                
+                if not ma:
+                    return []
+                
+                gr = ma.groupdict()
+                
+                def extract_datetime(gr, n):
+                    if 'date%d' % n in gr and gr['date%d' % n]:
+                        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()
+                        return datetime.combine(d, t)
+                
                 # detail mode
-                return map(lambda z: time(*map(int, z.split(':'))), wrap(y, 5))
+                from_dtime = extract_datetime(gr, 1)
+                to_dtime = extract_datetime(gr, 2)
+                
+                return [from_dtime, to_dtime]
+                
         else:
             return []
 
-    @classmethod
-    def get_duration(cls, x):
-        y = rParser.get_tdtext(x, 'col_duration')
-        if y:
-            return time(*map(int, y.split(":")))
-        else:
-            return None
-
     def __iter__(self):
         for detail in self.details():
             yield detail
@@ -221,7 +239,7 @@ class rParser:
         tours = self.soup.findAll('div', {'class': 'data_table tourdetail'})
 
         trips = map(lambda x: map(lambda y: {
-                        'time': rParser.get_time(y),
+                        'timespan': rParser.get_datetime(y),
                         'station': map(lambda z: z[2:].strip(),
                                        filter(lambda x: type(x) == NavigableString, y.find('td', {'class': 'col_station'}).contents)), # filter non NaviStrings
                         'info': map(lambda x: x.strip(),
@@ -256,8 +274,7 @@ class rParser:
             rows = table.findAll('tr')[1:] # cut off headline
 
             overview = map(lambda x: {
-                               'time': rParser.get_datetime(x),
-                               'duration': rParser.get_duration(x), # grab duration
+                               'timespan': rParser.get_datetime(x),
                                'change': rParser.get_change(x),
                                'price': rParser.get_price(x),
                            },
diff --git a/scotty b/scotty
index bfc0e82..cf5086b 100755 (executable)
--- a/scotty
+++ b/scotty
@@ -107,15 +107,17 @@ while not finished:
             l = ''
             while not l == 'q':
                 for idx, overview in enumerate(overviews):
-                    if not overview['date'] or not overview['time']:
+                    timespan = overview['timespan']
+                    if not timespan:
                         # XXX: Bogus data for e.g. Pilgramgasse->Karlsplatz?!
                         continue
-    
-                    print '%d. [%s] %s-%s (%s)' % (idx + 1,
-                            overview['date'],
-                            overview['time'][0],
-                            overview['time'][1],
-                            overview['duration'])
+                    
+                    str_timespan = timespan[0].strftime('[%y-%d-%m] %H:%M')
+                    str_timespan += '-' + timespan[1].strftime('%H:%M')
+                    timedelta = timespan[1] - timespan[0]
+                    print '%d. %s (%s)' % (idx + 1,
+                            str_timespan,
+                            timedelta)
                 print 'q. Quit'
                 l = sys.stdin.readline().strip()
                 print
@@ -123,8 +125,8 @@ while not finished:
     
                 if l.isdigit() and int(l) <= len(details):
                     for detail in details[int(l) - 1]:
-                        if detail['time'] and detail['station']:
-                            time = '%s - %s' % (detail['time'][0].strftime(TIMEFORMAT), detail['time'][1].strftime(TIMEFORMAT))
+                        if detail['timespan'] and detail['station']:
+                            time = '%s - %s' % (detail['timespan'][0].strftime(TIMEFORMAT), detail['timespan'][1].strftime(TIMEFORMAT))
                             print '[%s] %s\n%s' % (time, ' -> '.join(detail['station']), '\n'.join(detail['info']))
                         else:
                             print '\n'.join(detail['info'])