|
|
@ -2,6 +2,7 @@ import datetime
|
|
|
|
import json
|
|
|
|
import json
|
|
|
|
import re
|
|
|
|
import re
|
|
|
|
import shutil
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
import hashlib
|
|
|
|
|
|
|
|
|
|
|
|
with open('web/templates/review.html', 'r') as f:
|
|
|
|
with open('web/templates/review.html', 'r') as f:
|
|
|
|
REVIEW_TEMPLATE = f.read()
|
|
|
|
REVIEW_TEMPLATE = f.read()
|
|
|
@ -25,7 +26,8 @@ def generate_website(website_source: str, json_source: str, dest: str):
|
|
|
|
website = populate_template_str(website_content, {
|
|
|
|
website = populate_template_str(website_content, {
|
|
|
|
'index': generate_index(data['reviews']),
|
|
|
|
'index': generate_index(data['reviews']),
|
|
|
|
'pesto_ratings': '\n\n'.join(generate_review_html(review) for review in data['reviews']),
|
|
|
|
'pesto_ratings': '\n\n'.join(generate_review_html(review) for review in data['reviews']),
|
|
|
|
'current_year': str(datetime.date.today().year)
|
|
|
|
'current_year': str(datetime.date.today().year),
|
|
|
|
|
|
|
|
'css_hash': stylesheet_hash(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
with open(dest, 'w') as f:
|
|
|
|
with open(dest, 'w') as f:
|
|
|
@ -46,6 +48,7 @@ def generate_review_html(review: dict) -> str:
|
|
|
|
, 'rating_price': review['rating_value']['price']
|
|
|
|
, 'rating_price': review['rating_value']['price']
|
|
|
|
, 'rating_size': review['rating_value']['size']
|
|
|
|
, 'rating_size': review['rating_value']['size']
|
|
|
|
, 'rating': review['final_verdict']['string']
|
|
|
|
, 'rating': review['final_verdict']['string']
|
|
|
|
|
|
|
|
, 'image_items': generate_image_items(review)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
def generate_index(reviews):
|
|
|
|
def generate_index(reviews):
|
|
|
@ -61,6 +64,23 @@ def populate_template_str(templatestr, fields: dict[str, str]):
|
|
|
|
|
|
|
|
|
|
|
|
return re.sub(r'{([A-Z_]+)}', fill, templatestr)
|
|
|
|
return re.sub(r'{([A-Z_]+)}', fill, templatestr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generate_image_items(review: dict) -> str:
|
|
|
|
|
|
|
|
return '\n'.join(
|
|
|
|
|
|
|
|
'<div class="image-item"><img src="img/{}" /></div>'.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__':
|
|
|
|
if __name__ == '__main__':
|
|
|
|
generate_website('web/templates/index.html', 'reviews.json', 'web/index.html')
|
|
|
|
generate_website('web/templates/index.html', 'reviews.json', 'web/index.html')
|
|
|
|
shutil.copy('reviews.json', 'web/reviews.json')
|
|
|
|
shutil.copy('reviews.json', 'web/reviews.json')
|