Commit 7bfac8f4 authored by berumuron's avatar berumuron
Browse files

Refactor feeds generation

parent f6743658
...@@ -5,10 +5,10 @@ import sys ...@@ -5,10 +5,10 @@ import sys
import distutils.dir_util import distutils.dir_util
import locale import locale
import boop.boopfeed as boopfeed
import boop.environment as environment import boop.environment as environment
import boop.configuration as config import boop.configuration as config
import boop.utils as utils import boop.utils as utils
from boop.feed import Feed
locale.setlocale(locale.LC_ALL, "") locale.setlocale(locale.LC_ALL, "")
...@@ -115,51 +115,34 @@ def main(environment): ...@@ -115,51 +115,34 @@ def main(environment):
print(f"Written article: {article.url()}") print(f"Written article: {article.url()}")
# STEP 3: we write also the Atom feeds (the main one + the series'). # STEP 3: we write also the Atom feeds (the main one + the series').
# Just create some variables so it's easier to manipulate after # We start by collecting the different feeds
site_url = configuration["SITE_URL"] feeds = []
site_uuid = configuration["SITE_UUID"]
site_title = configuration["SITE_TITLE"]
feed_dirpath = os.path.join(output_path, "feeds")
os.mkdir(feed_dirpath)
if len(articles) > 0: if len(articles) > 0:
# Build the main Atom feed # The main one...
feed = boopfeed.Atom() feed = Feed("feeds/all", articles, configuration=configuration)
feed.add_header( feeds.append(feed)
{
"title": site_title,
"self_link": f"{site_url}feeds/{feed_filename}",
"site_link": site_url,
"id": f"urn:uuid:{site_uuid}",
"updated": articles[0].update() or articles[0].date(),
}
)
utils.add_articles_to_feed(feed, articles)
feed_filepath = os.path.join(feed_dirpath, "all.atom.xml")
feed.write(feed_filepath)
print(f"Written feed: {site_url}feeds/{feed_filename}")
# Now, build the feeds for series (same process)
for serie, serie_articles in articles_by_series.items(): for serie, serie_articles in articles_by_series.items():
feed_filename = f"{os.path.basename(serie.slug())}.atom.xml" # And the series' ones
feed_filepath = os.path.join(feed_dirpath, feed_filename) if len(serie_articles) == 0:
continue
feed = boopfeed.Atom()
feed.add_header( slug = f"feeds/{os.path.basename(serie.slug())}"
{ feed_title = f"{serie.title()} - {configuration['SITE_TITLE']}"
"title": f"{serie.title()} - {site_title}", feed = Feed(
"self_link": f"{site_url}feeds/{feed_filename}", slug,
"site_link": serie.url(), serie_articles,
"id": f"urn:uuid:{serie.uuid()}", {"title": feed_title, "site_link": serie.url()},
"updated": serie_articles[0].update() or serie_articles[0].date(), configuration=configuration,
}
) )
utils.add_articles_to_feed(feed, serie_articles) feeds.append(feed)
feed.write(feed_filepath) if len(feeds) > 0:
print(f"Written feed: {site_url}feeds/{feed_filename}") # If we have collected feeds (i.e. we have at least one article), we
# write them.
for feed in feeds:
utils.write_feed(feed, output_path)
print(f"Written feed: {feed.url()}")
# STEP 4: and we finish by copying the static folder. # STEP 4: and we finish by copying the static folder.
static_dirpath = os.path.join(os.curdir, "static") static_dirpath = os.path.join(os.curdir, "static")
......
import uuid
class Feed:
def __init__(self, slug, articles, meta={}, configuration={}):
self.meta = {}
self.meta.update(configuration)
self.meta["FEED_SLUG"] = slug
self.meta["FEED_TITLE"] = configuration["SITE_TITLE"]
self.meta["FEED_SITE_LINK"] = configuration["SITE_URL"]
for key, value in meta.items():
feed_key = f"FEED_{key.upper()}"
self.meta[feed_key] = value
self.meta["FEED_ARTICLES"] = articles
def slug(self):
return self.meta["FEED_SLUG"]
def title(self):
return self.meta.get("FEED_TITLE", "A feed")
def url(self):
site_url = self.meta.get("SITE_URL", "")
return f"{site_url}{self.slug()}.atom.xml"
def site_link(self):
return self.meta["FEED_SITE_LINK"]
def uuid(self):
return str(uuid.uuid5(uuid.NAMESPACE_URL, self.url()))
def articles(self):
return self.meta["FEED_ARTICLES"]
...@@ -2,6 +2,7 @@ import os ...@@ -2,6 +2,7 @@ import os
import shutil import shutil
import boop.boopsy as boopsy import boop.boopsy as boopsy
from boop.atom import Atom
from boop.article import Article from boop.article import Article
from boop.page import Page from boop.page import Page
...@@ -18,28 +19,6 @@ def init_output_folder(output_path): ...@@ -18,28 +19,6 @@ def init_output_folder(output_path):
os.mkdir(output_path) os.mkdir(output_path)
def add_articles_to_feed(feed, articles):
"""Add article as standard entries to the feed.
"""
for article in articles:
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:
entry["author"] = article_author
article_update = article.update()
if article_update:
entry["updated"] = article_update
feed.add_entry(entry)
def build_article_from_filepath(filepath, configuration, serie=None): def build_article_from_filepath(filepath, configuration, serie=None):
"""Read a file and initialize an Article from it. """Read a file and initialize an Article from it.
""" """
...@@ -101,6 +80,45 @@ def write_content(content, output_path): ...@@ -101,6 +80,45 @@ def write_content(content, output_path):
output_file.write(rendered_content) output_file.write(rendered_content)
def write_feed(feed, output_path):
"""Write a feed as an Atom feed under output_path.
"""
feed_filepath = os.path.join(output_path, f"{feed.slug()}.atom.xml")
articles = feed.articles()
atom = Atom()
atom.add_header(
{
"title": feed.title(),
"self_link": feed.url(),
"site_link": feed.site_link(),
"id": f"urn:uuid:{feed.uuid()}",
"updated": articles[0].update() or articles[0].date(),
}
)
for article in articles:
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:
entry["author"] = article_author
article_update = article.update()
if article_update:
entry["updated"] = article_update
atom.add_entry(entry)
mkdirs_for_file(feed_filepath)
atom.write(feed_filepath)
def list_dirs(path): def list_dirs(path):
"""List all the directories within a path. """List all the directories within a path.
""" """
......
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