Пример #1
0
    def resubmit_best_form(self):
        if len(self.parser.select(self.document.getroot(),
                                  'img.img-error')) == 0:
            raise RoadmapError('No error reported!')

        ambi = None
        i = 0
        for form in self.parser.select(self.document.getroot(), 'form'):
            if 'id' in form.attrib and form.attrib['id'] == 'iti-ambi':
                ambi = form
                break
            i += 1

        if ambi is None:
            raise RoadmapError('No ambigous form!')

        props = self.parser.select(ambi, 'span.precision-arret input')
        if len(props) == 0:
            props = self.parser.select(ambi, 'span.precision-adresse input')
            if len(props) == 0:
                raise RoadmapError('Nothing to select to get a roadmap')

        self.browser.select_form(nr=i)
        propname = props[0].attrib['name']
        propvalue = props[0].attrib['value'].encode('utf-8')
        self.browser[propname] = [propvalue]
        self.browser.submit()
Пример #2
0
    def search(self, departure, arrival, departure_time, arrival_time):
        match = -1
        for i, form in enumerate(self.browser.forms()):
            try:
                if form.attrs['id'] == 'rech-iti':
                    match = i
            except KeyError:
                pass
        self.browser.select_form(nr=match)
        self.browser['Departure'] = departure
        self.browser['Destination'] = arrival

        time = None
        if departure_time:
            self.browser['sens'] = ['1']
            time = departure_time
        elif arrival_time:
            self.browser['sens'] = ['-1']
            time = arrival_time

        if time:
            try:
                self.browser['dateFull'] = '%02d/%02d/%d' % (
                    time.day, time.month, time.year)
                self.browser['hour'] = ['%02d' % time.hour]
                self.browser['minute'] = [
                    '%02d' % (time.minute - (time.minute % 5))
                ]
            except ClientForm.ItemNotFoundError:
                raise RoadmapError(
                    'Unable to establish a roadmap with %s time at "%s"' %
                    ('departure' if departure_time else 'arrival', time))
        self.browser.submit()
Пример #3
0
    def search(self, departure, arrival, departure_time, arrival_time):
        self.browser.select_form('formHiRecherche')
        self.browser['lieuDepart'] = departure.encode('utf-8')
        self.browser['lieuArrivee'] = arrival.encode('utf-8')

        time = None
        if departure_time:
            self.browser['typeHeure'] = ['1']
            time = departure_time
        elif arrival_time:
            self.browser['typeHeure'] = ['-1']
            time = arrival_time

        if time:
            try:
                self.browser['jour'] = ['%d' % time.day]
                self.browser['mois'] = ['%02d/%d' % (time.month, time.year)]
                self.browser['heure'] = ['%02d' % time.hour]
                self.browser['minutes'] = [
                    '%02d' % (time.minute - (time.minute % 5))
                ]
            except ClientForm.ItemNotFoundError:
                raise RoadmapError(
                    'Unable to establish a roadmap with %s time at "%s"' %
                    ('departure' if departure_time else 'arrival', time))
        self.browser.submit()
Пример #4
0
    def find_best(self):
        if len(self.parser.select(self.document.getroot(),
                                  'img.img-error')) > 0:
            if len(self.parser.select(self.document.getroot(),
                                      'form#iti-ambi')) > 0:
                raise RoadmapAmbiguity('Ambigious stop name')
            else:
                raise RoadmapError('Error when submitting form')

        best = self.parser.select(self.document.getroot(),
                                  'div.alerte-bloc-important div.bloc-iti')
        if len(best) == 0:
            best = self.parser.select(self.document.getroot(), 'div.bloc-iti')
            if len(best) == 0:
                raise RoadmapError('Unable to get the best roadmap')

        link = self.parser.select(best[0], 'a.btn-submit')
        if len(link) == 0:
            raise RoadmapError('Unable to get a link to best roadmap')

        return self.html_br_strip(link[0].attrib['href'])
