FYI: example script for SAB API, JSON and the queue

Got a program that plays well with SABnzbd? Share it here!
Post Reply
User avatar
sander
Release Testers
Release Testers
Posts: 8830
Joined: January 22nd, 2008, 2:22 pm

FYI: example script for SAB API, JSON and the queue

Post by sander »

As I couldn't find a python script handling JSON output from SABnzbd's API here in the forum itself, here's an example python script to handle the JSON output from SABnzbd's API queue output:

Code: Select all

import json
import urllib2
from urllib import urlencode

apikey = ''

def sab_query(query):
	url = 'http://localhost:8080/'
	query["apikey"] = apikey 
	response = urllib2.urlopen(urllib2.Request(url + "api?", urlencode(query)))
	response_data = response.read()
	return response_data

sabqueuejson = sab_query({"mode":"queue", "output":"json"})
print "SABnzbd queue info in bare JSON output:\n\n", sabqueuejson

print "\n\nSABnzbd queue info decoded:\n\n"
sabqueuedecoded = json.loads(sabqueuejson)
print sabqueuedecoded

print "\n\nSABnzbd queue info recoded into indented JSON reformat, easier for reading:\n\n"
print json.dumps(sabqueuedecoded, sort_keys=True, indent=2)

queueslots = sabqueuedecoded["queue"]["slots"]
print "\n\nThe queue itself:\n\n", queueslots
print "\n\nThe queue itself, in JSON and with indention for reading:\n\n", json.dumps(queueslots, sort_keys=True, indent=2)

print "\n\nName and size of individual downloads:\n\n"
for nzo in queueslots:
	print nzo["nzo_id"] + " has size " + nzo["mb"]

Example output:

Code: Select all

SABnzbd queue info in bare JSON output:

{"queue":{"active_lang":"en","session":"05b28c7cec4ec6f867627222ce45dd0f","slots":[{"status":"Paused","index":0,"eta":"unknown","missing":0,"avg_age":"74d","script":"None","msgid":"","verbosity":"","mb":"622.25","sizeleft":"616 MB","filename":"rar5---mini","priority":"Normal","cat":"*","mbleft":"615.80","timeleft":"0:00:00","percentage":"1","nzo_id":"SABnzbd_nzo_SqYCAW","has_rating":false,"unpackopts":"3","size":"622 MB"},{"status":"Paused","index":1,"eta":"unknown","missing":0,"avg_age":"760d","script":"None","msgid":"","verbosity":"","mb":"1928.45","sizeleft":"769 MB","filename":"Books - Reid","priority":"Normal","cat":"*","mbleft":"768.98","timeleft":"0:00:00","percentage":"60","nzo_id":"SABnzbd_nzo_f34kP8","has_rating":false,"unpackopts":"3","size":"1.9 GB"},{"status":"Paused","index":2,"eta":"unknown","missing":0,"avg_age":"74d","script":"None","msgid":"","verbosity":"","mb":"13479.54","sizeleft":"13.2 GB","filename":"rar5---13GB","priority":"Normal","cat":"*","mbleft":"13475.75","timeleft":"0:00:00","percentage":"0","nzo_id":"SABnzbd_nzo_XscFMc","has_rating":false,"unpackopts":"3","size":"13.2 GB"}],"speed":"0  ","size":"0 B","rating_enable":false,"limit":0,"start":0,"diskspacetotal2":"72.78","darwin":false,"last_warning":"","have_warnings":"0","noofslots":3,"newzbin_url":"www.newzbin2.es","pause_int":"0","categories":["*"],"pp_pause_event":false,"diskspacetotal1":"72.78","mb":"0.00","loadavg":"0.55 | 0.51 | 0.51 | V=2537M R=60M","cache_max":"0","speedlimit":"","webdir":"","left_quota":"0 ","uniconfig":"/home/sander/git-2014-05-16/sabnzbd/interfaces/Config/templates","paused":false,"isverbose":false,"restart_req":false,"power_options":true,"helpuri":"http://wiki.sabnzbd.org/","uptime":"2h","refresh_rate":"","my_home":"/home/sander","version":"0.7.x","my_lcldata":"/home/sander/.sabnzbd","color_scheme":"gold","new_release":"","nt":false,"status":"Idle","finish":0,"cache_art":"0","paused_all":false,"finishaction":null,"sizeleft":"0 B","quota":"0 ","cache_size":"0 B","mbleft":"0.00","diskspace2":"1.18","diskspace1":"1.18","scripts":["None","scan-directory-with-virustotal.py","scan-directory.py~","rename-null-fill-to-dollar2.sh","rename-null-file-to-dollar2-avi-2.sh","rename-null-fill-to-dollar2-avi.sh","scan-directory-with-virustotal3.py","scan-directory-with-virustotal2.py","scan-directory-with-virustotal3.py~","scan-directory-with-virustotal2.py~","scan-directory-with-virustotal.py~"],"timeleft":"0:00:00","have_quota":false,"nzb_quota":"","eta":"unknown","kbpersec":"0.00","new_rel_url":"","queue_details":"0"}}


SABnzbd queue info decoded:


{u'queue': {u'uniconfig': u'/home/sander/git-2014-05-16/sabnzbd/interfaces/Config/templates', u'cache_art': u'0', u'my_lcldata': u'/home/sander/.sabnzbd', u'scripts': [u'None', u'scan-directory-with-virustotal.py', u'scan-directory.py~', u'rename-null-fill-to-dollar2.sh', u'rename-null-file-to-dollar2-avi-2.sh', u'rename-null-fill-to-dollar2-avi.sh', u'scan-directory-with-virustotal3.py', u'scan-directory-with-virustotal2.py', u'scan-directory-with-virustotal3.py~', u'scan-directory-with-virustotal2.py~', u'scan-directory-with-virustotal.py~'], u'cache_size': u'0 B', u'active_lang': u'en', u'pp_pause_event': False, u'session': u'05b28c7cec4ec6f867627222ce45dd0f', u'restart_req': False, u'power_options': True, u'slots': [{u'status': u'Paused', u'index': 0, u'nzo_id': u'SABnzbd_nzo_SqYCAW', u'avg_age': u'74d', u'timeleft': u'0:00:00', u'script': u'None', u'mb': u'622.25', u'msgid': u'', u'verbosity': u'', u'missing': 0, u'sizeleft': u'616 MB', u'filename': u'rar5---mini', u'priority': u'Normal', u'eta': u'unknown', u'mbleft': u'615.80', u'percentage': u'1', u'cat': u'*', u'has_rating': False, u'unpackopts': u'3', u'size': u'622 MB'}, {u'status': u'Paused', u'index': 1, u'nzo_id': u'SABnzbd_nzo_f34kP8', u'avg_age': u'760d', u'timeleft': u'0:00:00', u'script': u'None', u'mb': u'1928.45', u'msgid': u'', u'verbosity': u'', u'missing': 0, u'sizeleft': u'769 MB', u'filename': u'Books - Reid', u'priority': u'Normal', u'eta': u'unknown', u'mbleft': u'768.98', u'percentage': u'60', u'cat': u'*', u'has_rating': False, u'unpackopts': u'3', u'size': u'1.9 GB'}, {u'status': u'Paused', u'index': 2, u'nzo_id': u'SABnzbd_nzo_XscFMc', u'avg_age': u'74d', u'timeleft': u'0:00:00', u'script': u'None', u'mb': u'13479.54', u'msgid': u'', u'verbosity': u'', u'missing': 0, u'sizeleft': u'13.2 GB', u'filename': u'rar5---13GB', u'priority': u'Normal', u'eta': u'unknown', u'mbleft': u'13475.75', u'percentage': u'0', u'cat': u'*', u'has_rating': False, u'unpackopts': u'3', u'size': u'13.2 GB'}], u'speed': u'0  ', u'helpuri': u'http://wiki.sabnzbd.org/', u'size': u'0 B', u'rating_enable': False, u'refresh_rate': u'', u'my_home': u'/home/sander', u'sizeleft': u'0 B', u'nzb_quota': u'', u'have_quota': False, u'start': 0, u'finish': 0, u'version': u'0.7.x', u'new_rel_url': u'', u'diskspacetotal2': u'72.78', u'cache_max': u'0', u'diskspacetotal1': u'72.78', u'nt': False, u'status': u'Idle', u'last_warning': u'', u'have_warnings': u'0', u'new_release': u'', u'paused_all': False, u'finishaction': None, u'noofslots': 3, u'uptime': u'2h', u'newzbin_url': u'www.newzbin2.es', u'quota': u'0 ', u'pause_int': u'0', u'mbleft': u'0.00', u'diskspace2': u'1.18', u'diskspace1': u'1.18', u'color_scheme': u'gold', u'categories': [u'*'], u'paused': False, u'darwin': False, u'timeleft': u'0:00:00', u'mb': u'0.00', u'eta': u'unknown', u'isverbose': False, u'loadavg': u'0.55 | 0.51 | 0.51 | V=2537M R=60M', u'limit': 0, u'kbpersec': u'0.00', u'speedlimit': u'', u'webdir': u'', u'left_quota': u'0 ', u'queue_details': u'0'}}


SABnzbd queue info recoded into indented JSON reformat, easier for reading:


{
  "queue": {
    "active_lang": "en", 
    "cache_art": "0", 
    "cache_max": "0", 
    "cache_size": "0 B", 
    "categories": [
      "*"
    ], 
    "color_scheme": "gold", 
    "darwin": false, 
    "diskspace1": "1.18", 
    "diskspace2": "1.18", 
    "diskspacetotal1": "72.78", 
    "diskspacetotal2": "72.78", 
    "eta": "unknown", 
    "finish": 0, 
    "finishaction": null, 
    "have_quota": false, 
    "have_warnings": "0", 
    "helpuri": "http://wiki.sabnzbd.org/", 
    "isverbose": false, 
    "kbpersec": "0.00", 
    "last_warning": "", 
    "left_quota": "0 ", 
    "limit": 0, 
    "loadavg": "0.55 | 0.51 | 0.51 | V=2537M R=60M", 
    "mb": "0.00", 
    "mbleft": "0.00", 
    "my_home": "/home/sander", 
    "my_lcldata": "/home/sander/.sabnzbd", 
    "new_rel_url": "", 
    "new_release": "", 
    "newzbin_url": "www.newzbin2.es", 
    "noofslots": 3, 
    "nt": false, 
    "nzb_quota": "", 
    "pause_int": "0", 
    "paused": false, 
    "paused_all": false, 
    "power_options": true, 
    "pp_pause_event": false, 
    "queue_details": "0", 
    "quota": "0 ", 
    "rating_enable": false, 
    "refresh_rate": "", 
    "restart_req": false, 
    "scripts": [
      "None", 
      "scan-directory-with-virustotal.py", 
      "scan-directory.py~", 
      "rename-null-fill-to-dollar2.sh", 
      "rename-null-file-to-dollar2-avi-2.sh", 
      "rename-null-fill-to-dollar2-avi.sh", 
      "scan-directory-with-virustotal3.py", 
      "scan-directory-with-virustotal2.py", 
      "scan-directory-with-virustotal3.py~", 
      "scan-directory-with-virustotal2.py~", 
      "scan-directory-with-virustotal.py~"
    ], 
    "session": "05b28c7cec4ec6f867627222ce45dd0f", 
    "size": "0 B", 
    "sizeleft": "0 B", 
    "slots": [
      {
        "avg_age": "74d", 
        "cat": "*", 
        "eta": "unknown", 
        "filename": "rar5---mini", 
        "has_rating": false, 
        "index": 0, 
        "mb": "622.25", 
        "mbleft": "615.80", 
        "missing": 0, 
        "msgid": "", 
        "nzo_id": "SABnzbd_nzo_SqYCAW", 
        "percentage": "1", 
        "priority": "Normal", 
        "script": "None", 
        "size": "622 MB", 
        "sizeleft": "616 MB", 
        "status": "Paused", 
        "timeleft": "0:00:00", 
        "unpackopts": "3", 
        "verbosity": ""
      }, 
      {
        "avg_age": "760d", 
        "cat": "*", 
        "eta": "unknown", 
        "filename": "Books - Reid", 
        "has_rating": false, 
        "index": 1, 
        "mb": "1928.45", 
        "mbleft": "768.98", 
        "missing": 0, 
        "msgid": "", 
        "nzo_id": "SABnzbd_nzo_f34kP8", 
        "percentage": "60", 
        "priority": "Normal", 
        "script": "None", 
        "size": "1.9 GB", 
        "sizeleft": "769 MB", 
        "status": "Paused", 
        "timeleft": "0:00:00", 
        "unpackopts": "3", 
        "verbosity": ""
      }, 
      {
        "avg_age": "74d", 
        "cat": "*", 
        "eta": "unknown", 
        "filename": "rar5---13GB", 
        "has_rating": false, 
        "index": 2, 
        "mb": "13479.54", 
        "mbleft": "13475.75", 
        "missing": 0, 
        "msgid": "", 
        "nzo_id": "SABnzbd_nzo_XscFMc", 
        "percentage": "0", 
        "priority": "Normal", 
        "script": "None", 
        "size": "13.2 GB", 
        "sizeleft": "13.2 GB", 
        "status": "Paused", 
        "timeleft": "0:00:00", 
        "unpackopts": "3", 
        "verbosity": ""
      }
    ], 
    "speed": "0  ", 
    "speedlimit": "", 
    "start": 0, 
    "status": "Idle", 
    "timeleft": "0:00:00", 
    "uniconfig": "/home/sander/git-2014-05-16/sabnzbd/interfaces/Config/templates", 
    "uptime": "2h", 
    "version": "0.7.x", 
    "webdir": ""
  }
}


The queue itself:

[{u'status': u'Paused', u'index': 0, u'nzo_id': u'SABnzbd_nzo_SqYCAW', u'avg_age': u'74d', u'timeleft': u'0:00:00', u'script': u'None', u'mb': u'622.25', u'msgid': u'', u'verbosity': u'', u'missing': 0, u'sizeleft': u'616 MB', u'filename': u'rar5---mini', u'priority': u'Normal', u'eta': u'unknown', u'mbleft': u'615.80', u'percentage': u'1', u'cat': u'*', u'has_rating': False, u'unpackopts': u'3', u'size': u'622 MB'}, {u'status': u'Paused', u'index': 1, u'nzo_id': u'SABnzbd_nzo_f34kP8', u'avg_age': u'760d', u'timeleft': u'0:00:00', u'script': u'None', u'mb': u'1928.45', u'msgid': u'', u'verbosity': u'', u'missing': 0, u'sizeleft': u'769 MB', u'filename': u'Books - Reid', u'priority': u'Normal', u'eta': u'unknown', u'mbleft': u'768.98', u'percentage': u'60', u'cat': u'*', u'has_rating': False, u'unpackopts': u'3', u'size': u'1.9 GB'}, {u'status': u'Paused', u'index': 2, u'nzo_id': u'SABnzbd_nzo_XscFMc', u'avg_age': u'74d', u'timeleft': u'0:00:00', u'script': u'None', u'mb': u'13479.54', u'msgid': u'', u'verbosity': u'', u'missing': 0, u'sizeleft': u'13.2 GB', u'filename': u'rar5---13GB', u'priority': u'Normal', u'eta': u'unknown', u'mbleft': u'13475.75', u'percentage': u'0', u'cat': u'*', u'has_rating': False, u'unpackopts': u'3', u'size': u'13.2 GB'}]


The queue itself, in JSON and with indention for reading:

[
  {
    "avg_age": "74d", 
    "cat": "*", 
    "eta": "unknown", 
    "filename": "rar5---mini", 
    "has_rating": false, 
    "index": 0, 
    "mb": "622.25", 
    "mbleft": "615.80", 
    "missing": 0, 
    "msgid": "", 
    "nzo_id": "SABnzbd_nzo_SqYCAW", 
    "percentage": "1", 
    "priority": "Normal", 
    "script": "None", 
    "size": "622 MB", 
    "sizeleft": "616 MB", 
    "status": "Paused", 
    "timeleft": "0:00:00", 
    "unpackopts": "3", 
    "verbosity": ""
  }, 
  {
    "avg_age": "760d", 
    "cat": "*", 
    "eta": "unknown", 
    "filename": "Books - Reid", 
    "has_rating": false, 
    "index": 1, 
    "mb": "1928.45", 
    "mbleft": "768.98", 
    "missing": 0, 
    "msgid": "", 
    "nzo_id": "SABnzbd_nzo_f34kP8", 
    "percentage": "60", 
    "priority": "Normal", 
    "script": "None", 
    "size": "1.9 GB", 
    "sizeleft": "769 MB", 
    "status": "Paused", 
    "timeleft": "0:00:00", 
    "unpackopts": "3", 
    "verbosity": ""
  }, 
  {
    "avg_age": "74d", 
    "cat": "*", 
    "eta": "unknown", 
    "filename": "rar5---13GB", 
    "has_rating": false, 
    "index": 2, 
    "mb": "13479.54", 
    "mbleft": "13475.75", 
    "missing": 0, 
    "msgid": "", 
    "nzo_id": "SABnzbd_nzo_XscFMc", 
    "percentage": "0", 
    "priority": "Normal", 
    "script": "None", 
    "size": "13.2 GB", 
    "sizeleft": "13.2 GB", 
    "status": "Paused", 
    "timeleft": "0:00:00", 
    "unpackopts": "3", 
    "verbosity": ""
  }
]


Name and size of individual downloads:


SABnzbd_nzo_SqYCAW has size 622.25
SABnzbd_nzo_f34kP8 has size 1928.45
SABnzbd_nzo_XscFMc has size 13479.54
HTH
User avatar
sander
Release Testers
Release Testers
Posts: 8830
Joined: January 22nd, 2008, 2:22 pm

Re: FYI: example script for SAB API, JSON and the queue

Post by sander »

FWIW: Some alternative code:

Code: Select all

### Made by the JSON King 

import json
import urllib

apikey = 'b463b755ad289e4fd2e2e7319ab6eacf'
baseurl = 'http://localhost:8080/'

url = '%sapi?mode=queue&start=START&limit=LIMIT&output=json&apikey=%s' % (baseurl, apikey)
urlresult = urllib.urlopen(url)
parsedjson = json.loads(urlresult.read())

print json.dumps(parsedjson, indent=4, sort_keys=True)

#print "The queue:", parsedjson['queue']
print "\nCurrent speed", parsedjson['queue']['speed']

#print parsedjson['queue']['slots']
for thisslot in parsedjson['queue']['slots']:
	print "\nThisslot:\n",thisslot
Post Reply