Skip to content

Instantly share code, notes, and snippets.

@Hellowlol
Last active January 3, 2020 02:48
Show Gist options
  • Save Hellowlol/a5d0cab4bde185b38404 to your computer and use it in GitHub Desktop.
Save Hellowlol/a5d0cab4bde185b38404 to your computer and use it in GitHub Desktop.
Simple script to throttle sabnsbd and nzbget
from __future__ import print_function
import argparse
import requests # pip install requests
import sys
from jsonrpclib import jsonrpc # pip install jsonrpclib-pelix
"""
Simple script to throttle nzb clients for plexpy
There are 3 params:
-t {duration}
-sc {streams}
-a {action}
no arguments will simply reduce the speed
It can also be used as a commandline tool:
python throttle_nzbclient.py -c sabnzbd -h 10.0.0.97 -p 8085 -api 1234 -t 60 # pauses sab for 60 minutes
"""
#### Edit vars ####
i_use = 'sabnzbd' # or 'nzbget'
reduce_speed_to = 1000 # in kbit\s
max_speed = 10000 # bandwidth from isp in kbits
pause_for = None # int minutes or None to disable
remote_transcode_speed = None # remote transcode session speed in plex or None to disable
max_transcode_sessions = None # Number of streams before we do something, int or None to disable
sabnzbd_host = 'localhost'
sabnzbd_port = 8080
sabnzbd_apikey = '' # string
sabnzbd_ssl = False # True or False
sabnzbd_webdir = '/sabnzbd/' # Default is '/sabnzbd/'
nzbget_host = 'localhost'
nzbget_port = 6789
nzbget_username = 'nzbget' # Default username
nzbget_password = 'tegbzn6789 ' # default password
nzbget_ssl = False # True or False
nzbget_webdir = '/' # default webdir is '/'
#### Edit stop ####
action = None # Dont edit me
class Sab(object):
def __init__(self, apikey, host,
port, ssl, webdir):
ssl = 's' if ssl else ''
self.url = 'http%s://%s:%s%sapi?output=json&apikey=%s&' % (ssl, host, port, webdir, apikey)
def changespeed(self, speed):
print("Changed sabznbd speed to %s" % speed)
return self.request('mode=config&name=speedlimit&value=' + str(speed))
def pause_for(self, minutes):
print("Pausing sabnzbd for %s minutes" % minutes)
return self.request('mode=config&name=set_pause&value=' + str(minutes))
def restart(self):
print('Restarting Sabznbd')
return self.request('mode=restart')
def resume(self):
print('Resuming downloading with sabznbd')
return self.request('mode=resume')
def shutdown(self):
print('Shutting down Sabznbd')
return self.request('mode=shutdown')
def status(self):
pass # todo
def request(self, s):
try:
requests.get(self.url + s)
except Exception as e:
print('Failed to do %s' % (s, e))
class Nzbget(object):
def __init__(self, username, password, host, port, ssl, webdir):
if username and password:
authstring = '%s:%s@' % (username, password)
else:
authstring = ''
ssl = 's' if ssl else ''
self.url = 'http%s://%s%s:%s%sjsonrpc' % (ssl, authstring, host, port, webdir)
self.action = jsonrpc.ServerProxy(self.url)
def pause(self):
print('Paused nzbget')
return self.action.pause()
def changespeed(self, speed):
print("Changed speed to %s kbit\'s" % speed)
return self.action.rate(int(speed))
def pause_for(self, sec):
self.pause()
mins = int(sec) * 60
self.action.scheduleresume(mins) # convert from minutes to sec
print('for %s' % mins)
def resume(self):
print('Resumed download for nbzbget')
return self.action.resume()
def shutdown(self):
print('Shutting down nbzbget')
return self.action.shutdown()
def status(self):
pass # todo
if __name__ == '__main__':
if i_use == 'sabnzbd' and 'nzbget' not in sys.argv:
client_host = sabnzbd_host
client_port = sabnzbd_port
client_apikey = sabnzbd_apikey
client_ssl = sabnzbd_ssl
client_webdir = sabnzbd_webdir
client_username = None
client_password = None
else:
client_host = nzbget_host
client_port = nzbget_port
client_username = nzbget_username
client_password = nzbget_password
client_ssl = nzbget_ssl
client_apikey = None
client_webdir = nzbget_webdir
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--client', action='store', default=i_use,
help='Choose download client, sabznbd or nzbget')
parser.add_argument('-sc', '--stream_count', action='store', default=0, type=int,
help='stream_count argument from plexpy')
parser.add_argument('-s', '--speed', action='store', default=reduce_speed_to, type=int,
help='Reduce download speed to')
parser.add_argument('-a', '--action', action='store', default=None,
help='notify_action argument from plexpy')
parser.add_argument('-t', '--time', action='store', default=pause_for, type=int,
help='Number of minutes the client is paused')
parser.add_argument('-host', '--host', action='store', default=client_host,
help='host for the nzb client')
parser.add_argument('-p', '--port', action='store', default=client_port,
help='port for the nzb client')
parser.add_argument('-api', '--apikey', action='store', default=client_apikey,
help='sabnzbd apikey')
parser.add_argument('-u', '--username', action='store', default=client_username,
help='username nzbget')
parser.add_argument('-pass', '--password', action='store', default=client_password,
help='password nzbget')
parser.add_argument('-wd', '--webdir', action='store', default=client_webdir,
help='password nzbget')
parser.add_argument('-ssl', '--ssl', action='store_true', default=client_ssl,
help='Does the the nzb client use ssl?')
parser.add_argument('-ms', '--max_speed', action='store', default=max_speed,
help='Max speed of your internet connection in kbits')
parser.add_argument('-rts', '--remote_transcode_speed', action='store', default=remote_transcode_speed,
help='Max speed of your internet connection in kbits')
parser.add_argument('-mts', '--max_transcode_sessions', action='store', default=max_transcode_sessions,
help='Number of max streams before we reduce the speed')
p = parser.parse_args()
if p.client == 'sabnzbd':
client = Sab(host=p.host, port=p.port, apikey=p.apikey, ssl=p.ssl, webdir=p.webdir)
else:
client = Nzbget(host=p.host, port=p.port,
username=p.username, password=p.password,
ssl=p.ssl, webdir=p.webdir)
if p.time:
# should this be added to a current pause or add to current pause?
client.pause_for(p.time)
if p.stream_count:
if p.stream_count and p.remote_transcode_speed:
pass # todo
if int(p.streamcount) >= int(p.max_transcode_sessions):
client.changespeed(p.speed)
else:
client.changespeed(p.max_speed)
if p.action:
if p.action in ('play', 'resume'):
client.changespeed(p.speed)
elif p.action == 'buffer':
client.pausefor(15)
elif p.action in ('stop', 'pause'):
client.changespeed(p.max_speed)
@Hellowlol
Copy link
Author

@nomadore Dunno what the error is then, nzbget says your password is incorrect. Any special chars in the username and or password?

@nomadore
Copy link

@Hellowlol The username and password is standard letters. I am stumped. Thanks for answering though.

@Hellowlol
Copy link
Author

@nomadore and what is you pass the URL manually into the browser? Your adding the username and password of the control username /password?

@Magikarplvl4
Copy link

Magikarplvl4 commented Sep 12, 2016

Nice script! i have some problems running it.

When plexpy trigger the script when someone stops watching "PlexPy Notifiers :: Trying to run notify script, action: stop, arguments: None"
The script don't reset the download limit to max.

I like to pause nzbget when there is a buffer warning and slow down the download speed when there is 1 or more watchers.
Can you explain how to do the configuration?

My configuration now:

Edit vars

i_use = 'nzbget' # or 'nzbget'
reduce_speed_to = 51200 # in kbit\s
max_speed = 70000 # bandwidth from isp in kbits
maxtranscodesessions = 1 # Before we reduce the speed
pause_for = None # int minutes or None to disable
remote_transcode_speed = None # remote transcode session speed in plex or None to disable
max_transcode_sessions = 1 # Number of streams before we do something, int or None to disable

@Hellowlol
Copy link
Author

@whitecollar17 I didnt see what you wrote before now,. Regarding the stop argument. Try running the script manually and see it works and if you get any errors.

What are the script arguments in plexpy?

@Magikarplvl4
Copy link

@Hellowlol

The text what i wrote:

Nice script! i have some problems running it.

When plexpy trigger the script when someone stops watching "PlexPy Notifiers :: Trying to run notify script, action: stop, arguments: None"
The script don't reset the download limit to max.

I like to pause nzbget when there is a buffer warning and slow down the download speed when there is 1 or more watchers.
Can you explain how to do the configuration?

