"""
if not self._stations.has_key(name):
st = {}
-
+
if not self.lines.has_key(name):
return None
-
+
bs = BeautifulSoup(urlopen(self.lines[name]))
tables = bs.findAll('table', {'class': 'text_10pix'})
for i in range(2):
dir = tables[i].div.contents[-1].strip(' ')
-
+
sta = []
for tr in tables[i].findAll('tr', {'onmouseout': 'obj_unhighlight(this);'}):
if tr.a:
sta.append((tr.a.text, settings.line_overview + tr.a['href']))
else:
sta.append((tr.text.strip(' '), None))
-
+
st[dir] = sta
self._stations[name] = st
bs = BeautifulSoup(urlopen(settings.line_overview))
# get tables
lines = bs.findAll('td', {'class': 'linie'})
-
+
for line in lines:
if line.a:
href = settings.line_overview + line.a['href']
self._lines[line.text] = href
elif line.img:
self._lines[line.img['alt']] = href
-
+
return self._lines
def get_departures(self, url):
integer if time until next departure
time if time of next departure
"""
-
+
#TODO parse line name and direction for station site parsing
-
+
if not url:
# FIXME prevent from calling this method with None
return []
bs = BeautifulSoup(urlopen(url))
result_lines = bs.findAll('table')[-1].findAll('tr')
-
+
dep = []
for tr in result_lines[1:]:
th = tr.findAll('th')
#TODO replace with logger
print "[DEBUG] Unable to find th in:\n%s" % str(tr)
continue
-
+
# parse time
time = th[-2].text.split(' ')
if len(time) < 2:
print 'Invalid time: %s' % time
continue
-
+
time = time[1]
-
+
if time.find('rze...') >= 0:
dep.append(0)
elif time.isdigit():
# Unexpected content
#TODO replace with logger
print "[DEBUG] Invalid data:\n%s" % time
-
+
return dep
DEBUGLOG = os.path.expanduser('~/gotoVienna.debug')
class ParserError(Exception):
-
+
def __init__(self, msg='Parser error'):
self.message = msg
class PageType:
UNKNOWN, CORRECTION, RESULT = range(3)
-
+
def search(origin_tuple, destination_tuple, dtime=None):
""" build route request
"""
if not dtime:
dtime = datetime.now()
-
+
origin, origin_type = origin_tuple
destination, destination_type = destination_tuple
if not origin_type in POSITION_TYPES or\
not destination_type in POSITION_TYPES:
raise ParserError('Invalid position type')
-
+
post = settings.search_post
post['name_origin'] = origin
post['type_origin'] = origin_type
post['itdTime'] = dtime.strftime('%H:%M')
params = urlencode(post)
url = '%s?%s' % (settings.action, params)
-
+
try:
f = open(DEBUGLOG, 'a')
f.write(url + '\n')
f.close()
except:
print 'Unable to write to DEBUGLOG: %s' % DEBUGLOG
-
+
return urlopen(url)
def __init__(self, html):
self.soup = BeautifulSoup(html)
-
+
def check_page(self):
if self.soup.find('form', {'id': 'form_efaresults'}):
return PageType.RESULT
-
+
if self.soup.find('div', {'class':'form_error'}):
return PageType.CORRECTION
-
+
return PageType.UNKNOWN
-
+
def get_correction(self):
nlo = self.soup.find('select', {'id': 'nameList_origin'})
nld = self.soup.find('select', {'id': 'nameList_destination'})
-
+
if not nlo and not nld:
raise ParserError('Unable to parse html')
-
+
if nlo:
origin = map(lambda x: x.text, nlo.findAll('option'))
else:
destination = map(lambda x: x.text, nld.findAll('option'))
else:
destination = []
-
+
return (origin, destination)
-
+
def get_result(self):
return rParser(str(self.soup))
-
-
-
+
+
+
class rParser:
""" Parser for routing results
"""
@classmethod
def get_tdtext(cls, x, cl):
return x.find('td', {'class': cl}).text
-
+
@classmethod
def get_change(cls, x):
y = rParser.get_tdtext(x, 'col_change')
return datetime.strptime(y, '%d.%m.%Y').date()
else:
return None
-
+
@classmethod
def get_time(cls, x):
y = rParser.get_tdtext(x, 'col_time')
return map(lambda z: time(*map(int, z.split(':'))), wrap(y, 5))
else:
return []
-
+
@classmethod
def get_duration(cls, x):
y = rParser.get_tdtext(x, 'col_duration')
[ [ { 'time': [datetime.time, datetime.time] if time else [],
'station': [u'start', u'end'] if station else [],
'info': [u'start station' if station else u'details for walking', u'end station' if station else u'walking duration']
- }, ... # next trip step
+ }, ... # next trip step
], ... # next trip possibility
]
"""
if table and table.findAll('tr'):
# get rows
rows = table.findAll('tr')[1:] # cut off headline
-
+
overview = map(lambda x: {
'date': rParser.get_date(x),
'time': rParser.get_time(x),
print '%d. %s' % (i, c)
i += 1
lo = sys.stdin.readline().strip()
-
+
args.origin = cor[0][int(lo) - 1]
-
+
if cor[1]:
print
print '* Destination ambiguous:'
print '%d. %s' % (j, c)
j += 1
ld = sys.stdin.readline().strip()
-
+
args.destination = cor[1][int(ld) - 1]
-
+
html = search((args.origin.encode('UTF-8'), args.ot), (args.destination.encode('UTF-8'), args.dt)).read()
parser = sParser(html)
state = parser.check_page()
-
+
except ParserError:
print 'PANIC at correction page'
-
+
if state == PageType.RESULT:
parser = rParser(html)
try:
l = sys.stdin.readline().strip()
print
print '~' * 100
-
+
if l.isdigit() and int(l) <= len(details):
for detail in details[int(l) - 1]:
if detail['time'] and detail['station']:
print '\n'.join(detail['info'])
print '-' * 100
print
-
+
except ParserError:
print 'parsererror'
-
+
elif state == PageType.UNKNOWN:
print 'PANIC unknown result'