support roadmaps in transilien (#681)

This commit is contained in:
Romain Bignon 2011-06-28 17:09:39 +02:00
commit 142bc04f56
5 changed files with 147 additions and 17 deletions

View file

@ -72,3 +72,10 @@ class Traveloob(ReplApplication):
for backend, departure in self.do('iter_station_departures', station_id, arrival_id, backends=backends):
self.format(departure)
self.flush()
def do_roadmap(self, line):
departure, arrival = self.parse_command_args(line, 2, 2)
for backend, route in self.do('iter_roadmap', departure, arrival):
self.format(route)
self.flush()

View file

@ -18,7 +18,7 @@
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.capabilities.travel import ICapTravel, Station, Departure
from weboob.capabilities.travel import ICapTravel, Station, Departure, RoadStep
from weboob.tools.backend import BaseBackend
from .browser import Transilien
@ -40,11 +40,26 @@ class TransilienBackend(BaseBackend, ICapTravel):
yield Station(_id, name)
def iter_station_departures(self, station_id, arrival_id=None):
for i, d in enumerate(self.browser.iter_station_departures(station_id, arrival_id)):
departure = Departure(i, d['type'], d['time'])
departure.departure_station = d['departure']
departure.arrival_station = d['arrival']
departure.late = d['late']
departure.information = d['late_reason']
departure.plateform = d['plateform']
yield departure
with self.browser:
for i, d in enumerate(self.browser.iter_station_departures(station_id, arrival_id)):
departure = Departure(i, d['type'], d['time'])
departure.departure_station = d['departure']
departure.arrival_station = d['arrival']
departure.late = d['late']
departure.information = d['late_reason']
departure.plateform = d['plateform']
yield departure
def iter_roadmap(self, departure, arrival):
with self.browser:
roadmap = self.browser.get_roadmap(departure, arrival)
for s in roadmap['steps']:
step = RoadStep(s['id'])
step.line = s['line']
step.start_time = s['start_time']
step.end_time = s['end_time']
step.departure = s['departure']
step.arrival = s['arrival']
step.duration = s['duration']
yield step

View file

@ -18,18 +18,31 @@
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.tools.browser import BaseBrowser
from weboob.tools.browser import BaseBrowser, BasePage, BrowserUnavailable
from .pages.route import RoutePage
from .pages.departures import DeparturesPage
from .pages.roadmap import RoadmapSearchPage, RoadmapConfirmPage, RoadmapPage
class UnavailablePage(BasePage):
def on_loaded(self):
raise BrowserUnavailable('Website is currently unavailable')
class Transilien(BaseBrowser):
DOMAIN = 'www.transilien.com'
PROTOCOL = 'https'
USER_AGENT = BaseBrowser.USER_AGENTS['microb']
PAGES = {'https://www\.transilien\.com/web/ITProchainsTrainsAvecDest\.do\?.*': RoutePage,
'https://www\.transilien\.com/web/ITProchainsTrains\.do\?.*': RoutePage,
PAGES = {'https://www\.transilien\.com/web/ITProchainsTrainsAvecDest\.do\?.*': DeparturesPage,
'https://www\.transilien\.com/web/ITProchainsTrains\.do\?.*': DeparturesPage,
'https://www\.transilien\.com/web/site.*': RoadmapSearchPage,
'https://www\.transilien\.com/web/RedirectHI.do.*': RoadmapConfirmPage,
'https://www\.transilien\.com/web/RedirectHIIntermediaire.do.*': RoadmapPage,
'https://www\.transilien\.com/transilien_sncf_maintenance_en_cours.htm': UnavailablePage,
}
def is_logged(self):
""" Do not need to be logged """
return True
def iter_station_search(self, pattern):
pass
@ -41,6 +54,16 @@ class Transilien(BaseBrowser):
return self.page.iter_routes()
def is_logged(self):
""" Do not need to be logged """
return True
def get_roadmap(self, departure, arrival):
self.location('/web/site/accueil/etat-trafic/chercher-itineraire/lang/en')
assert self.is_on_page(RoadmapSearchPage)
self.page.search(departure, arrival)
assert self.is_on_page(RoadmapConfirmPage)
self.page.confirm()
assert self.is_on_page(RoadmapPage)
roadmap = {}
roadmap['steps'] = list(self.page.get_steps())
return roadmap

View file

@ -22,10 +22,14 @@ import datetime
from weboob.tools.misc import to_unicode
from weboob.tools.browser import BasePage, BrokenPageError
__all__ = ['StationNotFound', 'DeparturesPage']
class StationNotFound(Exception):
pass
class RoutePage(BasePage):
class DeparturesPage(BasePage):
def iter_routes(self):
try:
table = self.parser.select(self.document.getroot(), 'table.horaires3', 1)

View file

@ -0,0 +1,81 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2010-2011 Romain Bignon
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
import re
import datetime
from weboob.tools.browser import BasePage
from weboob.tools.misc import to_unicode
__all__ = ['RoadmapPage']
class RoadmapSearchPage(BasePage):
def search(self, departure, arrival):
self.browser.select_form('formHiRecherche')
self.browser['lieuDepart'] = departure.encode('utf-8')
self.browser['lieuArrivee'] = arrival.encode('utf-8')
self.browser.submit()
class RoadmapConfirmPage(BasePage):
def select(self, name, num):
try:
self.browser[name] = str(num)
except TypeError:
self.browser[name] = [str(num)]
def confirm(self):
self.browser.select_form('form1')
self.browser.set_all_readonly(False)
self.select('idDepart', 1)
self.select('idArrivee', 1)
self.browser.submit()
class RoadmapPage(BasePage):
def get_steps(self):
current_step = None
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'] = 0
current_step['start_time'] = self.parse_time(self.parser.select(tr, 'td.formattedHeureDepart p', 1).text.strip())
current_step['line'] = 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
def parse_time(self, time):
h, m = time.split('h')
return datetime.time(int(h), int(m))
def parse_duration(self, dur):
m = re.match('(\d+)min.', dur)
if m:
return datetime.timedelta(minutes=int(m.group(1)))
m = re.match('(\d+)h(\d+)', dur)
if m:
return datetime.timedelta(hours=int(m.group(1)),
minutes=int(m.group(2)))