joeni

Add TODO for future bus caching

Author
Maarten Vangeneugden
Date
Aug. 24, 2018, 4:42 a.m.
Hash
f8fdc1a03f835dd9c6a4a9df123c1764657a2e88
Parent
3571706c34739595c87d79818f1a79b60f751911
Modified file
administration/bus.py

administration/bus.py

26 additions and 1 deletion.

View changes Hide changes
1
1
import json
2
2
import requests
3
3
from datetime import datetime, tzinfo
4
4
+
5
+
6
+
7
def departures(stop, amount=0):
+
8
    pass
+
9
    """ Returns the remaining departures of buses from the given stop for this day.
+
10
    If amount is given, a maximum amount of departures will be returned.
+
11
    Departures in the past are never returned."""
+
12
    #today = datetime.now().strftime("%d-%m-%Y")
+
13
    #cache = caches["departures"]
+
14
    #cached_data = cache.get(stop)
+
15
    #if cached_data is None:
+
16
        ####received_json_data = requests.get('https://www.delijn.be/rise-api-core/dienstregeling/halteDienstregeling/'+stop+'/'+today+'/nl').json()
+
17
        #buses = list()
+
18
        #for bus_json in received_json_data['halteDoorkomsten']:
+
19
            #bus = dict()
+
20
        ## Some destinations should be a little bit more descriptive, they're handled here hardcoded
+
21
        #destination = bus_json["bestemming"]
+
22
        #bus["time"] = bus_json["lijn"]["tijdstip"]
+
23
+
24
# TODO: I've opted not to use the caching system for a complete day timetable, because it wouldn't load
+
25
# via destinations and give a rather hard time to work with. So I'm gonna stick with the simple
+
26
# realtime thingy, which does the job well enough.
+
27
+
28
+
29
5
30
def arrivals(stops):
6
31
    """ Returns the a list of buses that stop at the given stops.
7
32
    The buses are in a list, with dicts with the following keys:
8
33
    - number :: The line number of the bus
9
34
    - color :: The hex color to display for this bus line
10
35
    - destination :: The name of the destination (already formatted properly
11
-
    - time :: The datetime of the departure
+
36
    - time :: The datetime of the departure
12
37
    - via :: If the bus does a detour, the name is stored here
13
38
    The list can have a maximum of 20 entries.
14
39
    The subroutine throws an exception if no stops were given.
15
40
    """
16
41
    stops_string = stops.pop(0)  # This returns an error in an empty list, which is supposed to happen if no stops are sent
17
42
    while len(stops) != 0:
18
43
        stops_string = stops_string + "+" + stops.pop(0)
19
44
    received_json_data = requests.get('https://www.delijn.be/rise-api-core/haltes/Multivertrekken/'+stops_string+'/20').json()
20
45
    buses = list()
21
46
    for bus_json in received_json_data["lijnen"]:
22
47
        bus = dict()
23
48
        # Some destinations should be a little bit more descriptive, they're handled here hardcoded
24
49
        destination = bus_json["bestemming"]
25
50
        if destination in ["Hasselt", "Genk", "Neerpelt", "Tongeren", "Maastricht", "Bilzen", "Diepenbeek"]:
26
51
            destination += " Station"
27
52
        bus["destination"] = destination
28
53
        bus["number"] = bus_json["lijnNummerPubliek"]
29
54
        bus["color"] = bus_json["kleurAchterGrond"]
30
55
        unix_time = str(bus_json["vertrekCalendar"])[:10]  # De Lijn API returns too many trailing zeros
31
56
        bus["time"] = datetime.fromtimestamp(int(unix_time))
32
57
        # About via:
33
58
        # The viaBestemming has an annoying tendency to uppercase all destinations.
34
59
        # Some can be hardcoded to the right destination, others just need to be
35
60
        # properly capitalized.
36
61
        via = bus_json["viaBestemming"]
37
62
        if via is not None:
38
63
            if via == "GENK":
39
64
                via = "Genk Station"
40
65
            elif via == "MM VILL & GENK":
41
66
                via = "Genk Station en Maasmechelen Village"
42
67
            elif via == "UNIVERSITEIT":
43
68
                via = "Universiteit Hasselt"
44
69
            elif via == "HASSELT":
45
70
                via = "Hasselt station"
46
71
            elif via == "PATERSPLEIN":
47
72
                via = "Diepenbeek Patersplein"
48
73
            else:
49
74
                via = via.title()  # If it's anything else just capitalize and hope for the best
50
75
            bus["via"] = via
51
76
        buses.append(bus)
52
77
    return buses
53
78