weboob-devel/weboob/backends/transilien/browser.py
2011-04-08 12:48:07 +02:00

163 lines
5.9 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
# Copyright(C) 2010-2011 Julien Hébert, 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/>.
from datetime import datetime, date, time
import HTMLParser
from weboob.tools.browser import BaseBrowser
from weboob.tools.misc import to_unicode
from .pages.route import RoutePage
class Route(object):
"une ligne code_mission | time"
def __init__(self, code_mission, time, destination, platform):
self.code_mission = code_mission
self.time = time
self.destination = destination
self.platform = platform
def __repr__(self):
return "<Route %s %s %s %s>" % (self.code_mission,
self.time, self.destination, self.platform)
class Parser(HTMLParser.HTMLParser):
"Parse les tableaux html contenant les horaires"
def __init__(self):
HTMLParser.HTMLParser.__init__(self)
self.__table_horaires3 = False
self.__code_de_mission = False
self.__a_code_de_mission = False
self.__time = False
self.__destination = False
self.__platform = False
self.__liste_train = []
self.__liste_horaire = []
self.__liste_destination = []
self.__liste_platform = []
@classmethod
def parse(cls, data, encoding):
parser = cls()
parser.feed(data.read())
return parser
def handle_starttag(self, tag, attrs):
"execute a chaque balise ouvrante"
if (tag == 'table' and (dict(attrs)['class'] == 'horaires3')):
self.__table_horaires3 = True
elif self.__table_horaires3 and tag == 'td':
try:
self.__code_de_mission = (
dict(attrs)['headers'] == 'Code_de_mission')
self.__time = (
dict(attrs)['headers'] == 'Heure_de_passage')
self.__destination = (
dict(attrs)['headers'] == 'Destination')
self.__platform = (
dict(attrs)['headers'] == 'Voie')
except KeyError:
if dict(attrs).has_key('headers'):
raise
else:
pass
else:
self.__a_code_de_mission = (tag == 'a' and self.__code_de_mission)
def handle_data(self, data):
"execute pour chaque contenu de balise"
if self.__a_code_de_mission:
self.__liste_train.append(data.strip())
if self.__time and data.strip() != '*':
self.__liste_horaire.append(data.strip())
if self.__destination:
self.__liste_destination.append(data.strip())
if self.__platform:
self.__liste_platform.append(data.strip())
def handle_endtag(self, tag):
"execute à chaque balise fermante"
self.__a_code_de_mission ^= (self.__a_code_de_mission and tag == 'a')
self.__time ^= (self.__time and tag == 'td')
self.__destination ^= (self.__destination and tag == 'td')
self.__platform ^= (self.__platform and tag == 'td')
@property
def list_route(self):
"getter"
__list_route = []
__curseur_horaire = 0
for __i in self.__liste_train:
__list_route.append(Route(
code_mission=__i,
time=self.__liste_horaire[__curseur_horaire],
destination=self.__liste_destination[__curseur_horaire],
platform=self.__liste_platform[__curseur_horaire]
))
__curseur_horaire += 1
return __list_route
class Transilien(BaseBrowser):
DOMAIN = 'www.transilien.com'
PAGES = {'https://www\.transilien\.com/web/ITProchainsTrainsAvecDest\.do\?.*': RoutePage,
'https://www\.transilien\.com/web/ITProchainsTrains\.do\?.*': RoutePage
}
PROTOCOL = 'https'
USER_AGENT = BaseBrowser.USER_AGENTS['microb']
def __init__(self, **kwargs):
kwargs['parser'] = Parser
BaseBrowser.__init__(self, '', **kwargs)
def iter_station_search(self, pattern):
pass
def iter_station_departures(self, station_id, arrival_id=None):
if arrival_id:
self.location('https://www.transilien.com/web/ITProchainsTrainsAvecDest.do?codeTr3aDepart=%s&codeTr3aDest=%s&urlModule=/site/pid/184&gareAcc=true' % (station_id, arrival_id))
else:
self.location('https://www.transilien.com/web/ITProchainsTrains.do?tr3a=%s&urlModule=/site/pid/184' % station_id)
for route in self.page.document.list_route:
_late_reason = None
try :
_time = datetime.combine(date.today(), time(*[int(x) for x in route.time.split(':')]))
except ValueError:
_time = None
_late_reason = route.time
else:
yield {'type': to_unicode(route.code_mission),
'time': _time,
'departure': to_unicode(station_id),
'arrival': to_unicode(route.destination),
'late': time(),
'late_reason': _late_reason,
'plateform': to_unicode(route.platform)}
def home(self):
pass
def login(self):
pass
def is_logged(self):
""" Do not need to be logged """
return True