1
0
Fork 0
mirror of https://github.com/Findus23/lw1.at.git synced 2024-09-11 06:23:44 +02:00

support RSS feeds

This commit is contained in:
Lukas Winkler 2023-02-26 00:17:20 +01:00
parent 3b4155c2c0
commit 103021146d
Signed by: lukas
GPG key ID: 54DE4D798D244853
6 changed files with 78 additions and 2 deletions

61
lw1/feed.py Normal file
View file

@ -0,0 +1,61 @@
import json
from pathlib import Path
from xml.etree import ElementTree
from xml.etree.ElementTree import SubElement
from lw1.post import Post
from lw1.settings import DOMAIN, GENERATOR_URL
class RSSFeed:
def __init__(self, lang: str, feed_name: str):
self.feed_name = feed_name
self.lang = lang
self.root = ElementTree.Element("rss", {"version": "2.0"})
self.feed = {
"version": "https://jsonfeed.org/version/1.1",
"title": "Lukas Winkler - lw1.at",
"language": lang,
"home_page_url": DOMAIN,
"feed_url": DOMAIN + "/" + lang + "/" + feed_name + ".json",
"generator": GENERATOR_URL,
"authors": [{"name": "Lukas Winkler"}],
"items": []
}
self.channel = SubElement(self.root, "channel")
SubElement(self.channel, "title").text = self.feed["title"]
SubElement(self.channel, "lang").text = self.feed["language"]
SubElement(self.channel, "link").text = self.feed["home_page_url"]
SubElement(self.channel, "generator").text = self.feed["generator"]
SubElement(self.channel, "description").text = self.feed["title"]
# self.root.attrib["xmlns"] = ""
# self.root.attrib["{http://www.sitemaps.org/schemas/sitemap/0.9}xhtml"] = "http://www.w3.org/1999/xhtml"
def dump(self, output_dir: Path):
tree = ElementTree.ElementTree(self.root)
ElementTree.indent(tree, space="\t", level=0)
with (output_dir / self.lang / (self.feed_name + ".xml")).open("wb") as f:
tree.write(f, xml_declaration=True, encoding="utf-8")
with (output_dir / self.lang / (self.feed_name + ".json")).open("w") as f:
json.dump(self.feed, f, indent=2)
def add_post(self, post: Post):
item = {
"id": post.absolute_url_with_domain,
"url": post.absolute_url_with_domain,
"content_html": str(post.html),
"title": post.title,
"date_published": post.date.isoformat(),
"tags": post.tags
}
if post.description:
item["summary"] = post.description
self.feed["items"].append(item)
item_el = SubElement(self.channel, "item")
SubElement(item_el, "title").text = item["title"]
SubElement(item_el, "link").text = post.absolute_url_with_domain
SubElement(item_el, "guid", {"isPermaLink": "true"}).text = post.absolute_url_with_domain
SubElement(item_el, "pubdate").text = post.date.strftime("%a, %d %b %Y %H:%M:%S %z")
SubElement(item_el, "description").text = item["content_html"]
SubElement(item_el, "author").text = "Lukas Winkler"

View file

@ -3,6 +3,7 @@ from time import perf_counter_ns
from babel.support import Translations
from lw1.compression import compress_files
from lw1.feed import RSSFeed
from lw1.generators import PostsGenerator, HomepageGenerator, ImprintGenerator, LangRedirectGenerator, \
NotFoundGenerator, PermissionDeniedGenerator, ServerErrorGenerator
from lw1.loader import PostLoader, TagsLoader, AssetsLoader
@ -14,6 +15,8 @@ from lw1.writer import Writer
def main(debug=False):
sitemap = Sitemap()
rss_feed_de = RSSFeed(lang="de", feed_name="postfeed")
rss_feed_en = RSSFeed(lang="en", feed_name="postfeed")
start = perf_counter_ns()
posts = PostLoader.load_posts()
tags = TagsLoader.load_tags()
@ -40,7 +43,14 @@ def main(debug=False):
sitemap=sitemap
)
g.generate(writer)
for post in posts:
if post.lang == "de":
rss_feed_de.add_post(post)
else:
rss_feed_en.add_post(post)
sitemap.dump(output_dir / "sitemap.xml")
rss_feed_de.dump(output_dir)
rss_feed_en.dump(output_dir)
end = perf_counter_ns()
if not debug:
compress_files()

View file

@ -7,6 +7,7 @@ from pathlib import Path
from typing import Dict, List
from babel import dates
from markupsafe import Markup
from lw1.file import Image
from lw1.settings import DOMAIN
@ -69,7 +70,7 @@ class Post:
return "https://github.com/" + self.github
@property
def html(self) -> str:
def html(self) -> Markup:
return markdown2html(self.markdown)
@property

View file

@ -10,3 +10,5 @@ FALLBACK_LANGUAGES: Dict[Language, Language] = {
}
DOMAIN = "https://lw1.at"
GENERATOR_URL = "https://github.com/Findus23/lw1.at/"

View file

@ -6,7 +6,7 @@ from lw1.cache import cache
from lw1.utils import long_hash
def markdown2html(md: str) -> str:
def markdown2html(md: str) -> Markup:
key = "md" + long_hash(md)
cached = cache.get(key)
if cached:

View file

@ -12,6 +12,8 @@
<link rel="alternate" hreflang="{{ lang }}" href="https://lw1.at{{ url }}"/>
<link rel="alternate" hreflang="{{ otherlang }}" href="https://lw1.at{{ otherlang_url }}"/>
{% endblock %}
<link rel="alternate" type="application/feed+json" href="https://lw1.at/{{ lang }}/postfeed.json">
<link rel="alternate" type="application/rss+xml" href="https://lw1.at/{{ lang }}/postfeed.xml">
{% block additionalHead %}
{% endblock %}
<title>{% block title %}{% endblock %} - lw1.at</title>