Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
100 lines (75 sloc) 4.39 KB
from xml.etree import cElementTree as ET
import time
import dateutil.parser
import httplib2
class reismogelijkheid():
class tijden():
def __init__(self, xml, typetijd):
self.reistijd = time.strptime(xml.find('{}'+typetijd+'ReisTijd').text, '%H:%M')
self.aankomsttijd = dateutil.parser.parse(xml.find('{}'+typetijd+'AankomstTijd').text)
self.vertrektijd = dateutil.parser.parse(xml.find('{}'+typetijd+'VertrekTijd').text)
class verstoring():
def __init__(self, xml): = xml.find('{}Id').text
self.ernstig = (xml.find('{}Ernstig').text == 'true')
self.text = xml.find('{}Text').text
class reisdeel():
class reisstop():
def __init__(self, xml):
self.naam = xml.find('{}Naam').text
self.tijd = dateutil.parser.parse(xml.find('{}Tijd').text)
self.spoor = xml.find('{}Spoor')
self.spoorwijziging = None
if self.spoor is not None:
self.spoorwijziging = (self.spoor.attrib['wijziging'] == 'true')
self.spoor = self.spoor.text
def __init__(self, xml):
self.vervoertype = xml.find('{}VervoerType').text
self.ritnummer = xml.find('{}RitNummer').text
self.reissoort = xml.attrib['reisSoort']
self.reisstops = [self.reisstop(xmlstop) for xmlstop in xml.findall('{}ReisStop')]
def __init__(self, xml):
self.aantaloverstappen = xml.find('{}AantalOverstappen')
if self.aantaloverstappen is not None:
self.aantaloverstappen = self.aantaloverstappen.text
self.gepland = self.tijden(xml, 'Geplande')
self.actueel = self.tijden(xml, 'Actuele')
self.reisdelen = [self.reisdeel(xmldeel) for xmldeel in xml.findall('{}ReisDeel')]
self.melding = xml.find('{}Melding')
if self.melding is not None:
self.melding = self.verstoring(self.melding)
self.aankomstvertraging = xml.find('{}AankomstVertraging')
if self.aankomstvertraging is not None:
self.aankomstvertraging = self.aankomstvertraging.text
class treinplanner():
def __init__(self, username, password):
self.http = httplib2.Http(disable_ssl_certificate_validation=True)
self.http.follow_redirects = False
self.http.add_credentials(username, password)
def fetch(self, from_station, to_station, via_station=None, previous_advices=5, next_advices=5, date_time=None, departure=True, hsl_allowed=True, year_card=False):
query = 'fromStation=%s&toStation=%s' % (from_station, to_station)
if via_station is not None:
query += '&viaStation=%s' % (via_station)
if previous_advices < 5:
query += '&previousAdvices=%d' % (previous_advices)
if next_advices < 5:
query += '&nextAdvices=%d' % (next_advices)
if date_time is not None :
query += '&dateTime=%s' % (date_time.isoformat())
if departure == False:
query += '&departure=false'
if hsl_allowed == False:
query += '&hslAllowed=false'
if year_card == True:
query += '&yearCard=true'
resp, content = self.http.request("" % (query))
if resp['status'] == '200':
content = content.replace('\n', '').replace('\t', '').replace(' ','').replace(' ', '')
return ET.XML(content.replace('<ReisMogelijkheden>', '<ReisMogelijkheden xmlns="">'))
def fetchandparse(self, *args, **kwargs):
root = self.fetch(*args, **kwargs)
if root is not None:
return [reismogelijkheid(xmlmogelijkheid) for xmlmogelijkheid in root.findall('{}ReisMogelijkheid')]
You can’t perform that action at this time.