r/flask•
MGoRedditor

Question on How to Generate a RSS Feed

Hi all! I've built a blog from scratch in Flask that utilizes MongoEngine for post storage, and I'm attempting to create an RSS feed that would allow me to send post notifications out via MailChimp. Does anyone have a good method for doing so, possibly including code snippets or a github?

A lot of Googling has led me to either the dead AtomFeed or methods of reading RSS feeds into your site - not the other way around... Any help would be appreciated!

Posted Jan 29, 2020, 6:54 AM UTC

1011
EpocDotFrIntermediate•

I'm attempting to create an RSS feed that would allow me to send post notifications out via MailChimp

I'm not sure to understand what you are trying to achieve, anyway the python-feedgen package will allow you to painlessly generate RSS and Atom feeds.

# ...
from feedgen.feed import FeedGenerator
from flask import make_response
# ...

@app.route('/rss')
def rss():
    fg = FeedGenerator()
    fg.title('Feed title')
    fg.description('Feed description')
    fg.link(href='https://awesome.com')

    for article in get_news(): # get_news() returns a list of articles from somewhere
        fe = fg.add_entry()
        fe.title(article.title)
        fe.link(href=article.url)
        fe.description(article.content)
        fe.guid(article.id, permalink=False) # Or: fe.guid(article.url, permalink=True)
        fe.author(name=article.author.name, email=article.author.email)
        fe.pubDate(article.created_at)

    response = make_response(fg.rss_str())
    response.headers.set('Content-Type', 'application/rss+xml')

    return response
6
MGoRedditorOP•

This looks really useful, I will have to give it a shot! I already have a page set up that does similar.

1
EpocDotFrIntermediate•

So you need help more about the MailChimp stuff? If so, could you elaborate a bit? 😀

1
MGoRedditorOP•

Mailchimp has a pretty good guide already on how to monitor an RSS feed and send emails if there's anything new... I will just utilize that!

1
EpocDotFrIntermediate•

Mmmh, so the RSS feed you'd like to monitor is the one that you're generating right? In other words: do you own the data used to generate this feed?

1
[deleted]•

[deleted]

1
MGoRedditorOP•

Hmm is there a guide somewhere?

1
[deleted]•

[deleted]

1
MGoRedditorOP•

Thanks!

1