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

View File

@@ -387,6 +387,8 @@ def character():
This file also pulls information to build character descriptions and other relevant information. This file also pulls information to build character descriptions and other relevant information.
""" """
data = load_file(os.path.join(DATA_DIR, 'algolia.json'), True) data = load_file(os.path.join(DATA_DIR, 'algolia.json'), True)
descriptions = load_file(os.path.join(DATA_DIR, 'character_descriptions.json'), True)
key_list = [('speaker',), ('text',), ('season',), ('episode_rel', 'episode'), ('section_rel', 'scene'), key_list = [('speaker',), ('text',), ('season',), ('episode_rel', 'episode'), ('section_rel', 'scene'),
('quote_rel', 'quote')] ('quote_rel', 'quote')]
master = map(lambda item: algolia_transform(item, key_list), filter(lambda item: True, data)) master = map(lambda item: algolia_transform(item, key_list), filter(lambda item: True, data))
@@ -396,8 +398,15 @@ def character():
for quote in master: for quote in master:
char_data[character_id(quote['speaker'])].append(quote) char_data[character_id(quote['speaker'])].append(quote)
final_data = {}
for character, quotes in char_data.items():
final_data[character] = {'quotes': quotes, 'summary': None, 'name': None}
if character in descriptions.keys():
final_data[character]['name'] = descriptions[character].get('name')
final_data[character]['summary'] = descriptions[character].get('summary')
# Save to characters.json # Save to characters.json
save_file(os.path.join(DATA_DIR, 'characters.json'), char_data, True) save_file(os.path.join(DATA_DIR, 'characters.json'), final_data, True)
@build.command('final') @build.command('final')

View File

File diff suppressed because it is too large Load Diff