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 ...@@ -12,9 +12,8 @@ import uuid
import markdown import markdown
import pytz import pytz
import xml.etree.ElementTree as ET
import boopsy import boopsy
import boopfeed
locale.setlocale(locale.LC_ALL, "") locale.setlocale(locale.LC_ALL, "")
...@@ -196,10 +195,6 @@ def main(): ...@@ -196,10 +195,6 @@ def main():
with open(output_filepath, "w") as output_file: with open(output_filepath, "w") as output_file:
output_file.write(article.render()) 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 # Just create some variables so it's easier to manipulate after
site_url = configuration["SITE_URL"] site_url = configuration["SITE_URL"]
site_uuid = configuration["SITE_UUID"] site_uuid = configuration["SITE_UUID"]
...@@ -208,45 +203,42 @@ def main(): ...@@ -208,45 +203,42 @@ def main():
timezone = pytz.timezone(site_timezone) timezone = pytz.timezone(site_timezone)
current_datetime = datetime.datetime.now(tz=timezone) current_datetime = datetime.datetime.now(tz=timezone)
# Start to build the XML feed with the root information # Build the Atom feed
feed = ET.Element("feed", xmlns="http://www.w3.org/2005/Atom") feed = boopfeed.Atom()
ET.SubElement(feed, "title").text = site_title feed.add_header(
ET.SubElement( {
feed, "title": site_title,
"link", "self_link": f"{site_url}feeds/all.atom.xml",
href=f"{site_url}feeds/all.atom.xml", "site_link": site_url,
rel="self", "id": f"urn:uuid:{site_uuid}",
type="application/atom+xml", "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: for article in articles:
# and complete it for each article # and complete it for each article
entry = ET.SubElement(feed, "entry") entry = {
ET.SubElement(entry, "title").text = article.title() "title": article.title(),
ET.SubElement(entry, "id").text = f"urn:uuid:{article.uuid()}" "id": f"urn:uuid:{article.uuid()}",
"article_link": article.url(),
"published": article.date(),
"content": article.content(),
}
article_author = article.author() article_author = article.author()
if article_author: if article_author:
author = ET.SubElement(entry, "author") entry["author"] = article_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"
)
article_update = article.update() article_update = article.update()
if article_update: if article_update:
ET.SubElement(entry, "updated").text = article_update.isoformat( entry["updated"] = article_update
timespec="seconds"
) feed.add_entry(entry)
ET.SubElement(entry, "content", type="html").text = article.content()
# And write it on the filesystem # 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") 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__": 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