Skip to content

Commit

Permalink
Merge pull request #388 from knatten/rss-atom
Browse files Browse the repository at this point in the history
Rss atom
  • Loading branch information
knatten authored Feb 21, 2025
2 parents 88a1b15 + 0244780 commit 1ba994b
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions cppquiz/local_settings_example.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from . import settings

settings.SITE_URL = "https://cppquiz.org"
settings.DEBUG = True
settings.ALLOWED_HOSTS += ['127.0.0.1']
settings.ADMINS = (
Expand Down
2 changes: 2 additions & 0 deletions deployment/dreamhost/cronjobs/dump_published.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

source $HOME/sites/cppquiz.org/venv/bin/activate || exit $?
python $HOME/sites/cppquiz.org/cppquiz/manage.py dump_published_questions > $HOME/static.cppquiz.org/published.json || exit $?
python $HOME/sites/cppquiz.org/cppquiz/manage.py generate_feeds rss > $HOME/static.cppquiz.org/rss.xml || exit $?
python $HOME/sites/cppquiz.org/cppquiz/manage.py generate_feeds atom > $HOME/static.cppquiz.org/atom.xml || exit $?

87 changes: 87 additions & 0 deletions quiz/management/commands/generate_feeds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import datetime

from django.core.management.base import BaseCommand
from django.urls import reverse


from quiz.util import get_published_questions
from cppquiz import settings

rss_header = """<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>CppQuiz - New Questions</title>
<link>https://cppquiz.org</link>
<description>Latest C++ quiz questions from CppQuiz.org</description>
<language>en-us</language>
<lastBuildDate>{{last_build_date}}</lastBuildDate>
"""

rss_question = """
<item>
<title>Question {{id}}</title>
<link>{{url}}</link>
<description>Question {{id}} on CppQuiz.org</description>
<pubDate>{{pubdate}}</pubDate>
<guid>{{url}}</guid>
</item>
"""

rss_footer = """
</channel>
</rss>
"""


atom_header = """<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>CppQuiz - New Questions</title>
<link href="https://cppquiz.org/" />
<updated>{{last_build_date}}</updated>
<author>
<name>CppQuiz</name>
</author>
<id>https://cppquiz.org/</id>
"""

atom_question = """
<entry>
<title>Question {{id}}</title>
<link href="{{url}}" />
<id>{{url}}</id>
<updated>{{pubdate}}</updated>
<summary>Question {{id}} on CppQuiz.org</summary>
</entry>
"""

atom_footer = """
</feed>
"""


class Command(BaseCommand):
help = "Generate an RSS or Atom feed for published questions."
post_count = 20

def add_arguments(self, parser):
parser.add_argument(
"format", choices=["rss", "atom"],
help="Choose feed format: 'rss' or 'atom'"
)

def handle(self, *args, **options):
feed_format = options["format"]
header = atom_header if feed_format == "atom" else rss_header
question = atom_question if feed_format == "atom" else rss_question
footer = atom_footer if feed_format == "atom" else rss_footer
time_format = "%Y-%m-%dT%H:%M:%SZ" if feed_format == "atom" else "%a, %d %b %Y %H:%M:%S GMT"

last_build_date = datetime.datetime.now().strftime(time_format)
print(header.replace("{{last_build_date}}", last_build_date))

for q in get_published_questions().order_by('-publish_time')[:self.post_count]:
url = settings.SITE_URL + reverse('quiz:question', args=[q.pk])
print(question.replace("{{id}}", str(q.id)).replace("{{url}}", url).replace(
"{{pubdate}}", q.publish_time.strftime(time_format)))

print(footer)
4 changes: 4 additions & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<link type="text/css" rel="stylesheet" href="{{STATIC_URL}}highlight.css">
<link type="text/css" rel="stylesheet" href="{{STATIC_URL}}quiz.css?v=5">
<link rel="shortcut icon" href="{{STATIC_URL}}favicon.png">
<link rel="alternate" type="application/rss+xml" title="CppQuiz RSS" href="{{STATIC_URL}}rss.xml">
<link rel="alternate" type="application/atom+xml" title="CppQuiz Atom" href="{{STATIC_URL}}atom.xml">

<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-BSF86HNJZY"></script>
Expand Down Expand Up @@ -107,6 +109,8 @@ <h1><a href="/">C++ Quiz</a></h1>
<a href="https://github.com/knatten/cppquiz/blob/master/CODE_OF_CONDUCT.md">CoC</a> |
<a href="https://mastodon.online/@cppquiz"><img src="{{STATIC_URL}}/mastodon.png" height="15" alt="Mastodon"> Mastodon</a> |
<a href="https://bsky.app/profile/cppquiz.bsky.social"><img src="{{STATIC_URL}}/bluesky.png" height="15" alt="Bluesky"> Bluesky</a> |
<a href="{{STATIC_URL}}rss.xml">RSS</a> |
<a href="{{STATIC_URL}}atom.xml">Atom</a> |
<a href="https://github.com/knatten/cppquiz"> GitHub</a> |
© <a href="http://knatten.org">Anders Schau Knatten</a> {% now "Y"%}.
</p>
Expand Down

0 comments on commit 1ba994b

Please sign in to comment.