|
| 1 | +""" |
| 2 | +Create an RSS feed of tutorials |
| 3 | +
|
| 4 | +Cribbed from: https://github.com/python/peps/blob/main/pep_sphinx_extensions/generate_rss.py |
| 5 | +""" |
| 6 | + |
| 7 | +from dataclasses import dataclass, asdict |
| 8 | +from datetime import datetime, UTC |
| 9 | +from email.utils import format_datetime |
| 10 | +from html import escape |
| 11 | +from pprint import pformat |
| 12 | +from typing import TYPE_CHECKING |
| 13 | +from urllib.parse import urljoin |
| 14 | + |
| 15 | +if TYPE_CHECKING: |
| 16 | + from sphinx.application import Sphinx |
| 17 | + |
| 18 | + |
| 19 | +def _format_rfc_2822(dt: datetime) -> str: |
| 20 | + datetime = dt.replace(tzinfo=UTC) |
| 21 | + return format_datetime(datetime, usegmt=True) |
| 22 | + |
| 23 | + |
| 24 | +@dataclass |
| 25 | +class RSSItem: |
| 26 | + title: str |
| 27 | + date: datetime |
| 28 | + description: str |
| 29 | + url: str |
| 30 | + author: str = "pyOpenSci" |
| 31 | + |
| 32 | + @classmethod |
| 33 | + def from_meta(cls, page_name: str, meta: dict, app: "Sphinx") -> "RSSItem": |
| 34 | + """Create from a page's metadata""" |
| 35 | + url = urljoin(app.config.html_baseurl, app.builder.get_target_uri(page_name)) |
| 36 | + # purposely don't use `get` here because we want to error if these fields are absent |
| 37 | + return RSSItem( |
| 38 | + title=meta[":og:title"], |
| 39 | + description=meta[":og:description"], |
| 40 | + date=datetime.fromisoformat(meta["date"]), |
| 41 | + author=meta.get(":og:author", "pyOpenSci"), |
| 42 | + url=url, |
| 43 | + ) |
| 44 | + |
| 45 | + def render(self) -> str: |
| 46 | + return f"""\ |
| 47 | +<item> |
| 48 | + <title>{escape(self.title, quote=False)}</title> |
| 49 | + <link>{escape(self.url, quote=False)}</link> |
| 50 | + <description>{escape(self.description, quote=False)}</description> |
| 51 | + <author>{escape(self.author, quote=False)}</author> |
| 52 | + <guid isPermaLink="true">{self.url}</guid> |
| 53 | + <pubDate>{_format_rfc_2822(self.date)}</pubDate> |
| 54 | +</item>""" |
| 55 | + |
| 56 | + |
| 57 | +@dataclass |
| 58 | +class RSSFeed: |
| 59 | + items: list[RSSItem] |
| 60 | + last_build_date: datetime = datetime.now() |
| 61 | + title: str = "pyOpenSci Tutorials" |
| 62 | + link: str = "https://www.pyopensci.org/python-package-guide/tutorials/intro.html" |
| 63 | + self_link: str = "https://www.pyopensci.org/python-package-guide/tutorials.rss" |
| 64 | + description: str = "Tutorials for learning python i guess!!!" |
| 65 | + language: str = "en" |
| 66 | + |
| 67 | + def render(self) -> str: |
| 68 | + items = sorted(self.items, key=lambda i: i.date, reverse=True) |
| 69 | + items = "\n".join([item.render() for item in items]) |
| 70 | + return f"""\ |
| 71 | +<?xml version='1.0' encoding='UTF-8'?> |
| 72 | +<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0"> |
| 73 | + <channel> |
| 74 | + <title>{self.title}</title> |
| 75 | + <link>{self.link}</link> |
| 76 | + <atom:link href="{self.self_link}" rel="self"/> |
| 77 | + <description>{self.description}</description> |
| 78 | + <language>{self.language}</language> |
| 79 | + <lastBuildDate>{_format_rfc_2822(self.last_build_date)}</lastBuildDate> |
| 80 | +{items} |
| 81 | + </channel> |
| 82 | +</rss> |
| 83 | + """ |
| 84 | + |
| 85 | + |
| 86 | +def generate_tutorials_feed(app: "Sphinx"): |
| 87 | + from sphinx.util import logging |
| 88 | + |
| 89 | + logger = logging.getLogger("_ext.rss") |
| 90 | + logger.info("Generating RSS feed for tutorials") |
| 91 | + metadata = app.builder.env.metadata |
| 92 | + tutorials = [t for t in metadata if t.startswith("tutorials/")] |
| 93 | + feed_items = [RSSItem.from_meta(t, metadata[t], app) for t in tutorials] |
| 94 | + feed = RSSFeed(items=feed_items) |
| 95 | + with open(app.outdir / "tutorials.rss", "w") as f: |
| 96 | + f.write(feed.render()) |
| 97 | + |
| 98 | + logger.info( |
| 99 | + f"Generated RSS feed for tutorials, wrote to {app.outdir / 'tutorials.rss'}" |
| 100 | + ) |
| 101 | + logger.debug(f"feed items: \n{pformat([asdict(item) for item in feed_items])}") |
0 commit comments