Commit c9eff5a1 authored by Marien Fressinaud's avatar Marien Fressinaud

tec: Abstract the feed generation with Boopfeed

parent 43428c21
......@@ -12,9 +12,8 @@ import uuid
import markdown
import pytz
import xml.etree.ElementTree as ET
import boopsy
import boopfeed
locale.setlocale(locale.LC_ALL, "")
......@@ -196,10 +195,6 @@ def main():
with open(output_filepath, "w") as output_file:
output_file.write(article.render())
# Finish by writing the news feed
feed_dirpath = os.path.join(output_path, "feeds")
os.mkdir(feed_dirpath)
# Just create some variables so it's easier to manipulate after
site_url = configuration["SITE_URL"]
site_uuid = configuration["SITE_UUID"]
......@@ -208,45 +203,42 @@ def main():
timezone = pytz.timezone(site_timezone)
current_datetime = datetime.datetime.now(tz=timezone)
# Start to build the XML feed with the root information
feed = ET.Element("feed", xmlns="http://www.w3.org/2005/Atom")
ET.SubElement(feed, "title").text = site_title
ET.SubElement(
feed,
"link",
href=f"{site_url}feeds/all.atom.xml",
rel="self",
type="application/atom+xml",
# Build the Atom feed
feed = boopfeed.Atom()
feed.add_header(
{
"title": site_title,
"self_link": f"{site_url}feeds/all.atom.xml",
"site_link": site_url,
"id": f"urn:uuid:{site_uuid}",
"updated": current_datetime,
}
)
ET.SubElement(feed, "link", href=site_url, rel="alternate", type="text/html")
ET.SubElement(feed, "id").text = f"urn:uuid:{site_uuid}"
ET.SubElement(feed, "updated").text = current_datetime.isoformat(timespec="seconds")
for article in articles:
# and complete it for each article
entry = ET.SubElement(feed, "entry")
ET.SubElement(entry, "title").text = article.title()
ET.SubElement(entry, "id").text = f"urn:uuid:{article.uuid()}"
entry = {
"title": article.title(),
"id": f"urn:uuid:{article.uuid()}",
"article_link": article.url(),
"published": article.date(),
"content": article.content(),
}
article_author = article.author()
if article_author:
author = ET.SubElement(entry, "author")
ET.SubElement(author, "name").text = article_author
ET.SubElement(
entry, "link", href=article.url(), rel="alternate", type="text/html"
)
ET.SubElement(entry, "published").text = article.date().isoformat(
timespec="seconds"
)
entry["author"] = article_author
article_update = article.update()
if article_update:
ET.SubElement(entry, "updated").text = article_update.isoformat(
timespec="seconds"
)
ET.SubElement(entry, "content", type="html").text = article.content()
entry["updated"] = article_update
feed.add_entry(entry)
# And write it on the filesystem
feed_dirpath = os.path.join(output_path, "feeds")
os.mkdir(feed_dirpath)
feed_filepath = os.path.join(feed_dirpath, "all.atom.xml")
ET.ElementTree(feed).write(feed_filepath, encoding="unicode", xml_declaration=True)
feed.write(feed_filepath)
if __name__ == "__main__":
......
import xml.etree.ElementTree as ET
class Atom:
def __init__(self):
self.feed = ET.Element("feed", xmlns="http://www.w3.org/2005/Atom")
def add_header(self, attributes):
ET.SubElement(self.feed, "title").text = attributes["title"]
ET.SubElement(
self.feed,
"link",
href=attributes["self_link"],
rel="self",
type="application/atom+xml",
)
ET.SubElement(
self.feed,
"link",
href=attributes["site_link"],
rel="alternate",
type="text/html",
)
ET.SubElement(self.feed, "id").text = attributes["id"]
ET.SubElement(self.feed, "updated").text = attributes["updated"].isoformat(
timespec="seconds"
)
def add_entry(self, attributes):
entry = ET.SubElement(self.feed, "entry")
ET.SubElement(entry, "title").text = attributes["title"]
ET.SubElement(entry, "id").text = attributes["id"]
if "author" in attributes:
author = ET.SubElement(entry, "author")
ET.SubElement(author, "name").text = attributes["author"]
ET.SubElement(
entry,
"link",
href=attributes["article_link"],
rel="alternate",
type="text/html",
)
ET.SubElement(entry, "published").text = attributes["published"].isoformat(
timespec="seconds"
)
if "updated" in attributes:
ET.SubElement(entry, "updated").text = attributes["updated"].isoformat(
timespec="seconds"
)
ET.SubElement(entry, "content", type="html").text = attributes["content"]
def write(self, filepath):
element_tree = ET.ElementTree(self.feed)
element_tree.write(filepath, encoding="unicode", xml_declaration=True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment