Better station list.

This commit is contained in:
W1CDN 2023-07-09 22:06:57 -05:00
parent 40513cc488
commit d382a2b8f7
2 changed files with 27 additions and 3 deletions

View File

@ -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

View File

@ -46,7 +46,7 @@
{% for i in stations %}
<tr>
<td> <a href="https://digi.w1cdn.net/aprs_api/packets?from={{ i['from'] }}">{{ i['from'] }}</a> </td>
<td> {{ i['last_heard_unix'] }} </td>
<td> {{ i['last_heard'] }} </td>
<td> {{ i['count']}} </td>
<td> <a href="https://aprs.fi/#!mt=roadmap&z=12&call=a%2F{{ i['from'] }}">aprs.fi</a></td>
</tr>