Package: vbbvg
This little tool will fetch and display real-time departure times for VBB/BVG
public transport lines for a single stop in Berlin and Brandenburg, Germany.
Here,VBBis the “Verkehrsverbund Berlin-Brandenburg” andBVGis the “Berliner
Verkehrsbetriebe”.This tool was partly developed as an instructive example (although a toy one)
of usingPandasand producing some output to be fed into a web-based
dashboard like those one can create withDashing(to be done).Sample outputHere is an example output for “Möckernbrücke” (including the optional--header)
on the command-line:$vbbvg--stopMöckernbrücke--headerNow: 14:06:04
Stop-Name: U Möckernbrücke (Berlin)
Stop-ID: 9017104

Wait    Departure    Line    Destination
------  -----------  ------  ----------------------------
00:48   14:07        U1      U Uhlandstr. (Berlin)
02:48   14:09        U7      S+U Rathaus Spandau (Berlin)
02:48   14:09        U1      S+U Warschauer Str. (Berlin)
04:48   14:11        U7      U Rudow (Berlin)This shows the waiting and departure times (in MM:SS and HH:MM format,
respectively) from one stop, limited to all earliest unique combinations of
theLineandDestinationcolumns.
This is usually the only information one is interested in just “before
leaving the office” as a typical use-case.
This tool filters these combinations, calculates the waiting times and inserts
them as the first column in the output.
There are quite a few other command-line options which you can find out more
about by typingpython vbbvg-h.Installation and TestYou can clone the repository and install it via pip. After
installing, you will have access system wide (or in your virtualenv)
tovbbvgprogrammatically or via the CLI.pip install -e .There is a list of dependencies in the filerequirements.txt(for more about them please read the next section) which you can install
with the commandpip install-rrequirements.txt.To run the little “test suite”, download and unpack this repository or
clone it, and run the commandpython setup.py testin the unpacked archive.
Of course this needs thepytestpackage to be available (not listed in
the requirements, but easy to install withpip install pytest).DependenciesThis tool was partly developed as an instructive example of usingPandasfor
greatly simplifying the code needed for working with tables (DataFrameobjects).
TheBeautifulSoup4andhtml5libpackages are optional dependencies,
but needed forpandas.read_html()which will barf if they are not
installed.
Output on the command-line is created by using thetermcolorandtabulatepackages, saving a great amount of code to write otherwise
oneself.System Requirements (Linux)As a Linux user pointed out: if you are on Linux you might have to install
the following packages manually:sudo apt-get install libxml2-dev libxslt1-devAnd if you run into an/usr/bin/ld: cannot find-lzerror consider
installing this one before runningpip, too:sudo apt-get install lib32z1-devImplementationSince VBB/BVG have no API for real time data access this data is fetched
(scraped usingPandas, yes!) from a web application onhttp://mobil.bvg.de.
You can use this page fortesting manually(in English).
There, as a real person, you can enter parts of the destination name and get
a list of matching destinations to chose from, before you get to see the one
result table you are interested in.To avoid multi-level scraping, speed things up, and add some more thrills,
a small part of an existing “Open Data”VBB database, published under theCC-BY 3.0 licenseis used to access the stop names and IDs of the VBB/BVG public transport
network (a simple CSV file named herevbbvg_stops.csv).The resulting tables are output as “real” tables in various formats on
the command-line, see usage examples below.UsageCommand-LineYou can run a few sample command-line calls using the options--testand--stop<NAME_ID>for a given stop name or ID like this for the
stopMöckernbrücke:$vbbvg--test--stopMöckernbrückeProgrammaticThe main function to use programmatically isvbbvg.get_next_departures(),
which returns aPandasDataFrameobject, which you can convert to almost
anything you like. See the following examples:Get departures of S7 and S75 from Berlin main station:In[1]:importvbbvgIn[2]:df=vbbvg.get_next_departures('9003201',filter_line='S7')In[3]:df.columnsOut[3]:Index([u'Wait',u'Departure',u'Line',u'Destination'],dtype='object')In[4]:list(df.to_records())Out[4]:[(1,'00:00',u'10:01',u'S75 (Gl. 16)',u'S Westkreuz (Berlin)'),(4,'01:10',u'10:03',u'S75 (Gl. 15)',u'S Wartenberg (Berlin)'),(14,'04:10',u'10:06',u'S7 (Gl. 16)',u'S Potsdam Hauptbahnhof'),(24,'07:10',u'10:09',u'S7 (Gl. 15)',u'S Ahrensfelde Bhf (Berlin)'),(62,'21:10',u'10:23',u'S75 (Gl. 15)',u'S Ostbahnhof (Berlin)')]In[5]:print(df.to_csv()),Wait,Departure,Line,Destination1,00:00,10:01,S75(Gl.16),SWestkreuz(Berlin)4,01:10,10:03,S75(Gl.15),SWartenberg(Berlin)14,04:10,10:06,S7(Gl.16),SPotsdamHauptbahnhof24,07:10,10:09,S7(Gl.15),SAhrensfeldeBhf(Berlin)62,21:10,10:23,S75(Gl.15),SOstbahnhof(Berlin)DashboardsWhen using this tool inside some kind of web-based dashboard like those
created byDashing(which was the originally intended use-case)
one should use a stop’s ID to be sure to provide a unique stop on the
VBB/BVG public transport network. You can find out the IDs by running
test queries with the--headeroption.Todomentionhttp://fahrinfo.vbb.de/bin/stboard.exe/en? (provides some more
filtering features)add more examples in the Usage section abovemake the codepolyglot, running not only on Python 2.7 but also 3.4/3.5test option to filter specific line types like S-Bahn (‘S.*’) or single
lines (‘U7’)use in some real dashboard like those of dhasing.io (the original purpose!)mention that case is ignored in the whole tool for all stop namesstore the last displayed stop (in ~/.vvbvg or so) and reuse when called
without any args/optionsremove index numbers (leftmost column) from result tables when used
programmaticallyDue to time limitations any help is welcome with any of the items above.