141 lines
3.8 KiB
Python
141 lines
3.8 KiB
Python
import unicodedata
|
|
import urllib
|
|
from collections import namedtuple as nt
|
|
from dataclasses import dataclass
|
|
from json import loads
|
|
import logging
|
|
|
|
import pygal
|
|
import pyrqlite.dbapi2 as dbapi2
|
|
import requests
|
|
|
|
connection = dbapi2.connect(
|
|
host="10.61.0.1",
|
|
user="root",
|
|
port=4001,
|
|
password="",
|
|
)
|
|
|
|
|
|
def remove_accents(input_str):
|
|
nfkd_form = unicodedata.normalize("NFKD", input_str)
|
|
return "".join([c for c in nfkd_form if not unicodedata.combining(c)])
|
|
|
|
|
|
@dataclass
|
|
class Género:
|
|
nombre: str = ""
|
|
masculinidad: float = 0
|
|
|
|
|
|
def split_por_genero(nombres):
|
|
# TODO: reimplementar usando datos de España de género asignado
|
|
return {"f": nombres, "m": nombres}
|
|
|
|
|
|
def handle(req):
|
|
"""handle a request to the function
|
|
Args:
|
|
req (str): request body
|
|
|
|
{
|
|
p: prefijo del nombre,
|
|
g: genero del nombre,
|
|
a: año de nacimiento
|
|
}
|
|
|
|
"""
|
|
|
|
try:
|
|
data = loads(req)
|
|
except Exception as e:
|
|
data = {}
|
|
|
|
try:
|
|
prefijo = data.get("p") or None
|
|
genero = data.get("g") or None
|
|
try:
|
|
año = int(data.get("a"))
|
|
except Exception:
|
|
año = None
|
|
except Exception as e:
|
|
prefijo = genero = año = None
|
|
return f"{req} -- {e}", 400
|
|
|
|
if prefijo is not None:
|
|
prefijo = prefijo.strip().lower()
|
|
|
|
if genero not in ("f", "m"):
|
|
genero = None
|
|
|
|
if prefijo is None and año is None: # Totales globales
|
|
with connection.cursor() as cursor:
|
|
sql = """
|
|
SELECT total, nombre
|
|
FROM totales
|
|
ORDER BY total DESC
|
|
LIMIT 50
|
|
"""
|
|
cursor.execute(sql)
|
|
datos = [(r["total"], r["nombre"]) for r in cursor.fetchall()]
|
|
|
|
elif prefijo is None and año is not None: # Totales por año
|
|
with connection.cursor() as cursor:
|
|
sql = """
|
|
SELECT contador, nombre
|
|
FROM nombres
|
|
WHERE
|
|
anio = :anio
|
|
ORDER BY contador DESC
|
|
LIMIT 50
|
|
"""
|
|
cursor.execute(sql, {"anio": año})
|
|
datos = [(r["contador"], r["nombre"]) for r in cursor.fetchall()]
|
|
|
|
elif prefijo is not None and año is None:
|
|
with connection.cursor() as cursor:
|
|
sql = """
|
|
SELECT total, nombre
|
|
FROM totales
|
|
WHERE
|
|
nombre LIKE :nombre
|
|
ORDER BY total DESC
|
|
LIMIT 50
|
|
"""
|
|
cursor.execute(sql, {"nombre": f"{prefijo}%"})
|
|
datos = [(r["total"], r["nombre"]) for r in cursor.fetchall()]
|
|
else:
|
|
with connection.cursor() as cursor:
|
|
sql = """
|
|
SELECT contador, nombre
|
|
FROM nombres
|
|
WHERE
|
|
anio = :anio AND
|
|
nombre LIKE :nombre
|
|
ORDER BY contador DESC
|
|
LIMIT 50
|
|
"""
|
|
cursor.execute(sql, {"anio": año, "nombre": f"{prefijo}%"})
|
|
datos = [(r["contador"], r["nombre"]) for r in cursor.fetchall()]
|
|
|
|
if genero:
|
|
datos = split_por_genero(datos)[genero]
|
|
|
|
datos = datos[:10]
|
|
|
|
chart = pygal.HorizontalBar(height=400, show_legend=False, show_y_labels=True)
|
|
chart.x_labels = [nombre.title() for _, nombre in datos[::-1]]
|
|
if len(datos) > 1:
|
|
chart.title = f"¿Puede ser ... {datos[0][1].title()}? ¿O capaz que {datos[1][1].title()}? ¡Contáme más!"
|
|
elif len(datos) == 1:
|
|
chart.title = f"¡Hola {datos[0][1].title()}!"
|
|
elif len(datos) < 1:
|
|
chart.title = "¡No esssistís!"
|
|
chart.add("", [contador for contador, _ in datos[::-1]])
|
|
|
|
return (
|
|
chart.render(is_unicode=True),
|
|
200,
|
|
{"Content-Type": "image/svg+xml"},
|
|
)
|