NDOJ/judge/utils/opengraph.py
2023-05-20 08:54:17 +09:00

24 lines
850 B
Python
Executable file

from django.core.cache import cache
from django.template.defaultfilters import truncatewords
from judge.jinja2.markdown import markdown
from judge.jinja2.reference import reference
def generate_opengraph(cache_key, data):
metadata = cache.get(cache_key)
if metadata is None:
description = None
tree = reference(markdown(data)).tree
for p in tree.iterfind(".//p"):
text = p.text_content().strip()
if text:
description = text
break
if description:
for remove in (r"\[", r"\]", r"\(", r"\)"):
description = description.replace(remove, "")
img = tree.xpath(".//img")
metadata = truncatewords(description, 60), img[0].get("src") if img else None
cache.set(cache_key, metadata, 86400)
return metadata