Merge pull request 'Add RSS feed at /rss/' (#11) from rss-feed into production
Reviewed-on: #11
This commit is contained in:
commit
55547fd076
2 changed files with 76 additions and 0 deletions
|
|
@ -5,6 +5,7 @@ from werkzeug.exceptions import abort
|
||||||
|
|
||||||
from flaskr.auth import login_required
|
from flaskr.auth import login_required
|
||||||
from flaskr.db import get_db
|
from flaskr.db import get_db
|
||||||
|
from flaskr.generate_rss import generate_feed
|
||||||
|
|
||||||
import markdown
|
import markdown
|
||||||
import datetime
|
import datetime
|
||||||
|
|
@ -66,6 +67,7 @@ def create():
|
||||||
(title, body, g.user['id'])
|
(title, body, g.user['id'])
|
||||||
)
|
)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
generate_feed(db)
|
||||||
return redirect(url_for('blog.index'))
|
return redirect(url_for('blog.index'))
|
||||||
|
|
||||||
return render_template('blog/create.html')
|
return render_template('blog/create.html')
|
||||||
|
|
@ -119,6 +121,7 @@ def update(id):
|
||||||
(title, body, created, id)
|
(title, body, created, id)
|
||||||
)
|
)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
generate_feed(db)
|
||||||
return redirect(url_for('blog.index'))
|
return redirect(url_for('blog.index'))
|
||||||
|
|
||||||
return render_template('blog/update.html', post=post)
|
return render_template('blog/update.html', post=post)
|
||||||
|
|
@ -131,3 +134,7 @@ def delete(id):
|
||||||
db.execute('DELETE FROM post WHERE id = ?',(id,))
|
db.execute('DELETE FROM post WHERE id = ?',(id,))
|
||||||
db.commit()
|
db.commit()
|
||||||
return redirect(url_for('blog.index'))
|
return redirect(url_for('blog.index'))
|
||||||
|
|
||||||
|
@bp.route('/rss/', methods=('GET',))
|
||||||
|
def redirect_rss():
|
||||||
|
return redirect(url_for('static', filename='rss.xml'))
|
||||||
|
|
|
||||||
69
flaskr/generate_rss.py
Normal file
69
flaskr/generate_rss.py
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
from datetime import datetime
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
from markdown import markdown
|
||||||
|
|
||||||
|
def soup_append_multiple_tags(soup: BeautifulSoup, children: dict) -> None:
|
||||||
|
for name, string in children.items():
|
||||||
|
soup.append(soup.new_tag(name))
|
||||||
|
soup.find_all(name)[-1].string = string
|
||||||
|
|
||||||
|
def post_find_first_plain_line(post: str) -> str:
|
||||||
|
while post:
|
||||||
|
if post[0].isalnum():
|
||||||
|
break
|
||||||
|
end = post.find("\n")
|
||||||
|
post = post[end+1:]
|
||||||
|
end = post.find("\n")
|
||||||
|
return post[:end]
|
||||||
|
|
||||||
|
def generate_feed(db):
|
||||||
|
soup = BeautifulSoup(features="xml")
|
||||||
|
|
||||||
|
soup.append(soup.new_tag("rss", version="2.0"))
|
||||||
|
rss = soup.rss
|
||||||
|
rss["xmlns:atom"] = "http://www.w3.org/2005/Atom"
|
||||||
|
rss["xmlns:content"] = "http://purl.org/rss/1.0/modules/content/"
|
||||||
|
|
||||||
|
rss.append(soup.new_tag("channel"))
|
||||||
|
channel = rss.channel
|
||||||
|
|
||||||
|
channel_children = {
|
||||||
|
"description": "Blog posts from allsparkinfinite's blog",
|
||||||
|
"link": "https://blog.allsparkinfinite.name",
|
||||||
|
"title": "blogsparkinfinite",
|
||||||
|
"docs": "https://rssboard.org/rss-specification",
|
||||||
|
"language": "en-IN",
|
||||||
|
"lastBuildDate": datetime.now().strftime("%a, %d %b %Y %T +0530"),
|
||||||
|
"ttl": "240",
|
||||||
|
}
|
||||||
|
soup_append_multiple_tags(channel, channel_children)
|
||||||
|
|
||||||
|
channel.append(channel.new_tag("atom:link"))
|
||||||
|
channel.find_all("atom:link")[-1]["href"] = "https://blog.allsparkinfinite.name/rss/"
|
||||||
|
channel.find_all("atom:link")[-1]["rel"] = "self"
|
||||||
|
channel.find_all("atom:link")[-1]["type"] = "application/rss+xml"
|
||||||
|
|
||||||
|
channel.append(channel.new_tag("atom:link"))
|
||||||
|
channel.find_all("atom:link")[-1]["href"] = "https://blog.allsparkinfinite.name/"
|
||||||
|
channel.find_all("atom:link")[-1]["rel"] = "alternate"
|
||||||
|
channel.find_all("atom:link")[-1]["type"] = "text/html"
|
||||||
|
|
||||||
|
posts = db.execute("SELECT * FROM post").fetchall()
|
||||||
|
posts.reverse()
|
||||||
|
|
||||||
|
for post in posts:
|
||||||
|
channel.append(soup.new_tag("item"))
|
||||||
|
item = channel.find_all("item")[-1]
|
||||||
|
item_children = {
|
||||||
|
"description": post_find_first_plain_line(post[4]),
|
||||||
|
"guid": f"https://blog.allsparkinfinite.name/{post[0]}/",
|
||||||
|
"pubDate": post[2].strftime("%a, %d %b %Y %T GMT"),
|
||||||
|
"title": post[3],
|
||||||
|
"content:encoded": markdown(post[4]),
|
||||||
|
}
|
||||||
|
soup_append_multiple_tags(item, item_children)
|
||||||
|
|
||||||
|
with open("flaskr/static/rss.xml", "w") as f:
|
||||||
|
f.write(soup.prettify())
|
||||||
Loading…
Add table
Add a link
Reference in a new issue