Atom RSS Feed Generator with Python Flask
An Atom feed is very similar to an RSS feed in that it is a lightweight XML format allowing for easy syndication of web content. In fact, most RSS readers and news aggregators will be able to read Atom feeds just fine, as it is becoming a widely-used alternative to RSS feeds.
In this post we will generate feeds using python module called werkzeug.
feed_url = URL Route of Published Feed. Example : http://www.example.com/feeds
url = Root URL of Blog. Example : http://www.example.com
Once feed object is created we would call feed.add()
title = Post Title
summary = Blog post summary.
content_type = html Example : http://www.example.com
author = Name of author.
url = Absolute URL for the blog post. http://www.example.com/my-post
updated = Last updated date and time in UTC format.
published = Created date and time in UTC format.
#app.py from flask import Flask, render_template, request from werkzeug.contrib.atom import AtomFeed from urllib.parse import urljoin from flask_sqlalchemy import SQLAlchemy from datetime import datetime app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///devdb.db' app.config['SECRET_KEY'] = 'cairocoders-ednalan' db = SQLAlchemy(app) class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(140)) content = db.Column(db.String(140)) author_name = db.Column(db.String(140)) url = db.Column(db.String(140)) mod_date = db.Column(db.DateTime, default=datetime.utcnow) created_date = db.Column(db.DateTime, default=datetime.utcnow) def get_abs_url(url): """ Returns absolute url by joining post url with base url """ return urljoin(request.url_root, url) @app.route('/feeds/') def feeds(): # feed = AtomFeed(title='Last 10 Posts from My Blog', # feed_url="https://tutorial101.blogspot.com/feeds/", # url="https://tutorial101.blogspot.com/") feed = AtomFeed(title='Last 10 Posts from My Blog', feed_url=request.url, url=request.url_root) posts = Post.query.filter().all() for post in posts: feed.add(post.title, post.content, content_type='html', author= post.author_name, url=get_abs_url(post.url), updated=post.mod_date, published=post.created_date) return feed.get_response() if __name__ == '__main__': app.run(debug=True)