From 6f64401785a1f00c11e15b91fa1e3f1133d0bb51 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Mon, 18 Jan 2021 03:59:21 +0000 Subject: [PATCH] Add optional skip and limit to API route --- apiserver/database.py | 3 ++- apiserver/server.py | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/apiserver/database.py b/apiserver/database.py index 9b84b47..6e968ec 100644 --- a/apiserver/database.py +++ b/apiserver/database.py @@ -68,12 +68,13 @@ def get_reflist(amount): q = session.query(Reflist).order_by(Reflist.rid.desc()).limit(amount) return [dict(ref=x.ref, sid=x.sid, source=x.source) for x in q.all()] -def get_stories(amount): +def get_stories(amount, skip=0): session = Session() q = session.query(Reflist, Story.meta_json).\ order_by(Reflist.rid.desc()).\ join(Story).\ filter(Story.title != None).\ + offset(skip).\ limit(amount) return [x[1] for x in q] diff --git a/apiserver/server.py b/apiserver/server.py index c0756d3..8aee440 100644 --- a/apiserver/server.py +++ b/apiserver/server.py @@ -42,7 +42,9 @@ cors = CORS(flask_app) @flask_app.route('/api') def api(): - stories = database.get_stories(FEED_LENGTH) + skip = request.args.get('skip', 0) + limit = request.args.get('limit', FEED_LENGTH) + stories = database.get_stories(limit, skip) # hacky nested json res = Response('{"stories":[' + ','.join(stories) + ']}') res.headers['content-type'] = 'application/json'