Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Add RSS endpoint for news #109

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cernopendata/modules/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Endpoints accessible via the CERN Open Data Portal API."""
61 changes: 61 additions & 0 deletions cernopendata/modules/api/news.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""News endpoint accessible via the CERN Open Data Portal API."""

from datetime import datetime

from flask import Blueprint, Response, request

from ..pages.utils import FeaturedArticlesSearch

blueprint = Blueprint("cernopendata_api_news", __name__)


@blueprint.route("/news.xml", methods=["GET"])
def get_latest_news():
"""Returns the set amount of latest news from the Open Data Portal."""
limit = request.args.get("limit", default=10, type=int)
limit = min(abs(limit), 128)

try:
news = (
FeaturedArticlesSearch().sort("-date_published")[:limit].execute().hits.hits
)
except Exception:
news = []

rss_items = "\n".join(
[
f"""
<item>
<title>{article.get("title", "CERN Open Data update")}</title>
<link>
https://opendata.cern.ch/docs/{article.get("slug", "")}
</link>
<pubDate>{
datetime.strptime(article.get("date_published", "2000-01-01"), "%Y-%m-%d")
.strftime("%a, %d %b %Y 00:00:00 +0000")
}</pubDate>
<description>
Author: {article.get("author", "Open Data Portal team")}
</description>
</item>
"""
for a in news
if (article := a.get("_source"))
]
)

rss_feed = f"""<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>CERN Open Data RSS News</title>
<link>https://opendata.cern.ch/</link>
<description>
Latest news from CERN Open Data
</description>
<language>en</language>
{rss_items}
</channel>
</rss>
"""

return Response(rss_feed, mimetype="application/xml")
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@
"invenio_base.api_apps": [
"cernopendata_xrootd = cernopendata.modules.xrootd:CODPXRootD"
],
"invenio_base.api_blueprints": [
"cernopendata_news_api = cernopendata.modules.api.news:blueprint",
],
"invenio_base.blueprints": [
"cernopendata = cernopendata.views:blueprint",
"cernopendata_pages = " "cernopendata.modules.pages.views:blueprint",
Expand Down