Features:
- Pause the SABnzbd queue whenever XBMC is playing a video and downloads are active
- Unpause the SABnzbd queue whenever XBMC is not playing a video and items are waiting to be downloaded
- Ignore resume conditions if the SABnzbd pause timer is in use (gives the user a way to manually pause the queue and avoid automatic resuming)
- Intelligently determine if action is required, Pause/Resume commands are only sent to SABnzbd when one of the above conditions are met
- Compatible with SABnzbd 0.5.0 Beta6 and later w/ API Key (earlier versions untested)
- Compatible with XBMC Camelot 9.11 and later (earlier versions untested)
- Tested in Linux (Ubuntu Karmic) and compatible with Python2.5 and Python2.6 (Python 3.0+ untested)
## IMPORTANT: Edit the XBMC and SABnzbd variables to work with your setup (XBMC web interface must be enabled and web authentication disabled)
sab_pause.py
Code: Select all
#!/usr/bin/python
import urllib2
import socket
# The address and port to your xbmc web interface (password authentication not supported in this script)
xbmcAddress = '192.168.1.2:8080'
# The address and port to your SABnzbd web interface
sabnzbdAddress = '192.168.1.2:8081'
# The username and password required to authenticate into your SABnzbd web interface
sabnzbdUser = 'username'
sabnzbdPass = 'password'
# The API Key required to interact with your SABnzbd web interface
sabnzbdAPIKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
# url timeout in seconds
timeout = 20
socket.setdefaulttimeout(timeout)
def sabControl(xbmcPlayback):
req = urllib2.Request('http://' + sabnzbdAddress + '/sabnzbd/api?mode=queue&output=xml&apikey=' + sabnzbdAPIKey + '&ma_username=' + sabnzbdUser + '&ma_password=' + sabnzbdPass)
try: handle = urllib2.urlopen(req)
except IOError, e:
print "SABnzbd Connection Error\n"
return 0
else:
page = handle.read()
if page.find('<index>0</index>') >= 0:
if page.find('<paused>False</paused>') >= 0 and xbmcPlayback:
apiMode = 'pause'
elif page.find('<paused>True</paused>') >= 0 and not xbmcPlayback:
if page.find('<pause_int>0</pause_int>') < 0:
print "Pause timer is active, SABnzbd resume command skipped\n"
return 0
apiMode = 'resume'
else:
print "No SABnzbd action required"
return 0
if apiMode:
req = urllib2.Request('http://' + sabnzbdAddress + '/sabnzbd/api?mode=' + apiMode + '&apikey=' + sabnzbdAPIKey + '&ma_username=' + sabnzbdUser + '&ma_password=' + sabnzbdPass)
try: handle = urllib2.urlopen(req)
except IOError, e:
print "SABnzbd Connection Error\n"
return 0
else:
page = handle.read()
else:
print "No items in the SABnzbd queue"
return 0
return apiMode
req = urllib2.Request('http://' + xbmcAddress + '/xbmcCmds/xbmcHttp?command=getcurrentlyplaying')
try: handle = urllib2.urlopen(req)
except IOError, e:
print "XBMC Connection Error\n"
exit
else:
page = handle.read()
if page.find('Filename:[Nothing Playing]') >= 0:
result = sabControl(0)
if result:
print "Nothing Playing, SABnzbd downloading " + result + "d\n"
elif page.find('Type:Video') >= 0:
result = sabControl(1)
if result:
print "Video Playing, SABnzbd downloading " + result + "d\n"
This script can be added to crontab to ensure that XBMC and SABnzbd are monitored and the queue is persistently managed. Example 5min Crontab interval:
Code: Select all
# m h dom mon dow command
*/5 * * * * /path_to_script/sab_pause.py