added endpoint to get most popular circles

This commit is contained in:
Kevin Froman 2020-03-28 02:41:23 -05:00
parent 825c576ce3
commit b256db2698
4 changed files with 38 additions and 19 deletions

View file

@ -2,6 +2,7 @@
This file primarily serves to allow specific fetching of circles board messages
"""
import operator
import json
import os
@ -26,17 +27,20 @@ from utils import identifyhome
flask_blueprint = Blueprint('circles', __name__)
with open(os.path.dirname(os.path.realpath(__file__)) + '/info.json', 'r') as info_file:
with open(
os.path.dirname(
os.path.realpath(__file__)) + '/info.json', 'r') as info_file:
data = info_file.read().strip()
version = json.loads(data, strict=False)['version']
BOARD_CACHE_FILE = identifyhome.identify_home() + '/board-index.cache.json',
BOARD_CACHE_FILE = identifyhome.identify_home() + '/board-index.cache.json'
read_only_cache = DeadSimpleKV(
BOARD_CACHE_FILE,
flush_on_exit=False,
refresh_seconds=30)
@flask_blueprint.route('/circles/getpostsbyboard/<board>')
def get_post_by_board(board):
board_cache = DeadSimpleKV(
@ -50,6 +54,7 @@ def get_post_by_board(board):
posts = ','.join(posts)
return Response(posts)
@flask_blueprint.route('/circles/getpostsbyboard/<board>/<offset>')
def get_post_by_board_with_offset(board, offset):
offset = int(offset)
@ -66,11 +71,14 @@ def get_post_by_board_with_offset(board, offset):
posts = ','.join(posts[offset:offset + OFFSET_COUNT])
return Response(posts)
@flask_blueprint.route('/circles/version')
def get_version():
return Response(version)
@flask_blueprint.route('/circles/removefromcache/<board>/<name>', methods=['POST'])
@flask_blueprint.route('/circles/removefromcache/<board>/<name>',
methods=['POST'])
def remove_from_cache(board, name):
board_cache = DeadSimpleKV(BOARD_CACHE_FILE,
flush_on_exit=False)
@ -83,7 +91,18 @@ def remove_from_cache(board, name):
board_cache.put(board, posts)
return Response('success')
#@flask_blueprint.route('/circles/getpopular/<count>')
#def get_popular(count):
#boards = read_only_cache.get
@flask_blueprint.route('/circles/getpopular/<count>')
def get_popular(count):
boards = json.loads(read_only_cache.get_raw_json())
for board in boards:
boards[board] = len(boards[board])
top_boards = sorted(boards.items(), key=operator.itemgetter(1), reverse=True)[:int(count)]
only_board_names = []
for b in top_boards:
only_board_names.append(b[0])
return Response(','.join(only_board_names), content_type='text/csv')