character name separation and character.json saving, refactor character ID generation to helpers

This commit is contained in:
Xevion
2020-09-14 12:15:08 -05:00
parent 21b471e9d4
commit 1d9d1d27ad
3 changed files with 19 additions and 11 deletions

View File

@@ -9,6 +9,7 @@ import re
import sys
import time
from collections import defaultdict
from pprint import pprint
from typing import List, Optional, Tuple, Union
import click
@@ -16,9 +17,8 @@ import enlighten
import requests
from bs4 import BeautifulSoup
from server.helpers import algolia_transform
sys.path[0] += '\\..'
from server.helpers import algolia_transform, character_id
from server.process import DATA_DIR, get_appearances, get_episodes, get_filepath, load_file, \
save_file, sleep_from, \
verify_episode
@@ -389,7 +389,15 @@ def character():
data = load_file(os.path.join(DATA_DIR, 'algolia.json'), True)
key_list = [('speaker',), ('text',), ('season',), ('episode_rel', 'episode'), ('section_rel', 'scene'),
('quote_rel', 'quote')]
master = map(lambda item: algolia_transform(item, key_list), filter(lambda: True, data))
master = map(lambda item: algolia_transform(item, key_list), filter(lambda item: True, data))
# Separate the quotes based on speaker
char_data = defaultdict(list)
for quote in master:
char_data[character_id(quote['speaker'])].append(quote)
# Save to characters.json
save_file(os.path.join(DATA_DIR, 'characters.json'), char_data, True)
@build.command('final')

View File

@@ -50,3 +50,10 @@ def algolia_transform(old_dictionary: dict, key_list: List[Tuple[str, Optional[s
new_dictionary[keyItem[0]] = old_dictionary[keyItem[0]]
return new_dictionary
def is_main_character(name: str) -> bool:
return None
def character_id(name: str) -> str:
return '-'.join(name.split(' ')).lower()

View File

@@ -117,13 +117,6 @@ def get_appearances(season, episode) -> Optional[List[Dict[str, Union[int, str]]
for scene in scenes:
for quote in scene.get('quotes', []):
characters[quote.get('speaker')] += 1
characters = [{'name': character, 'appearances': appearances, 'id': '-'.join(character.split(' ')).lower()}
characters = [{'name': character, 'appearances': appearances, 'id': character_id(character)}
for character, appearances in characters.items()]
return list(sorted(characters, key=lambda item: item['appearances'], reverse=True))
def get_character(character):
"""
Retrieves a character's appearances from every season and episode available.
:param character: The character's name
:return: A list of dictionary quotes including reference IDs
"""