Begin stats API route

This commit is contained in:
2025-11-20 22:25:26 +00:00
parent e84062394b
commit 5a7f55184d

View File

@@ -30,6 +30,8 @@ database.init()
search.init()
news_index = 0
ref_list = []
current_item = {}
def new_id():
nid = gen_rand_id()
@@ -51,6 +53,18 @@ def api():
res.headers['content-type'] = 'application/json'
return res
@flask_app.route('/api/stats', strict_slashes=False)
def apistats():
stats = {
'news_index': news_index,
'ref_list': ref_list,
'len_ref_list': len(ref_list),
'current_item': current_item,
}
return stats
@flask_app.route('/api/search', strict_slashes=False)
def apisearch():
q = request.args.get('q', '')
@@ -173,7 +187,7 @@ def static_story(sid):
http_server = WSGIServer(('', 33842), flask_app)
def feed_thread():
global news_index
global news_index, ref_list, current_item
try:
while True:
@@ -194,13 +208,13 @@ def feed_thread():
# update current stories
if news_index < len(ref_list):
item = ref_list[news_index]
current_item = ref_list[news_index]
try:
story_json = database.get_story(item['sid']).full_json
story_json = database.get_story(current_item['sid']).full_json
story = json.loads(story_json)
except AttributeError:
story = dict(id=item['sid'], ref=item['ref'], source=item['source'])
story = dict(id=current_item['sid'], ref=current_item['ref'], source=current_item['source'])
logging.info('Updating {} story: {}, index: {}'.format(story['source'], story['ref'], news_index))
@@ -209,8 +223,8 @@ def feed_thread():
database.put_story(story)
search.put_story(story)
else:
database.del_ref(item['ref'])
logging.info('Removed ref {}'.format(item['ref']))
database.del_ref(current_item['ref'])
logging.info('Removed ref {}'.format(current_item['ref']))
else:
logging.info('Skipping index: ' + str(news_index))