repos
/ status-django master

status-django

mirror archived upstream

Self-hostable uptime monitor and status page on Django: HTTP checks, Lighthouse audits, SEO crawls, and email and Discord alerts.

djangodockerhandcodedpythonself-hostedsqlitestatus-pageuptime-monitoringvite

1.2 KB · 39 lines · Python Raw History
 1"""
 2Template tags for social media and sharing in general.
 3"""
 4import hashlib
 5import io
 6import threading
 7
 8from django import template
 9from django.core.files.storage import default_storage
10from django.utils.html import format_html
11from django.utils import timezone
12
13from status.chromium import generate_screenshot_from_url
14
15register = template.Library()
16
17
18@register.simple_tag
19def og_image(url):
20    filename = hashlib.md5(url.encode("utf-8")).hexdigest()
21    filename = f"opengraph/{filename}.png"
22
23    if not default_storage.exists(filename):
24        # save a quick file to prevent infinite loop
25        default_storage.save(filename, io.BytesIO())
26        generate_screenshot_from_url(url, filename)
27    else:
28        # if the file exists, check it's timestamp, if it's older than a day
29        # regenerate it with threading since the old one is okay for now
30        one_day_ago = timezone.now() - timezone.timedelta(days=1)
31        if default_storage.get_modified_time(filename) < one_day_ago:
32            threading.Thread(
33                target=generate_screenshot_from_url, args=(url, filename)
34            ).start()
35
36    # get full url including domain
37    url = default_storage.url(filename)
38    return format_html('<meta property="og:image" content="{}">', url)