from make_website import populate_template_str, datetime, get_json, review_id PESTO_TYPES = ('Genovese', 'Calabrese', 'Arrabiata', 'Herbal', 'Ricotta', 'Rosso', 'Verde') CONSISTENCY_TYPES = ("Creamy", "Rustic") with open('web/templates/table.html', 'r') as f: TABLE_TEMPLATE = f.read() def generate_table_row(review: dict) -> str: return ("" + ("{}" * 12) + "").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 CONSISTENCY_TYPES) ) def make_link(name, url): return '{}'.format( url, name ) def get_type_from_categories(review: dict) -> str: cats = review['category'] for name in PESTO_TYPES: 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)