import datetime import json import re import shutil import hashlib with open('web/templates/review.html', 'r') as f: REVIEW_TEMPLATE = f.read() def review_id(review): return "_".join(re.sub('\s+', '-', x.lower() if x else '') for x in (review['company'], review['name'], review['variant'])) def review_title(review): return '{} "{}" {}'.format( review['company'], review['name'], '({})'.format(review['variant']) if review['variant'] else '' ) def get_json(): with open("reviews.json", 'r') as f: return json.load(f) def generate_website(template_dir: str, dest: str): with open(template_dir + 'index.html', 'r') as f: website_content = f.read() with open(template_dir + 'table.html', 'r') as f: table_template = f.read() data = get_json() website = populate_template_str(website_content, { 'index': generate_index(data['reviews']), 'pesto_ratings': '\n\n'.join(generate_review_html(review) for review in data['reviews']), 'current_year': str(datetime.date.today().year), 'css_hash': stylesheet_hash(), }) with open(dest + 'index.html', 'w') as f: f.write(website) f.write(''.format(datetime.datetime.now())) def generate_review_html(review: dict) -> str: return populate_template_str(REVIEW_TEMPLATE, { 'review_id': review_id(review) , 'title': review_title(review) , 'date': review['date'] , 'notes': review['notes'] , 'ingredients': ', '.join(review['ingredients']) , 'rating_taste': review['rating_value']['taste'] , 'rating_consistency': review['rating_value']['consistency'] , 'rating_ingredients': review['rating_value']['ingredients'] , 'rating_price': review['rating_value']['price'] , 'rating_size': review['rating_value']['size'] , 'rating_servings': review['rating_value']['servings'] , 'rating': review['final_verdict']['string'] , 'image_items': generate_image_items(review) , 'categories': " ".join('{0}'.format(cat[0].upper() + cat[1:]) for cat in review['category']) }) def generate_index(reviews): return "".format( "\n".join('
  • {}
  • '.format( review_id(review), review_title(review) ) for review in reviews) ) def populate_template_str(templatestr, fields: dict[str, str]): def fill(match): return fields.get(match.group(1).lower(), 'Unknown field {}'.format(match.group(1))) return re.sub(r'{([A-Z_]+)}', fill, templatestr) def generate_image_items(review: dict) -> str: return '\n'.join( '
    '.format(img) for img in review.get('images', []) ) def stylesheet_hash(): hash = hashlib.sha256() with open("web/style.css", 'rb') as f: while True: chunk = f.read(hash.block_size) if not chunk: break hash.update(chunk) return hash.hexdigest() if __name__ == '__main__': generate_website('web/templates/', 'web/') shutil.copy('reviews.json', 'web/reviews.json')