import datetime
import requests
import json
import math


class DigitransitAPIService:
    def __init__(self):
        self.url = 'http://api.digitransit.fi/routing/v1/routers/hsl/index/graphql'
        self.headers = {'Content-Type': 'application/graphql'}

    def get_stops(self, lat, lon, radius):
        data = {}
        stops = []
        stop_ids = self.get_stops_near_coordinates(lat, lon, radius)

        for stop_id in stop_ids:
            stops.append({"stop": self.get_busses_by_stop_id(stop_id)})

        data["stops"] = stops
        return data

    def get_stops_near_coordinates(self, lat, lon, radius=160):
        if radius > 1000:
            radius = 1000
        query = ("{stopsByRadius(lat:%f, lon:%f, radius:%d) {"
                 "  edges {"
                 "      node {"
                 "          distance"
                 "          stop {"
                 "    	        gtfsId"
                 "              name"
                 "          }"
                 "      }"
                 "    }"
                 "  }"
                 "}") % (lat, lon, radius)
        data = json.loads(self.get_query(query))
        data = data['data']['stopsByRadius']['edges']
        stoplist = []
        for n in data:
            stoplist.append(n['node']['stop']['gtfsId'])
        return stoplist[:3]

    def get_busses_by_stop_id(self, stop_id):
        query = ("{stop(id: \"%s\") {"
                      "  name"
                      "  code"
                      "  vehicleType"
                      "  stoptimesForServiceDate(date: \"%s\"){"
                      "     pattern {"
                      "         code"
                      "         name"
                      "         directionId"
                      "         route {"
                      "             gtfsId"
                      "             longName"
                      "             shortName"
                      "         }"
                      "     }"
                      "     stoptimes {"
                      "         serviceDay"
                      "    	    realtimeArrival"
                      "      }"
                      "    }"
                      "  }"
                      "}") % (stop_id, datetime.datetime.now().strftime("%Y%m%d"))

        data = json.loads(self.get_query(query))["data"]["stop"]

        lines = data["stoptimesForServiceDate"]

        current_time = datetime.datetime.now()

        stop = {'stop_name': data["name"], 'stop_code': data["code"], 'schedule': []}
        schedule = []
        for line in lines:
            stoptimes = line["stoptimes"]

            if line["pattern"]["directionId"]==1:
                destination = line["pattern"]["route"]["longName"].split("-")[0].strip()
            else:
                destination = line["pattern"]["route"]["longName"].split("-")[-1].strip()

            for time in stoptimes:
                arrival_time = datetime.datetime.fromtimestamp(time["serviceDay"] + time["realtimeArrival"])
                arrival = math.floor((arrival_time - current_time).total_seconds() / 60.0)  # Arrival in minutes
                if current_time < arrival_time:
                    schedule.append({'vehicle_id': line["pattern"]["code"].replace(':','_'),
                                     'line': line["pattern"]["route"]["shortName"],
                                     'destination': destination,
                                     'arrival': arrival,
                                     'routeId': line["pattern"]["route"]["gtfsId"],
                                     'vehicle_type': data["vehicleType"] 
                                     })

        sorted_list = sorted(schedule, key=lambda k: k['arrival'])
        stop["schedule"] = sorted_list[:10]

        return stop

    def get_query(self, query):
        response = requests.post(self.url, data=query, headers=self.headers)

        return response.text

@@ -2024,24 +2024,142 @@
 toptimes %7B%22%0A
+                      %22         trip%7B%22%0A                      %22             gtfsId%22%0A                      %22         %7D%22%0A
             
@@ -3762,17 +3762,105 @@
 leType%22%5D
- 
+,%0A                                     'trip_id': line%5B%22stoptimes%22%5D%5B%22trip%22%5D%5B%22gtfsId%22%5D%5B4:%5D
 %0A       

Update services.py