Start using new HSL API
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
from django.shortcuts import render
|
||||
from django.http import HttpResponse, JsonResponse, HttpResponseBadRequest
|
||||
from django.views.decorators.http import require_http_methods
|
||||
from django.conf import settings
|
||||
|
||||
from infoscreen.models import Rotation, InfoItem, InfoInstance
|
||||
from infoscreen.hsl_fetcher import HSLFetcher
|
||||
from infoscreen.models import Rotation, InfoItem, InfoInstance, HSLDataModel
|
||||
from infoscreen.hsl_fetcher import fetch as hsl_fetch
|
||||
|
||||
import json
|
||||
import logging
|
||||
import threading
|
||||
|
||||
|
||||
@require_http_methods(["GET"])
|
||||
@@ -82,22 +84,18 @@ def hsl_timetable_settings(request, *args, **kwargs):
|
||||
"""Set HSL timetable settings."""
|
||||
d = {"departure_threshold": settings.HSL_DEPARTURE_THRESHOLD,
|
||||
"hurry_threshold": settings.HSL_HURRY_THRESHOLD}
|
||||
resp = json.dumps(d)
|
||||
return HttpResponse(resp, status=200)
|
||||
|
||||
return JsonResponse(d, status=200)
|
||||
|
||||
|
||||
@require_http_methods(["GET"])
|
||||
def CurrentHSLView(request, *args, **kwargs):
|
||||
"""Get HSL data and return it."""
|
||||
fetcher = HSLFetcher()
|
||||
fetcherThread = threading.Thread(target=fetcher.fetch_if_needed, args=[])
|
||||
fetcherThread.setDaemon(False)
|
||||
fetcherThread.start()
|
||||
try:
|
||||
api_resp = hsl_fetch()
|
||||
except Exception as ex:
|
||||
logging.exception('Failed to fetch HSL timetables.')
|
||||
error = {'error': 'Aikataulujen haku epäonnistui.'}
|
||||
return JsonResponse(error, status=200)
|
||||
|
||||
data = HSLDataModel.objects.all()
|
||||
if len(data) < 1:
|
||||
return HttpResponse(
|
||||
'{"error" : "Could not find timetables from database."}',
|
||||
status=500)
|
||||
|
||||
return HttpResponse(data[len(data) - 1].data, status=200)
|
||||
return JsonResponse(api_resp, status=200, safe=False)
|
||||
|
||||
Reference in New Issue
Block a user