Add RSS and Atom feeds for topic listings

This commit is contained in:
Andrew Shu
2021-02-03 21:24:32 -08:00
committed by Deimos
parent 0404d0dfa2
commit a021b96bc7
10 changed files with 148 additions and 1 deletions

View File

@@ -19,6 +19,8 @@ from tildes.resources.user import user_by_username
def includeme(config: Configurator) -> None:
"""Set up application routes."""
config.add_route("home", "/")
config.add_route("home_atom", "/topics.atom")
config.add_route("home_rss", "/topics.rss")
config.add_route("search", "/search")
@@ -37,6 +39,8 @@ def includeme(config: Configurator) -> None:
config.add_route("new_topic", "/new_topic", factory=group_by_path)
config.add_route("group_topics", "/topics", factory=group_by_path)
config.add_route("group_topics_atom", "/topics.atom", factory=group_by_path)
config.add_route("group_topics_rss", "/topics.rss", factory=group_by_path)
config.add_route("group_search", "/search", factory=group_by_path)

View File

@@ -0,0 +1,14 @@
{# Copyright (c) 2021 Tildes contributors <code@tildes.net> #}
{# SPDX-License-Identifier: AGPL-3.0-or-later #}
<?xml version="1.0" encoding="UTF-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>{% block feed_title %}Tildes Atom feed{% endblock %}</title>
<id>{% block feed_id %}{{ request.current_route_url() }}{% endblock %}</id>
<link rel="self" href="{% block feed_link %}{{ request.current_route_url() }}{% endblock %}" />
<updated>{% block feed_updated %}{{ current_time.strftime("%Y-%m-%dT%H:%M:%SZ") }}{% endblock %}</updated>
{% block feed_entries %}{% endblock %}
</feed>

View File

@@ -55,6 +55,9 @@
<meta name="application-name" content="Tildes">
<meta name="msapplication-TileColor" content="#002b36">
{# RSS/Atom feeds #}
{% block link_alternate_feeds %}{% endblock %}
<title>{% block title_full %}{% block title %}{% endblock %} - Tildes{% endblock %}</title>
{% block templates %}{% endblock %}

View File

@@ -0,0 +1,15 @@
{# Copyright (c) 2021 Tildes contributors <code@tildes.net> #}
{# SPDX-License-Identifier: AGPL-3.0-or-later #}
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>{% block channel_title %}Tildes{% endblock %}</title>
<link>{% block channel_link %}https://tildes.net/{% endblock %}</link>
<description>{% block channel_description %}Tildes RSS feed{% endblock %}</description>
{% block channel_items %}{% endblock %}
</channel>
</rss>

View File

@@ -0,0 +1,6 @@
{# Copyright (c) 2021 Tildes contributors <code@tildes.net> #}
{# SPDX-License-Identifier: AGPL-3.0-or-later #}
{% extends 'topic_listing.atom.jinja2' %}
{% block feed_title %}Tildes Atom feed{% endblock %}

View File

@@ -0,0 +1,8 @@
{# Copyright (c) 2021 Tildes contributors <code@tildes.net> #}
{# SPDX-License-Identifier: AGPL-3.0-or-later #}
{% extends 'topic_listing.rss.jinja2' %}
{% block channel_title %}Tildes{% endblock %}
{% block channel_link %}https://tildes.net/{% endblock %}
{% block channel_description %}Topics RSS feed{% endblock %}

View File

@@ -0,0 +1,39 @@
{# Copyright (c) 2021 Tildes contributors <code@tildes.net> #}
{# SPDX-License-Identifier: AGPL-3.0-or-later #}
{% extends 'base.atom.jinja2' %}
{% block feed_title %}~{{ group.path }} - Tildes{% endblock %}
{% block feed_entries %}
{% for topic in topics %}
<entry>
<title><![CDATA[{{ topic.title }}]]></title>
<id>https://tildes.net{{ topic.permalink }}</id>
{% if topic.is_link_type %}
<link rel="alternate" href="{{ topic.link }}"/>
{% else %}
<link rel="alternate" href="{{ topic.permalink }}"/>
{% endif %}
<content type="html"><![CDATA[
{% if topic.is_link_type %}
<p>Link URL: <a href="{{ topic.link }}">{{ topic.link }}</a></p>
{% elif topic.is_text_type %}
{{ topic.rendered_html|safe }}
<hr/>
{% endif %}
<p>Comments URL: <a href="{{ topic.permalink }}">https://tildes.net{{ topic.permalink }}</a></p>
<p>Votes: {{ topic.num_votes }}</p>
<p>Comments: {{ topic.num_comments }}</p>
]]></content>
<author><name>{{ topic.user.username }}</name></author>
{% if topic.last_edited_time %}
<updated>{{ topic.last_edited_time.strftime("%Y-%m-%dT%H:%M:%SZ") }}</updated>
{% else %}
<updated>{{ topic.created_time.strftime("%Y-%m-%dT%H:%M:%SZ") }}</updated>
{% endif %}
</entry>
{% endfor %}
{% endblock %}

View File

@@ -10,6 +10,11 @@
{% block title %}Topics in ~{{ group.path }}{% endblock %}
{% block link_alternate_feeds %}
<link href="{{ request.current_route_path("topics.atom") }}" type="application/atom+xml" rel="alternate" title="Topics Atom feed" />
<link href="{{ request.current_route_path("topics.rss") }}" type="application/rss+xml" rel="alternate" title="Topics RSS feed" />
{% endblock %}
{% block header_context_link %}
{{ group_segmented_link(group) }}
{% endblock %}

View File

@@ -0,0 +1,37 @@
{# Copyright (c) 2021 Tildes contributors <code@tildes.net> #}
{# SPDX-License-Identifier: AGPL-3.0-or-later #}
{% extends 'base.rss.jinja2' %}
{% block channel_title %}~{{ group.path }} - Tildes{% endblock %}
{% block channel_link %}https://tildes.net/~{{ group.path }}{% endblock %}
{% block channel_description %}Topics in ~{{ group.path }}{% endblock %}
{% block channel_items %}
{% for topic in topics %}
<item>
<title><![CDATA[{{ topic.title }}]]></title>
{% if topic.is_link_type %}
<link>{{ topic.link }}</link>
{% else %}
<link>{{ topic.permalink }}</link>
{% endif %}
<description><![CDATA[
{% if topic.is_link_type %}
<p>Link URL: <a href="{{ topic.link }}">{{ topic.link }}</a></p>
{% elif topic.is_text_type %}
{{ topic.rendered_html|safe }}
<hr/>
{% endif %}
<p>Comments URL: <a href="{{ topic.permalink }}">https://tildes.net{{ topic.permalink }}</a></p>
<p>Votes: {{ topic.num_votes }}</p>
<p>Comments: {{ topic.num_comments }}</p>
]]></description>
<author>{{ topic.user.username }}</author>
<comments>{{ topic.permalink }}</comments>
<pubDate>{{ topic.created_time.strftime("%a, %d %b %Y %T %z") }}</pubDate>
</item>
{% endfor %}
{% endblock %}

View File

@@ -142,7 +142,11 @@ def post_group_topics(
@view_config(route_name="home", renderer="home.jinja2")
@view_config(route_name="home_atom", renderer="home.atom.jinja2")
@view_config(route_name="home_rss", renderer="home.rss.jinja2")
@view_config(route_name="group", renderer="topic_listing.jinja2")
@view_config(route_name="group_topics_atom", renderer="topic_listing.atom.jinja2")
@view_config(route_name="group_topics_rss", renderer="topic_listing.rss.jinja2")
@use_kwargs(TopicListingSchema())
def get_group_topics( # noqa
request: Request,
@@ -159,7 +163,9 @@ def get_group_topics( # noqa
# period needs special treatment so we can distinguish between missing and None
period = kwargs.get("period", missing)
is_home_page = request.matched_route.name == "home"
is_home_page = request.matched_route.name in ["home", "home_atom", "home_rss"]
is_atom = request.matched_route.name in ["home_atom", "group_topics_atom"]
is_rss = request.matched_route.name in ["home_rss", "group_topics_rss"]
if is_home_page:
# on the home page, include topics from the user's subscribed groups
@@ -192,6 +198,11 @@ def get_group_topics( # noqa
if period is missing:
period = default_settings.period
# force Newest sort order, and All Time period, for RSS feeds
if is_atom or is_rss:
order = TopicSortOption.NEW
period = None
# set up the basic query for topics
query = (
request.query(Topic)
@@ -285,6 +296,11 @@ def get_group_topics( # noqa
else:
financial_data = None
if is_atom:
request.response.content_type = "application/atom+xml"
if is_rss:
request.response.content_type = "application/rss+xml"
return {
"group": request.context,
"groups": groups,