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