@sugarfunk
Copy link

Python is throwing an attributeerror. Any guidance?

PlexPy Notifiers :: Script error:
Traceback (most recent call last):
File "G:\PlexPy Scripts\throttle_nzbclient.py", line 202, in
if p.streamcount:
AttributeError: 'Namespace' object has no attribute 'streamcount'

@Hellowlol
Copy link
Author

@sugarfunk It was a typo in it. I have fixed it now. Just copy the script again.

@Humtek
Copy link

Humtek commented Dec 14, 2016

@Hellowlol I get the following even though I have the module installed.

PlexPy Notifiers :: Script error:
Traceback (most recent call last):
File "/config/scripts/throttle_nzbclient.py", line 5, in
from jsonrpclib import jsonrpc # pip install jsonrpclib-pelix
ImportError: No module named jsonrpclib

@Hellowlol
Copy link
Author

Hellowlol commented Jan 11, 2017

@Humtek the required module isn't installed. Run the pip install jsonrpclib-pelix in bash/cmd

@gaben67
Copy link

gaben67 commented Apr 24, 2017

I am getting the following error, any ideas on what could be the problem?

PlexPy Notifiers :: Script error: 
Traceback (most recent call last): 
    File "D:\plexpy scripts\throttle_nzbclient.py", line 199, in 
        client.pause_for(p.time) 
    File "D:\plexpy scripts\throttle_nzbclient.py", line 62, in pause_for 
        return self.request('mode=config&name=set_pause&value=' + str(minutes)) 
    File "D:\plexpy scripts\throttle_nzbclient.py", line 83, in request 
        print('Failed to do %s' % (s, e)) 
TypeError: not all arguments converted during string formatting

@splnut
Copy link

splnut commented Jun 13, 2017

I am trying to run this script to change the speed of NZBGet, but all I can get it to do is pause downloads.

xxxxx@server:/opt/plexpy/scripts$ python throttle_nzbclient.py -c nzbget -host 192.168.1.133 -p 6789 -u xxxx -pass xxxx -t 60
Paused nzbget
for 3600
xxxxx@server:/opt/plexpy/scripts$ python throttle_nzbclient.py -c nzbget -host 192.168.1.133 -p 6789 -u xxxx -pass xxxx -s 1200
Paused nzbget
for 2700

Config:

Edit vars

i_use = 'nzbget' # or 'nzbget'
reduce_speed_to = 1000 # in kbit\s
max_speed = 30000 # bandwidth from isp in kbits
pause_for = 45 # int minutes or None to disable
remote_transcode_speed = None # remote transcode session speed in plex or None to disable
max_transcode_sessions = None # Number of streams before we do something, int or None to disable

@geekcroft
Copy link

geekcroft commented Sep 12, 2017

Hi there;

I get

PlexPy Notifiers :: Script error: Traceback (most recent call last): File "/config/scripts/throttle_nzbclient.py", line 5, in from jsonrpclib import jsonrpc # pip install jsonrpclib-pelix ImportError: No module named jsonrpclib

However when I run pip install jsonrpclib-pelix I get;

Requirement already satisfied (use --upgrade to upgrade): jsonrpclib-pelix in /usr/local/lib/python3.4/dist-packages

Any idea what I'm doing wrong?

EDIT: Pip was defaulting to Python 3, when I had 2.7 installed. Uninstalled Pip and reinstalled and all working now :)

@HawksRepos
Copy link

HawksRepos commented Dec 2, 2019

Is there any way to get this to limit to streams rather than just play/pause/stop/resume triggers?

so if there are no streams it is at max (defined by the user).

1 stream = 80% of max (or value set by the user)
2 stream = 70% of max (or value set by the user)
3 stream = 60% of max (or value set by the user)
4 stream = 50% of max (or value set by the user)
5 stream = pause downloads (or value set by the user)

Also, not tested this but if one user starts to play something, it throttles obviously. Then another user joins in and a command is sent to throttle but its throttled already so that's OK. then one user decides to stop streaming and a command is sent to unthrottle. does it set download speed to max again even though someone is still watching or does it have some kind of control via the number of streams? like it knows someone is still watching so it doesn't set it to max again.

Edit: I have now tested and it doesn't matter if someone is still watching or not. if someone stops or pauses their stream, It removes any throttle applied to the NZB downloader. Is there any way to monitor and control this throttle via number of streams?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment