RSS reader in Python for Discord
Tags: python, rss, tutorial
Published: 2023-02-05 06:17:24

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:

* [https://medium.com/feed/@devjorgecastro](https://medium.com/feed/@devjorgecastro)

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

```bash
pip install feedparser
```

Next, we’ll create our main.py, add the webhook, username, and avatar url.
```python
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)
```

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](https://cdn-images-1.medium.com/max/2504/1*BzCHzF_Itr9_YGpOqPalkg.png)

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**

```python
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
```


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](https://cdn-images-1.medium.com/max/2000/1*7odbZPPPHC9zkGqoyyxHfg.png)



The next thing is to import our class into **main.py** and call the function **prepare_and_notify**
```python
discord = DiscordNews(webhook, username, avatar_url, feed)
discord.prepare_and_notify()
```





Finally, we run the program.
```bash
python main.py
```
![Divider](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j39v0shfjc2gmtxaew0j.png)




Result

![Example of messages in Discord channel](https://cdn-images-1.medium.com/max/2000/1*58BVldziZNhhl2RjiV8Jbw.png)
<br />



See the full code here
{% gist https://gist.github.com/devjorgecastro/843dfcf5ca3defb798f5b5ef5cab4d3c.js %}


![Divider](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j39v0shfjc2gmtxaew0j.png)<br />


If you like my content and want to support my work, you can give me a cup of coffee ☕️ 🥰

[![Ko-fi](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j18vjijbegi1dhp83ozv.png)](https://ko-fi.com/devjorgecastro)

[![Buy me a coffee](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jdz714gpdbxv6ve001hf.png)](https://www.buymeacoffee.com/jorgecastro)

## Follow me in

* **Twitter:** [@devjcastro](https://twitter.com/devjcastro)

* **Linkedin:** [dev-jorgecastro](https://www.linkedin.com/in/dev-jorgecastro/)