diff --git a/helpers/StreckennetzSteffi.py b/helpers/StreckennetzSteffi.py index f83a813..c0f7010 100644 --- a/helpers/StreckennetzSteffi.py +++ b/helpers/StreckennetzSteffi.py @@ -64,6 +64,7 @@ def eva_route_length(self, waypoints) -> float: self.get_name(eva=waypoints[i + 1])) except KeyError: pass + return length @functools.lru_cache(maxsize=8000) def distance(self, u: str, v: str) -> float: diff --git a/server/api.py b/server/api.py index 7926bf6..c995485 100644 --- a/server/api.py +++ b/server/api.py @@ -11,7 +11,6 @@ import os import subprocess -# self-writen stuff from helpers.StationPhillip import StationPhillip from server.connection import get_connection, clean_data, get_trips_of_trains from server.predictor import Predictor @@ -21,7 +20,6 @@ logger = logging.getLogger(__name__) basepath = os.path.dirname(os.path.realpath(__file__)) -# make a new random-forest-predictor instance stations = StationPhillip() pred = Predictor() @@ -65,10 +63,10 @@ def analysis(connection): # add adelay5 to first and last station fs_data = pred.get_pred_data(connection[index1]) ls_data = pred.get_pred_data(connection[-index2]) - _x, _x, _x, _x, _x, connection[index1]["ddelay5"], _x, _x = pred.predict(fs_data, 0) + _x, _x, _x, _x, _x, connection[index1]["ddelay5"], _x, _x = pred.predict(fs_data, 0).values() _x, connection[-index2]["adelay5"], _x, _x, _x, _x, _x, _x = pred.predict( ls_data, 1 - ) + ).values() # there are two segments more than connections (= overall info at the end - 1 bc it is like that) for i in range(index1, len(connection) - index2): @@ -85,13 +83,6 @@ def analysis(connection): ).seconds // 60 ) % 60 # we just want minutes - logger.debug( - str(fromUnix(connection[i + 1]["departure"][time])) - + " - " - + str(fromUnix(connection[i]["arrival"][time])) - + " = " - + str(transtime) - ) ( connection[i]["con_score"], connection[i]["adelay5"], @@ -107,32 +98,11 @@ def analysis(connection): ).seconds // 60 ) % 60 # we just want minutes - logger.debug( - str(fromUnix(connection[i + 2]["departure"][time])) - + " - " - + str(fromUnix(connection[i]["arrival"][time])) - + " = " - + str(transtime) - ) ( connection[i]["con_score"], connection[i]["adelay5"], connection[i + 2]["ddelay5"], ) = pred.predict_con(data1, data2, transtime) - - logger.debug(data1) - logger.debug(data2) - - logger.debug( - "Score for connection[" - + connection[i]["train"]["name"] - + " to " - + connection[i + 1]["train"]["name"] - + " in " - + connection[i]["segmentDestination"]["title"] - + "] = " - + str(connection[i]["con_score"]) - ) if total_score == 0: total_score = connection[i]["con_score"] else: @@ -151,7 +121,7 @@ def analysis(connection): else: connection[-1]["total_score"] = int(total_score * 100) - logger.debug("Verbindungsscore:" + str(total_score)) + # logger.debug("Verbindungsscore:" + str(total_score)) for i in range(len(connection) - 1): if "ICE" in connection[i]["train"]["name"]: diff --git a/server/connection.py b/server/connection.py index 2de51a5..56f0f17 100644 --- a/server/connection.py +++ b/server/connection.py @@ -2,6 +2,7 @@ import sys sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from functools import lru_cache import requests from datetime import datetime, timedelta from pytz import timezone @@ -60,19 +61,24 @@ def get_trips_of_trains(connection): for n in range(len(connection[i]["segments"])): try: jid = connection[i]["segments"][n]["jid"] - r = requests.get( - "https://marudor.de/api/hafas/v1/journeyDetails?jid={}?profile=db".format( - jid - ) - ) - full_trip = r.json()["stops"] - full_trip = [int(part["station"]["id"]) for part in full_trip] - connection[i]["segments"][n]["full_trip"] = full_trip + connection[i]["segments"][n]["full_trip"] = get_trip_of_train(jid) except KeyError: pass # Fußweg has no jid return connection +@lru_cache +def get_trip_of_train(jid): + r = requests.get( + "https://marudor.de/api/hafas/v1/journeyDetails?jid={}?profile=db".format( + jid + ) + ) + trip = r.json()["stops"] + trip = [int(stop["station"]["id"]) for stop in trip] + return trip + + def clean_data(connection): """ Remove unneded content""" for i in range(len(connection)):