diff options
author | osmarks <osmarks> | 2021-06-18 16:40:21 +0000 |
---|---|---|
committer | osmarks <osmarks> | 2021-06-18 16:40:21 +0000 |
commit | 87650ea73d719f8caf3859f27eb12fa651fc774f (patch) | |
tree | 5b6a4799d9a4d534728a25b399fac2c5fbbdc970 | |
parent | f9c6526cdfabdea7352dfd499e041b8ad4de0f01 (diff) |
basic RSS capability, needs a refactor and probably improvement
-rw-r--r-- | apioforum/__init__.py | 5 | ||||
-rw-r--r-- | apioforum/forum.py | 13 | ||||
-rw-r--r-- | apioforum/templates/rss.xml | 17 | ||||
-rw-r--r-- | apioforum/thread.py | 13 | ||||
-rw-r--r-- | apioforum/util.py (renamed from apioforum/fuzzy.py) | 4 |
5 files changed, 45 insertions, 7 deletions
diff --git a/apioforum/__init__.py b/apioforum/__init__.py index 7baa7c1..9a989cc 100644 --- a/apioforum/__init__.py +++ b/apioforum/__init__.py @@ -29,8 +29,9 @@ def create_app(): from . import thread app.register_blueprint(thread.bp) - from .fuzzy import fuzzy - app.jinja_env.filters['fuzzy']=fuzzy + from .util import fuzzy, rss_datetime + app.jinja_env.filters["fuzzy"]=fuzzy + app.jinja_env.filters["rss_datetime"]=rss_datetime app.add_url_rule("/",endpoint="index") diff --git a/apioforum/forum.py b/apioforum/forum.py index 81674a8..92e981c 100644 --- a/apioforum/forum.py +++ b/apioforum/forum.py @@ -3,10 +3,11 @@ from flask import ( Blueprint, render_template, request, - g, redirect, url_for, flash + g, redirect, url_for, flash, Response ) from .db import get_db from .mdrender import render +from .thread import post_jump bp = Blueprint("forum", __name__, url_prefix="/") @@ -77,4 +78,12 @@ def search(): display_thread_id[ix] = False last_thread = result["thread"] rendered_posts = [render(q['content']) for q in results] - return render_template("search_results.html", results=results, query=query, rendered_posts=rendered_posts, display_thread_id=display_thread_id)
\ No newline at end of file + return render_template("search_results.html", results=results, query=query, rendered_posts=rendered_posts, display_thread_id=display_thread_id) + +@bp.route("/rss") +def rss_feed(): + db = get_db() + print(request.host_url) + items = db.execute("SELECT * FROM posts ORDER BY updated DESC LIMIT 50") + items = [ { **item, "rendered": render(item["content"]), "link": request.base_url + post_jump(item["thread"], item["id"]), "updated": item["updated"] or item["created"] } for item in items ] + return Response(render_template("rss.xml", title="New posts feed", description="The latest posts on the Apioforum", link=request.base_url, items=items), mimetype="text/xml")
\ No newline at end of file diff --git a/apioforum/templates/rss.xml b/apioforum/templates/rss.xml new file mode 100644 index 0000000..8ff7a72 --- /dev/null +++ b/apioforum/templates/rss.xml @@ -0,0 +1,17 @@ +<rss version="2.0"> + <channel> + <title>{{title}}</title> + <link>{{link}}</link> + <description>{{description}}</description> + <generator>Apioforum internal RSS generation</generator> + {% for item in items %} + <item> + <description>{{item.rendered}}</description> + <pubDate>{{item.updated | rss_datetime}}</pubDate> + <link>{{item.link}}</link> + <author>{{item.author}}</author> + <guid>{{item.id}}</guid> + </item> + {% endfor %} + </channel> +</rss>
\ No newline at end of file diff --git a/apioforum/thread.py b/apioforum/thread.py index 630cb33..2c7366e 100644 --- a/apioforum/thread.py +++ b/apioforum/thread.py @@ -2,7 +2,7 @@ from flask import ( Blueprint, render_template, abort, request, g, redirect, - url_for, flash + url_for, flash, Response ) from .db import get_db from .mdrender import render @@ -72,7 +72,7 @@ def delete_post(post_id): flash("post deleted deletedly") return redirect(url_for("thread.view_thread",thread_id=post["thread"])) else: - return render_template("delete_post.html",post=post,rendered_content=render_md(post["content"])) + return render_template("delete_post.html",post=post,rendered_content=render(post["content"])) @bp.route("/edit_post/<int:post_id>",methods=["GET","POST"]) @@ -104,5 +104,12 @@ def edit_post(post_id): else: flash(err) return render_template("edit_post.html",post=post) - +@bp.route("/<int:thread_id>/rss") +def rss_feed(thread_id): + db = get_db() + thread = db.execute("SELECT * FROM threads WHERE id = ?", (thread_id,)).fetchone() + items = db.execute("SELECT * FROM posts WHERE thread = ? ORDER BY updated DESC LIMIT 50", (thread_id,)) + items = [ { **item, "rendered": render(item["content"]), "link": request.base_url + post_jump(item["thread"], item["id"]), "updated": item["updated"] or item["created"] } for item in items ] + return Response(render_template("rss.xml", description=f"Posts in thread {thread_id}", title=f'''{thread['title']} - Apioforum''', + link=request.base_url.rstrip("/rss"), items=items), mimetype="text/xml")
\ No newline at end of file diff --git a/apioforum/fuzzy.py b/apioforum/util.py index 8396b8f..6384383 100644 --- a/apioforum/fuzzy.py +++ b/apioforum/util.py @@ -9,6 +9,7 @@ units = ( ) from datetime import datetime, timedelta, timezone +import email.utils def fuzzy(seconds, ago=False): if isinstance(seconds, timedelta): @@ -33,3 +34,6 @@ def fuzzy(seconds, ago=False): if not buf: return "now" return fmt.format(buf) + +def rss_datetime(dt): + return email.utils.format_datetime(dt.replace(tzinfo=timezone.utc))
\ No newline at end of file |