def get_routes(departure, dest, vehicle='vlakbus', time='', date=''):
    """Return list of available routes from departure city to destination"""

    def _get_leaf_element(table, path):
        """Returns leaf element's text in given path"""
        res = table.xpath(path + '/*[not(*)]')
        if res:
            return res[0].text
        return table.xpath(path + '/text()')[0]

    if time == '':
        time = datetime.datetime.now().strftime('%H:%M')

    if date == '':
        date = datetime.datetime.now().strftime('%d.%m.%Y')

    try:
        req = requests.get(CPSK_URL.format(vehicle),
                           params={'date': date, 'time': time, 'f': departure,
                                   't': dest, 'submit': 'true'})
    except:
        return False

    tree = html.fromstring(req.text)
    html_tables = tree.xpath('//div[@id="main-res-inner"]/table/tbody')
    routes = []

    for table in html_tables:
        drive = Drive()
        datalen = len(table.xpath('./tr'))

        prevdate = ''
        for i in range(1, datalen-1):
            line = Line()
            trf = './tr[' + str(i) + ']'
            trt = './tr[' + str(i+1) + ']'
            line.f = _get_leaf_element(table, trf + '/td[3]')
            line.t = _get_leaf_element(table, trt + '/td[3]')
            line.departure = _get_leaf_element(table, trf + '/td[5]')
            line.arrival = _get_leaf_element(table, trt + '/td[4]')
            line.vehicle = table.xpath(trf + '/td[7]/img[1]')[0] \
                                .get('title').replace('Autobus', 'Bus')
            if line.vehicle == 'Presun':
                line.walk_duration = table.xpath(trf + '/td[7]/text()')[0] \
                    .replace('Presun asi ', '')

            delay = table.xpath(trf + '/td[7]/div[1]/' +
                                'span[@class!="nodelay"]/text()')
            if delay and delay[0] is not u'Aktuálne bez meškania':
                mins = delay[0].replace(u'Aktuálne meškanie ', '') \
                               .replace(u' minúty', '') \
                               .replace(u' minútu', '') \
                               .replace(u' minút', '')

                minstr = 'minutes' if mins is not '1' else 'minute'

                line.delay = ' ({0} {1} delay)'.format(mins, minstr)

            platform = table.xpath(trf + '/td[6]/span[1]/text()')
            platform2 = table.xpath(trf + '/td[6]/table/tr[1]/' +
                                    'td[1]/span[1]/text()')
            if platform:
                line.platform = '[{0}]'.format(platform[0])
            elif platform2:
                line.platform = '[{0}]'.format(platform2[0])

            _date = table.xpath(trf + '/td[2]/text()')[0]
            if _date is not ' ':
                prevdate = _date
            line.date = prevdate

            drive.lines.append(line)

        drive.duration = table.xpath('./tr[' + str(datalen) +
                                     ']/td[3]/p/strong[1]/text()')[0]

        try:
            drive.distance = table.xpath('./tr[' + str(datalen) +
                                         ']/td[3]/p/strong[2]/text()')[0]
        except IndexError:
            drive.distance = 'Distance not known'

        routes.append(drive)

    return routes
def get_routes ( departure , dest , vehicle = 'vlakbus' , time = '' , date = '' ) : def _get_leaf_element ( table , path ) : res = table . xpath ( path + '/*[not(*)]' ) if res : return res [ 0 ] . text return table . xpath ( path + '/text()' ) [ 0 ] if time == '' : time = datetime . datetime . now ( ) . strftime ( '%H:%M' ) if date == '' : date = datetime . datetime . now ( ) . strftime ( '%d.%m.%Y' ) try : req = requests . get ( CPSK_URL . format ( vehicle ) , params = { 'date' : date , 'time' : time , 'f' : departure , 't' : dest , 'submit' : 'true' } ) except : return False tree = html . fromstring ( req . text ) html_tables = tree . xpath ( '//div[@id="main-res-inner"]/table/tbody' ) routes = [ ] for table in html_tables : drive = Drive ( ) datalen = len ( table . xpath ( './tr' ) ) prevdate = '' for i in range ( 1 , datalen - 1 ) : line = Line ( ) trf = './tr[' + str ( i ) + ']' trt = './tr[' + str ( i + 1 ) + ']' line . f = _get_leaf_element ( table , trf + '/td[3]' ) line . t = _get_leaf_element ( table , trt + '/td[3]' ) line . departure = _get_leaf_element ( table , trf + '/td[5]' ) line . arrival = _get_leaf_element ( table , trt + '/td[4]' ) line . vehicle = table . xpath ( trf + '/td[7]/img[1]' ) [ 0 ] . get ( 'title' ) . replace ( 'Autobus' , 'Bus' ) if line . vehicle == 'Presun' : line . walk_duration = table . xpath ( trf + '/td[7]/text()' ) [ 0 ] . replace ( 'Presun asi ' , '' ) delay = table . xpath ( trf + '/td[7]/div[1]/' + 'span[@class!="nodelay"]/text()' ) if delay and delay [ 0 ] is not u'Aktuálne bez meškania' : mins = delay [ 0 ] . replace ( u'Aktuálne meškanie ' , '' ) . replace ( u' minúty' , '' ) . replace ( u' minútu' , '' ) . replace ( u' minút' , '' ) minstr = 'minutes' if mins is not '1' else 'minute' line . delay = ' ({0} {1} delay)' . format ( mins , minstr ) platform = table . xpath ( trf + '/td[6]/span[1]/text()' ) platform2 = table . xpath ( trf + '/td[6]/table/tr[1]/' + 'td[1]/span[1]/text()' ) if platform : line . platform = '[{0}]' . format ( platform [ 0 ] ) elif platform2 : line . platform = '[{0}]' . format ( platform2 [ 0 ] ) _date = table . xpath ( trf + '/td[2]/text()' ) [ 0 ] if _date is not ' ' : prevdate = _date line . date = prevdate drive . lines . append ( line ) drive . duration = table . xpath ( './tr[' + str ( datalen ) + ']/td[3]/p/strong[1]/text()' ) [ 0 ] try : drive . distance = table . xpath ( './tr[' + str ( datalen ) + ']/td[3]/p/strong[2]/text()' ) [ 0 ] except IndexError : drive . distance = 'Distance not known' routes . append ( drive ) return routes
Return list of available routes from departure city to destination