RSS scraping with Python and passing new items to Slack using Webhooks

So my use case is that I wanted new Red Hat Errata published at https://rhn.redhat.com/rpc/recent-errata.pxt to be sent to one of our Slack channels. However the above url contains all errata and I´m only interested in critical errata to keep down the noise in the slack channel.

* First step is to create a slack webhook, visit: https://my.slack.com/services/new/incoming-webhook/ . Add the new webhook to the webhook variable in the script below. Also change channel name, etc in the payload variable.
* Next you need save the script to your linux box, lets create directory /usr/local/bin/errata-to-slack .
* I´m also using a library called feedparser to parse the RSS: https://github.com/kurtmckee/feedparser , download it and place it in the same directory as your script.
* Next you need to touch the datafile /var/lib/rhn-errata.txt which will hold a list of errata already sent to slack. If you will run the script as another user than root, you will need to chown the datafile to the correct user.

#!/usr/bin/env python                                                                                                                                               
 
datafile='/var/lib/rhn-errata.txt'                                                                                                                                  
import feedparser                                                                                                                                                   
import json                                                                                                                                                         
import os                                                                                                                                                           
import sys                                                                                                                                                          
import requests                                                                                                                                                     
 
d = feedparser.parse('https://rhn.redhat.com/rpc/recent-errata.pxt')                                                                                                
webhook = 'https://hooks.slack.com/services/XYXYXYXYXYX/XYXYXYXYXY/xyxyxyxyxyxyxy'                                                                           
 
for post in d.entries:                                                                                                                                              
  if post.title.find('Critical:') > 0:                                                                                                                              
    f = open(datafile, 'r+')                                                                                                                                        
    if post.title not in f.read():                                                                                                                                  
      payload = {                                                                                                                                                   
        'text': post.title + ": " + post.link + "\n"                                                                                                                
      }                                                                                                                                                             
      payload['channel'] = "#alerts"                                                                                                                                
      payload['username'] = "alerts"                                                                                                                                
      payload['icon_emoji'] = "ghost"                                                                                                                               
      try:                                                                                                                                                          
        res = requests.post(webhook, data=json.dumps(payload))                                                                                                      
      except Exception as e:                                                                                                                                        
        sys.stderr.write('An error occurred when trying to deliver the message:\n  {0}'.format(e.message))                                                          
      if res.ok:                                                                                                                                                    
        f.write(post.title+"\n")                                                                                                                                    
    f.close

Leave a Reply

Your email address will not be published. Required fields are marked *