import requests
from lxml import html


BVG_URL = 'http://mobil.bvg.de/Fahrinfo/bin/stboard.bin/dox?'


def request_station_ids(station_name):
    ''' Requests the station ids for the provided station name.

    The function has two different outcomes dependend on how distinctive
    the station name is. A list of possibel stations or the one station.

    Return a tuple (data, ok). Data holdes the <stations> with their name
    and id. The status flag can be True or False if there are network problems.
    '''

    r = requests.get(BVG_URL, data={'input' : station_name})

    # network
    if r.status_code != 200:
        return None, False

    tree = html.fromstring(r.content)

    data = []

    # possibel stations
    if tree.cssselect('span.error'):
        for station in tree.cssselect('span.select a'):
            station_name = station.text.strip()
            # TODO: clean up direct list access
            station_id = station.get("href").split('&')[1].split('=')[1]
            data.append((station_name,station_id))
        return data, True

    # one station
    # TODO: clean up direct list access
    station_name = tree.cssselect('span.desc strong')[0].text
    station_id = tree.cssselect('p.links a')[0].get('href').split('&')[1].split('=')[1]
    return (station_name, station_id), True

@@ -95,16 +95,95 @@
 n/dox?'%0A
+# https://fahrinfo.bvg.de/Fahrinfo/bin/stboard.bin/dox?input=9079201&start=yes%0A
 %0A%0Adef re
@@ -1389,12 +1389,679 @@
 n_id), True%0A
+%0A%0Adef request_departures(station_id):%0A    ''' Requests the departure times for the provided station id.%0A%0A    Return a tuple (data, ok). Data holdes the %3Cdepartures%3E with time, line and%0A    destination. The status flag can be True or False if there are network problems.%0A    '''%0A    payload = %7B'input' : station_id, 'start' : 'yes' %7D%0A    r = requests.get(BVG_URL, params= payload)%0A%0A    # network%0A    if r.status_code != 200:%0A        return None, False%0A%0A    tree = html.fromstring(r.content)%0A%0A    data = %5B%5D%0A    for row in tree.cssselect('tbody tr'):%0A        cells = tuple(e for e in row.text_content().split('%5Cn') if e)%0A        data.append(cells)%0A    return data, True%0A

Add function to request departure times