You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.1 KiB
Python

from make_website import populate_template_str, datetime, get_json, review_id
with open('web/templates/table.html', 'r') as f:
TABLE_TEMPLATE = f.read()
def generate_table_row(review: dict) -> str:
return ("<tr>" + ("<td>{}</td>" * 12) + "</tr>").format(
review['company'],
make_link(
review['name'] + (' ({})'.format(review['variant']) if review['variant'] else ''),
'index.html#' + review_id(review)
),
get_type_from_categories(review),
review['final_verdict']['string'],
review['rating_value']['taste'],
review['rating_value']['consistency'],
review['rating_value']['ingredients'],
review['rating_value']['size'],
review['rating_value']['price'],
price_per_100g(review),
taste_grams_per_price(review),
", ".join(cat for cat in review['category'] if cat in ('Creamy', 'Rustic'))
)
def make_link(name, url):
return '<a href="{}">{}</a>'.format(
url, name
)
def get_type_from_categories(review: dict) -> str:
cats = review['category']
for name in ('Genovese', 'Calabrese', 'Ricotta', 'Tomato'):
if name in cats:
return name
def price_per_100g(review: dict, raw=False):
weight, price = review['rating_value'].get('size_value', None), review['rating_value'].get('price_value', None)
if None in (weight, price):
return "???"
if raw:
return price / weight * 100
return "{:.2f}".format(price / weight * 100)
def taste_grams_per_price(review):
price_per_100g_val = price_per_100g(review, True)
if price_per_100g_val == '???':
return '???'
return "{:.1f}".format(
review['rating_value']['taste_percent'] / price_per_100g_val * 10
)
if __name__ == '__main__':
reviews = get_json()['reviews']
html = populate_template_str(
TABLE_TEMPLATE,
{
'table_rows': "\n".join(generate_table_row(review) for review in reviews),
'current_year': str(datetime.datetime.now().year)
}
)
with open("web/table.html", 'w') as f:
f.write(html)