Script to alert feed owner to high number of listeners

Status
Not open for further replies.

radef

Member
Joined
Feb 8, 2008
Messages
40
I've been running a feed for about a year. Most of the time, I'm under 20 listeners at a time. I had some event the other day where my listeners peaked at nearly 200. I wish I would have been notified at the time of the event so I could have added a feed alert or something...and potentially driven up the number of listeners further.

So I whipped up a python script to do just that. Here it is if anyone else wants to implement the same feature. This runs on a Raspberry Pi that supplies my feed.

This file is located at /home/pi/stats/stats.py

Code:
#!/usr/bin/python

import urllib2, json
import os

feedId = "put_your_radioreference_feedId_here"
username = "put_your_radioreference_username_here"
password = "put_your_radioreference_password_here"
email = "put_your_email_here"

alertThreshold = 30
alertSubject = "[elmpi] Broadcastify Alert"
alertBody = "Broadcastify listener threshold exceeded " + str(alertThreshold) + " listeners.  "
alertBody += "Listen to the feed here http://www.broadcastify.com/listen/feed/" + feedId + "  "
alertBody += "Manage the feed here http://www.broadcastify.com/manage/feed/" + feedId + "  "

url = "https://api.broadcastify.com/owner/?a=feed&feedId=" + feedId + "&type=json&u=" + username + "&p=" + password
response = urllib2.urlopen(url)
data = json.load(response)
listeners = data['Feed'][0]['listeners']
alertBody += "The current number of listeners is " + str(listeners) + "  "

if listeners > alertThreshold:
        cmd = 'echo ' + alertBody + ' | mail -s "' + alertSubject + '" ' + email
        os.system(cmd)

I then have then running every 5 minutes thanks to a cronjob:
Code:
crontab -e

then add the line
Code:
*/5 * * * * /home/pi/stats/stats.py

The "mail" command above requires ssmtp & mailutils to be installed (ssmtp to send emails – Raspberry Pi Projects) which I have configured to send via a gmail account.
 

webstar22

RenfrewCountyScanner.com
Feed Provider
Joined
Dec 21, 2003
Messages
1,006
Location
Ontario, Canada
I used my network monitoring software to do the same thing. lets me keep a screen on a secondary monitor all the time to see whats going on plus alerts me at set levels.

Oi8eJ5k.png


Also I've been monitoring server disconnections from the past week or so on Audio3.

6BL8tnQ.png
 

radef

Member
Joined
Feb 8, 2008
Messages
40
Here's an update. I reordered the information and added some HTML to the emails. The goal was to have the current number of subscribers near the top of the email (so my email programs show the current number of listeners in the email preview).

Code:
#!/usr/bin/python

import urllib2, json
import os

feedId = "your_feed_number_here"
username = "your_username_here"
password = "your_password_here"
email = "email_address_here"

alertThreshold = 40
alertSubject = "[servername] Broadcastify Alert"
alertBody = "<p>Broadcastify listener threshold exceeded " + str(alertThreshold) + " listeners.</p>"
alertBody += "<p><a href='http://www.broadcastify.com/listen/feed/" + feedId + "'>Listen to the feed here</a></p>"
alertBody += "<p><a href='http://www.broadcastify.com/manage/feed/" + feedId + "'>Manage the feed here</a></p>"

url = "https://api.broadcastify.com/owner/?a=feed&feedId=" + feedId + "&type=json&u=" + username + "&p=" + password
response = urllib2.urlopen(url)
data = json.load(response)
listeners = data['Feed'][0]['listeners']
alertBody = "<h2>Current Listeners:  " + str(listeners) + "</h2>" + alertBody

if listeners > alertThreshold:
	cmd = 'echo ' + repr(alertBody) + ' | mail -a "Content-type: text/html;" -s "' + alertSubject + '" ' + email
	os.system(cmd)
 
Status
Not open for further replies.
Top