From 6957219468c233382c7b7d2a7d60ad7cd0009228 Mon Sep 17 00:00:00 2001 From: W1CDN Date: Sun, 16 Apr 2023 21:04:26 -0500 Subject: [PATCH] Add basic API call to select by "from" station. --- api_app.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/api_app.py b/api_app.py index 00d0a9e..427680e 100644 --- a/api_app.py +++ b/api_app.py @@ -65,6 +65,17 @@ def select_all_frames(conn): rows = cur.fetchall() return rows +def select_frames(conn, n, from_): + cur = conn.cursor() + # Workaround to deal with missing value in WHERE + from_ = "IS NOT NULL" if from_ == None else "='"+from_+"'" + #sql = "SELECT * FROM frames LIMIT "+n + sql = 'SELECT * FROM frames WHERE "from" {from_} LIMIT {n}'.format(from_=from_, n=n) + print(sql) + cur.execute(sql) + rows = cur.fetchall() + return rows + class Packets(Resource): def get(self): # Handle arguments that may or may not exist @@ -72,10 +83,11 @@ class Packets(Resource): n = int(request.args.get('n')) except: n = 10 + from_ = None if request.args.get('from') == None else request.args.get('from') conn = get_db_connection() # Limit to number of records requested - data = select_all_frames(conn)[-n:] + data = select_frames(conn, n = n, from_ = from_) # Sort by created date, descending (https://stackoverflow.com/a/45266808) data.sort(key=operator.itemgetter('created'), reverse=True) #data.sort(key=created, reverse=True)