diff --git a/api_app.py b/api_app.py index 26d899f..b35ee05 100644 --- a/api_app.py +++ b/api_app.py @@ -1,8 +1,9 @@ from flask import Flask, request, render_template -from flask_restful import Resource, Api, reqparse +from flask_restful import Resource, Api, reqparse, url_for from datetime import date, timedelta import configparser import csv +import datetime import ast import glob import json, operator @@ -120,7 +121,12 @@ def index(): response = json.loads(requests.get("https://digi.w1cdn.net/aprs_api/packets").text)['data'] # Play with function to create station list - stations = select_all_stations(get_db_connection()) + #stations = select_all_stations(get_db_connection()) + #print(url_for("static", filename="test.txt", _external=True)) + stations = json.loads(requests.get(url_for("stations", _external=True)).text)['data'] + # Convert unix time to datetime on the fly because I'm lazy right now + for station in stations: + station['last_heard'] = datetime.datetime.fromtimestamp(station['last_heard_unix']) return render_template('index.html', station_call = config['Settings']['station_call'], @@ -144,6 +150,21 @@ class Packets(Resource): #data.sort(key=operator.itemgetter('created'), reverse=True) return {'data':data}, 200 # return data and 200 OK code +class Stations(Resource): + def get(self): + # Handle arguments that may or may not exist + try: + n = int(request.args.get('n')) + except: + n = 10 + + conn = get_db_connection() + # Limit to number of records requested + data = select_all_stations(conn) + # Sort by created date, descending (https://stackoverflow.com/a/45266808) + #data.sort(key=operator.itemgetter('created'), reverse=True) + return {'data':data}, 200 # return data and 200 OK code + # Read config config = read_config() log_folder = config['Settings']['log_folder'] @@ -155,7 +176,10 @@ import subprocess proc = subprocess.Popen("exec " + "python3 kiss_and_db.py", stdout=subprocess.PIPE, shell=True) print("kiss_and_db.py as subprocess pid "+str(proc.pid)) +# The packets endpoint api.add_resource(Packets, '/packets') +# The stations endpoint +api.add_resource(Stations, '/stations') if __name__ == '__main__': api_app.run(debug=True, host='0.0.0.0', port=5001) # run our Flask app diff --git a/templates/index.html b/templates/index.html index 7ba1ae7..9f3d900 100644 --- a/templates/index.html +++ b/templates/index.html @@ -46,7 +46,7 @@ {% for i in stations %}