SABnzbd Pause/Resume based on XBMC Playback
#1
I posted this over on the SABnzbd forums but figured a few of the xbmc forum users may find it useful as well.

The idea behind this script is to avoid having SABnzbd post-processing cause video playback issues during XBMC use. I had been experiencing choppy video, pausing, audio sync and other issues whenever a SABnzbd download would finish and enter post-processing. I tried using SABnzbd's built in scheduler to avoid this during the hours I would most likely be using XBMC but this left my downloads paused during a good part of the day when XBMC was idle.

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:
#!/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:
# m h  dom mon dow   command
*/5 * * * * /path_to_script/sab_pause.py

tret
Reply
#2
Thumbs Up 
Sound like a brilliant idea. Nod Big Grin
I have the same problem but I run on Windows 7. Would there be any way of implementing this on a windows based htpc?
Reply
#3
Andromeda_nevel Wrote:Sound like a brilliant idea. Nod Big Grin
I have the same problem but I run on Windows 7. Would there be any way of implementing this on a windows based htpc?

Because this is written in Python it should work in Windows 7, you would need to install Python 2.5 or 2.6 and then add the script to the windows scheduler. Give it a shot and let us know how it goes.
Reply
#4
Great work. Just got Sabnzbd installed. Will most defenetly use this..
Pneumatic | SABnzbd | XBMC that just works - openelec
Reply
#5
Great work, going to try your script!

Just one question, your script pauses the sabnzdb download while xbmc is playing video as i understand.

The downloading itself does not take so much resources in my case, but the par checking and unpacking does.

Is it able to keep downloading but do not par checking and unpacking while xbmc play's video or music?
Reply
#6
SystemLord Wrote:Great work, going to try your script!

Just one question, your script pauses the sabnzdb download while xbmc is playing video as i understand.

The downloading itself does not take so much resources in my case, but the par checking and unpacking does.

Is it able to keep downloading but do not par checking and unpacking while xbmc play's video or music?

It doesnt seem like sabnzbd has the option to just queue the par checking and unpacking. ...
Pneumatic | SABnzbd | XBMC that just works - openelec
Reply
#7
Popeye Wrote:It doesnt seem like sabnzbd has the option to just queue the par checking and unpacking. ...

Ok point made. Thanks for the info.
Reply
#8
check out

http://linux.die.net/man/1/ionice

i used it some time ago to smooth up the i/o of the processes while par checking and unpacking ... should be no problem to implement this into your script!
Reply
#9
are u guys using this?
Enable Quick Check:
Skip par2 checking when files are 100% valid.
i see much better performance with this on. When the extract happens that lasts for less then 1min it wont bother that much. Oh and make sure to give sabnzb more chach memory so its not writing to the hard drive constantly.

Article Cache Limit:
Cache articles in memory to reduce disk access.
In bytes, optionally follow with K,M,G. For example: "64M" or "128M"
Reply
#10
tret Wrote:Because this is written in Python it should work in Windows 7, you would need to install Python 2.5 or 2.6 and then add the script to the windows scheduler. Give it a shot and let us know how it goes.


Can't get it working in windows 7... i'm using eventghost to load the python script... I get a can't connect to sabnzbd error... i'm setup on a localhost and configured the script for it... script contacts xbmc but not sab any ideas?

UPDATE:
Couldn't figure out why the script wouldn't work... So I wrote my own using eventghost xbmc broadcasts to trigger sab api functions...
Image Lunatixz - Kodi / Beta repository
Image PseudoTV - Forum | Website | Youtube | Help?
Reply
#11
Can you elaborate on what you did in eventghost please?
Reply
#12
kushty Wrote:Can you elaborate on what you did in eventghost please?

install EventGhost xbmc event receiver <--use to create events
example:
xbmc is playing a video...
xbmc has stopped play video...

in eventghost add action-Python Script
access sabnzbd's api through urlhttp python script
example:

import urllib
urllib.urlopen('http://IP:PORT/sabnzbd/api?mode=config&name=speedlimit&value=1000&output=xml&apikey=YOURAPIKEY&ma_username=YOURUSERNAME&ma_password=YOURPASSWORD')

<--this example sets the speed limit to 1mb, wiki sabnzbd api to learn more commands that can be sent... you can pause, resume, set bandwidth and more. REPLACE THE BOLD VALUES WITH YOUR INFO

if you wanted to pause and resume you would replace "mode=config&name=speedlimit&value=1000&output=xml" with either example below...

mode=pause&output=xml
mode=resume&output=xml
Image Lunatixz - Kodi / Beta repository
Image PseudoTV - Forum | Website | Youtube | Help?
Reply
#13
does this pause Sab based on any video playback, or streaming only? I would like it to ignore local/internal network paths.
Reply

Logout Mark Read Team Forum Stats Members Help
SABnzbd Pause/Resume based on XBMC Playback1