RSS reader in Python for Discord

Jorge Castro - Feb 5 '23 - - Dev Community

Hey guys, I hope everyone is having a good day. Today I want to create a short article on how to read RSS feeds with Python and notify a Discord channel using Webhook. First of all I wanted to use some of the Discord bot alternatives on the market, but the free options were very limited for what I wanted šŸ˜…, itā€™s not bad to pay for the use of tools that make your life easier, but honestly I took this as an excuse to do some coding over the weekend. It is my hobby! ā¤ļø.

Letā€™s start

To get started, we need to search for any site with RSS support; in my case i searched for sources for android development and for this example i will use the following URL:

Once the project is created, we need to install the **feedparser **library to read rss with python.

pip install feedparser
Enter fullscreen mode Exit fullscreen mode

Next, weā€™ll create our main.py, add the webhook, username, and avatar url.

import feedparser

webhook = "https://discord.com/webhooks/{webhook_id}/{webhook_token}"
username = "Androd News"
avatar_url = "https://source.android.com/static/docs/setup/images/Android_symbol_green_RGB.png"

feed = feedparser.parse(feed_url)
print(feed)
Enter fullscreen mode Exit fullscreen mode

If everything is ok, when you run the program you should be able to see a terminal output similar to the following image

Example of RSS reader result

Letā€™s continue, the next thing we will do is create a class to manage our Discord data and have the ability to launch notifications. We will call our class discord_module/DiscordNews.py

import requests

class DiscordNews:

    def __init__(self, webhook, username, avatar_url, feed):
        self.webhook = webhook
        self.username = username
        self.avatar_url = avatar_url
        self.feed = feed

    def prepare_and_notify(self):
        for entry in self.feed.entries:
            self.__notify_to_discord_channel(entry)

    def __notify_to_discord_channel(self, data):
        # Code for notify to Discord channel
Enter fullscreen mode Exit fullscreen mode

As you may have noticed we have 2 parameters in the class constructor: avatar_url and username, these are used to customize the posting of the message in the Discord channel; with avatar_url we specify a url of an image that will be the icon of our bot and username will be its name.

details of the botā€™s post on the Discord channel

The next thing is to import our class into main.py and call the function prepare_and_notify

discord = DiscordNews(webhook, username, avatar_url, feed)
discord.prepare_and_notify()
Enter fullscreen mode Exit fullscreen mode

Finally, we run the program.

python main.py
Enter fullscreen mode Exit fullscreen mode

Divider

Result

Example of messages in Discord channel

See the full code here

Divider

If you like my content and want to support my work, you can give me a cup of coffee ā˜•ļø šŸ„°

Ko-fi

Buy me a coffee

Follow me in

. . . . . . . . . . .
Terabox Video Player