brainsteam.co.uk/bstools/card.py

93 lines
2.9 KiB
Python

import uuid
import os
from datetime import datetime
from pathlib import Path
import html2image
import click
import frontmatter
html = """
<html>
<head>
<style>
body {
margin: 0;
padding: 0;
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.content {
width: 95%;
}
</style>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css2?family=Nunito&display=swap" rel="stylesheet">
</head>
<body>
<div class="content p-4" style="background-color: #fefefe;font-family: 'Nunito', sans-serif; font-size: 20px;">
<div class="d-flex flex-column justify-content-center align-items-center text-center" style="border: 10px solid #025a5f; padding: 20px; border-radius:50px;">
<span class="tweet-text mb-2" style="font-size: 2.6rem;">
{title}
</span>
<span class="text-muted mb-2">
{date}
</span>
<div class="flex justify-center my-4">
<div class="rounded-full inline-flex" style="background-color: #ffffff; height: 0.25rem; width: 4rem;"></div>
</div>
<div class="mb-2">
<img src="https://brainsteam.co.uk/images/avatar_small.png" class="rounded-circle" style="width: 150px;">
</div>
<h4 class="mt-2" style="color: #025a5f" >James Ravenscroft</h4>
<span class="text-muted">@jamesravey@fosstodon.org</span>
</div>
</div>
</body>
</html>
"""
@click.command()
@click.option('--input-file', '-i', required=True, type=click.Path(exists=True))
@click.option('--workspace-path', '-w', required=True, type=click.Path(exists=True))
def main(input_file, workspace_path):
front_matter = frontmatter.load(input_file)
title = front_matter.get('title')
date = front_matter.get('date', datetime.now().isoformat())
if title and date:
parsed_date = datetime.fromisoformat(
date).strftime("%b %d, %Y")
parsed_html = html.replace(
"{title}", title).replace("{date}", parsed_date)
file_name = f"{uuid.uuid4()}.png"
hti = html2image.Html2Image()
output_path = Path(workspace_path) / \
"static" / "social" / file_name
hti.temp_path = Path(workspace_path) / "tmp"
if not os.path.exists(hti.temp_path):
os.mkdir(hti.temp_path)
files = hti.screenshot(
html_str=[parsed_html], save_as=file_name, size=[(1128, 600)])
# move from tmp location to workspace folder
output_path = output_path.absolute().as_posix()
os.rename(files[0], output_path)
front_matter['preview'] = f"/social/{file_name}"
frontmatter.dump(front_matter, input_file)
if __name__ == "__main__":
main()