SABnzbd Pause/Resume based on XBMC Playback

Got a program that plays well with SABnzbd? Share it here!
Post Reply
tret
Full Member
Full Member
Posts: 148
Joined: August 2nd, 2008, 2:07 pm

SABnzbd Pause/Resume based on XBMC Playback

Post by tret »

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: 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
tret
Last edited by tret on January 12th, 2010, 7:24 pm, edited 1 time in total.
Camelot
Jr. Member
Jr. Member
Posts: 64
Joined: August 18th, 2008, 6:23 am

Re: SABnzbd Pause/Resume based on XBMC Playback

Post by Camelot »

thanks for the script. Will be trying it out over the next few days, and get back to you if I have any issues :)
jefier
Newbie
Newbie
Posts: 1
Joined: January 15th, 2010, 2:16 am

Re: SABnzbd Pause/Resume based on XBMC Playback

Post by jefier »

Thanks for sharing nice  script. Will be trying it out over the next few days, and get back to you if I have any issues.keep it up
scales
Newbie
Newbie
Posts: 1
Joined: May 1st, 2010, 2:07 pm

Re: SABnzbd Pause/Resume based on XBMC Playback

Post by scales »

Awesome Script. Is there any way to adjust the code to have it only pause when a movie is playing I don't want it to pause while playing tv shows or anything else. I use the Home Theater Experieince script in XBMC and don't want the apple trailers to stop and buffer every 10 seconds.
Last edited by scales on May 1st, 2010, 5:56 pm, edited 1 time in total.
Post Reply