Пример #5
0
    def get_steps(self):
        errors = []
        for p in self.parser.select(self.document.getroot(), 'p.errors'):
            if p.text:
                errors.append(p.text.strip())

        if len(errors) > 0:
            raise RoadmapError('Unable to establish a roadmap: %s' %
                               ', '.join(errors))

        current_step = None
        i = 0
        for tr in self.parser.select(self.document.getroot(),
                                     'table.horaires2 tbody tr'):
            if not 'class' in tr.attrib:
                continue
            elif tr.attrib['class'] == 'trHautTroncon':
                current_step = {}
                current_step['id'] = i
                i += 1
                current_step['start_time'] = self.parse_time(
                    self.parser.select(tr, 'td.formattedHeureDepart p',
                                       1).text.strip())
                current_step['line'] = to_unicode(
                    self.parser.select(tr,
                                       'td.rechercheResultatColumnMode img')
                    [-1].attrib['alt'])
                current_step['departure'] = to_unicode(
                    self.parser.select(tr, 'td.descDepart p strong',
                                       1).text.strip())
                current_step['duration'] = self.parse_duration(
                    self.parser.select(tr, 'td.rechercheResultatVertAlign',
                                       1).text.strip())
            elif tr.attrib['class'] == 'trBasTroncon':
                current_step['end_time'] = self.parse_time(
                    self.parser.select(tr, 'td.formattedHeureArrivee p',
                                       1).text.strip())
                current_step['arrival'] = to_unicode(
                    self.parser.select(tr, 'td.descArrivee p strong',
                                       1).text.strip())
                yield current_step
Пример #6
0
 def __init__(self, error):
     RoadmapError.__init__(self, error)
Пример #7
0
    def get_steps(self):
        errors = []
        # for p in self.parser.select(self.document.getroot(), 'p.errors'):
        #    if p.text:
        #        errors.append(p.text.strip())

        if len(errors) > 0:
            raise RoadmapError('Unable to establish a roadmap: %s' %
                               ', '.join(errors))

        current_step = None
        i = 0
        for tr in self.parser.select(self.document.getroot(),
                                     'table.itineraire-detail tr'):
            if current_step is None:
                current_step = {
                    'id': i,
                    'start_time': datetime.datetime.now(),
                    'end_time': datetime.datetime.now(),
                    'line': '',
                    'departure': '',
                    'arrival': '',
                    'duration': datetime.timedelta()
                }

            if 'class' in tr.attrib:
                if 'bg-ligne' in tr.attrib['class']:
                    continue

                if 'iti-map' in tr.attrib['class']:
                    continue

            for td in self.parser.select(tr, 'td'):
                if 'class' not in td.attrib:
                    continue

                if 'iti-inner' in td.attrib['class']:
                    continue

                if 'cell-infos' in td.attrib['class']:
                    if 'id' in td.attrib:
                        if td.attrib['id'].find('MapOpenLink') >= 0:
                            hasA = self.parser.select(td, 'a')
                            if len(hasA) == 0:
                                if len(current_step['line']) > 0 and \
                                   len(current_step['departure']) > 0 and \
                                   len(current_step['arrival']) > 0:
                                    current_step['line'] = to_unicode(
                                        "%s : %s" % (current_step['mode'],
                                                     current_step['line']))
                                    del current_step['mode']
                                    yield current_step
                                    i += 1
                                    current_step = None
                                    continue

                if 'cell-horaires' in td.attrib['class']:
                    # real start
                    for heure in self.parser.select(td, 'span.heure'):
                        if heure.attrib['id'].find('FromTime') >= 0:
                            current_step['start_time'] = self.parse_time(
                                heure.text)
                        if heure.attrib['id'].find('ToTime') >= 0:
                            current_step['end_time'] = self.parse_time(
                                heure.text)
                    for mode in self.parser.select(td,
                                                   'span.mode-locomotion img'):
                        current_step['mode'] = mode.attrib['title']

                if 'cell-details' in td.attrib['class']:
                    # If we get a span, it's a line indication,
                    # otherwise check for id containing LibDeparture or
                    # LibDestination
                    spans = self.parser.select(td, 'span.itineraire-ligne')
                    if len(spans) == 1:
                        line = self.html_br_strip(spans[0].text,
                                                  " ").replace('Ligne ', '')
                        if line.index('- ') == 0:
                            line = re.sub(r'^- ', '', line)
                        current_step['line'] = line

                    elif 'id' in td.attrib:
                        stops = self.parser.select(td, 'strong')
                        stop = self.html_br_strip(stops[0].text, " ")

                        if td.attrib['id'].find('LibDeparture') >= 0:
                            current_step['departure'] = to_unicode(stop)

                        if td.attrib['id'].find('LibDestination') >= 0:
                            current_step['arrival'] = to_unicode(stop)

                            duree = self.parser.select(td, 'span.duree strong')
                            if len(duree) == 1:
                                current_step['duration'] = self.parse_duration(
                                    duree[0].text)
Пример #8
0
 def __init__(self, error):
     RoadmapError.__init__(self, error)