ready API for returning just quotes by page (and fix page KeyError), base summary/name for Episode-like pre-loading, generating and merging with cli.py build characters

This commit is contained in:
Xevion
2020-09-15 10:30:33 -05:00
parent 84fce25cc8
commit 1a3eee0ac4
3 changed files with 102051 additions and 101330 deletions

View File

@@ -3,6 +3,7 @@ api.py
Provides a accessible protected backend API. JSON I/O only, CSRF protected.
"""
import copy
import json
import os
from copy import deepcopy
@@ -19,6 +20,9 @@ with open(os.path.join(BASE_DIR, 'data', 'data.json'), 'r', encoding='utf-8') as
with open(os.path.join(BASE_DIR, 'data', 'characters.json'), 'r', encoding='utf-8') as file:
character_data = json.load(file)
# Cached preload character data
character_list = dict()
stats = {
'totals': {
'quote': 0,
@@ -98,8 +102,16 @@ def api_character_list():
@current_app.route('/api/character/<character>/')
def api_character_all(character: str):
if request.args['page']:
_data = copy.deepcopy(character_data[character])
_data['quotes'] = _data['quotes'][:10]
return jsonify(_data)
@current_app.route('/api/character/<character>/quotes/')
def api_character_quotes(character: str):
quotes = character_data[character]['quotes']
if 'page' in request.args.keys():
index: int = (int(request.args['page']) - 1) * 10
return jsonify(character_data[character][index: index + 10])
return jsonify(quotes[index: index + 10])
else:
return jsonify(character_data[character])
return jsonify(quotes)