Skip to content

Instantly share code, notes, and snippets.

@oleeander
Created August 19, 2013 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oleeander/6270640 to your computer and use it in GitHub Desktop.
Save oleeander/6270640 to your computer and use it in GitHub Desktop.
Vehicle wants to be a library giving access to timetable enquiries using HAFAS. But it is heavily broken.
#!/usr/bin/env python3
# Copyright (C) 2013 Oleander Reis <oleander@oleander.cc>
#
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the authors be held liable for any damages
# arising from the use of this software.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must not
# claim that you wrote the original software. If you use this software
# in a product, an acknowledgment in the product documentation would be
# appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.
"""
Vehicle public transport library
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vehicle is a library giving access to timetable enquiries using HAFAS.
Basic usage:
>>> from vehicle import Vehicle
>>> rmv = Vehicle('http://www.rmv.de/auskunft')
>>> rmv.get_station_departures(3019904, 10)
[<Service dep=16:29 arr=None start=None dest=Marburg-Wehrda Sachsenring route=Bus 1>,
<Service dep=16:31 arr=None start=None dest=Marburg Eisenacher Weg route=Bus 1>,
<Service dep=16:32 arr=None start=None dest=Marburg-Cappel Moischter Straße route=Bus 3>,
<Service dep=16:33 arr=None start=None dest=Marburg Hauptbahnhof route=Bus E2>,
<Service dep=16:34 arr=None start=None dest=Marburg Pommernweg route=Bus 1>,
<Service dep=16:35 arr=None start=None dest=Marburg Hauptbahnhof route=Bus 2>,
<Service dep=16:35 arr=None start=None dest=Marburg Sonnenblick route=Bus 7>,
<Service dep=16:38 arr=None start=None dest=Marburg-Wehrshausen Neuhöfe route=Bus 17>,
<Service dep=16:38 arr=None start=None dest=Marburg-Cappel Cappeler Gleiche route=Bus 2>,
<Service dep=16:38 arr=None start=None dest=Marburg Universitätsklinikum route=Bus 7>]
"""
__title__ = 'Vehicle'
__version__ = '0.0.1'
__author__ = 'Oleander Reis'
__license__ = 'zlib'
__copyright__ = '2013 by Oleander Reis'
from pyquery import PyQuery as pq
import datetime
class Vehicle(object):
def __init__(self, base_url):
self.base_url = base_url
def get_station_departures(self, station, count):
return Station.departures(self, station, count)
def get_route_stations(self, route):
return Route.stations(self, route)
class Service(object):
def __init__(self, dep=None, arr=None, start=None, dest=None, route=None):
self._dep = dep
self._arr = arr
self._start = start
self._dest = dest
self._route = route
def __repr__(self):
return "<{cls} dep={dep} arr={arr} start={start} dest={dest} route={route}>".format(
cls=type(self).__name__,
dep=self._dep,
arr=self._arr,
start=self._start,
dest=self._dest,
route=self._route
)
def __range__(self):
return self.dep, self.arr
@property
def dep(self):
return self._dep
@property
def arr(self):
return self._arr
@property
def start(self):
return Station(self._start)
@property
def dest(self):
return Station(self._dest)
@property
def route(self):
return self._route
class Station(object):
_id = None
def __init__(self, id):
self._id = id
def __repr__(self):
return "<{cls} id={id}>".format(
cls=type(self).__name__,
id=self._id,
)
def __str__(self):
return self._id
def departures(self, id, count):
data = pq(
url='{base_url}/bin/jp/stboard.exe/dt?input={id}&boardType=dep&dateBegin={today}&dateEnd={future}&maxJourneys={count}&start=yes&outputMode=stationInfoBoardOnly'.format(
base_url=self.base_url,
id=id,
today=datetime.date.today().strftime('%d.%m.%y'),
future=(datetime.date.today() + datetime.timedelta(days=30)).strftime('%d.%m.%y'),
count=count
)
)
return [
Service(
dep=pq(service).find('td[headers=hafasSQarrTime]').text(),
dest=pq(service).find('td[headers=hafasSQarrDest]').text(),
route=pq(service).find('td[headers=hafasSQarrLine]').text()
) for service in data('table > tbody > tr')
]
class Route(object):
_id = None
def __init__(self, id):
self._id = id
def __repr__(self):
return "<{cls} id={id}>".format(
cls=type(self).__name__,
id=self._id,
)
def stations(self, id):
return None
if __name__ == '__main__':
vehicle = Vehicle('http://www.rmv.de/auskunft')
for service in vehicle.get_station_departures(3019904, 10):
print((service.dep, str(service.dest), service.route))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment