Skip to content

Instantly share code, notes, and snippets.

@topnotcher
Created March 6, 2014 01:09
Show Gist options
  • Save topnotcher/9380250 to your computer and use it in GitHub Desktop.
Save topnotcher/9380250 to your computer and use it in GitHub Desktop.
#!/usr/bin/python2
import sys
import os
import urllib
import urllib.error
import urllib.request
import json
import string
import hashlib
import math
# Get the api key from sabnzbd.ini or the web interface
apikey = "76513cb9e17540fff211afedb3277691"
# Name of the Usenet server in your config
newsreader = "news.usenetserver.com"
# Create a unique secret seed. Use the same secret for everyone in your group
# Also change this in the remote PHP script!
secret = 'ub3rs3kRitSabnzbD+Thing'
# URL to the remote PHP script that functions as a database
remoteURL = "http://localhost/web/cgi-bin/sabc.py"
# Maximum number of lines to share
maxConnections = 20
# Local URLs for sabnzbd
statusURL = "http://localhost:8080/sabnzbd/api?mode=qstatus&output=json&apikey=" + apikey
configURL = "http://localhost:8080/sabnzbd/api?mode=set_config&section=servers&keyword=" + newsreader + "&apikey=" + apikey
# Ask local sabnzbd if it's busy downloading
def getLocalStatus():
try:
response = urllib.request.urlopen(statusURL).read()
except urllib.error.URLError as e:
print("Sabnzbd API error: " + e.reason)
response = response.decode('utf-8')
sabnzbdStatus = json.loads(response)
if len(sys.argv) > 1 and sys.argv[1] == "force":
sabnzbdStatus["state"] = "DOWNLOADING"
return 1 if sabnzbdStatus["state"] == "DOWNLOADING" else 0
# Get remote status and update it with our local status
def checkConnections():
try:
response = urllib.request.urlopen(getRemoteURL()).read()
except urllib.error.URLError as e:
print("Couldn't get remote status: " + e.reason)
return 0
try:
statuses = json.loads(response.decode('utf-8'))
except ValueError as e:
print('Could not decode json response: ')
print('Raw output: ')
print(response)
return 0
if 'error' in statuses:
print('Error! Remote said: '+ statuses['error'])
return 0
downloaders = 0
for key, value in statuses.items():
downloaders += int(value)
if localStatus == 0:
# We're not downloading, so set connections to zero
return 0
if localStatus == 1 and downloaders == 0:
# We're the only one downloading
return maxConnections
else:
# Someone else is downloading too, divide up the available connections
return int(math.floor(maxConnections / downloaders))
def getRemoteURL():
meHash = hashlib.md5((me + str(localStatus) + secret).encode('utf-8')).hexdigest()
return remoteURL + "?meh=" + meHash + "&me=" + me + "&v=" + str(localStatus)
# Create unique indentifier from the api key and the seed
me = hashlib.md5((apikey + secret).encode('utf-8')).hexdigest()
localStatus = getLocalStatus()
# Check how many connections we need
connections = checkConnections()
# Tell sabnzbd to use the new number of available connections
try:
response = urllib.request.urlopen(configURL + "&connections=" + str(connections)).read()
except urllib.error.URLError as e:
print("Sabnzbd API error: " + e.reason)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment