Advertisement
Guest User

throttle_nzbclient.py

a guest
Jan 29th, 2016
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. """
  2. Simple script to throttle nzb clients for plexpy
  3. if you add script args, the first is expected to be {duration}, if you add that, all download will be paused for that time.
  4. You can edit this script to list number of {stream_count} and restore the nzbclient to full speed if its above xx etc.
  5. The nzbget part is untested.
  6.  
  7. """
  8.  
  9. ### Edit vars ###
  10.  
  11. i_use = 'sabnzbd'  # or nzbget
  12. reduce_speed_to = 1000  # in kbit\s
  13.  
  14. sabnzbd_host = '192.168.1.7'
  15. sabnzbd_port = 8080
  16. sabnzbd_apikey = '******************************'
  17. sabnzbd_ssl = False
  18. sabnzbd_webdir = '/sabnzbd/'
  19.  
  20. nzbget_host = 'localhost'
  21. nzbget_port = 6789
  22. nzbget_username = ''
  23. nzbget_password = 'tegbzn6789 '
  24. nzbget_ssl = False
  25. nzbget_webdir = '/'
  26.  
  27. #### Edit stop ####
  28.  
  29. import requests  # pip install requests
  30. import sys
  31. from jsonrpclib import jsonrpc  # pip install jsonrpclib-pelix
  32.  
  33. if len(sys.argv) > 1:
  34.     duration = sys.argv[1]  # {duration} tag from plexpy, in minutes
  35. else:
  36.     duration = None
  37.  
  38.  
  39. class Sab(object):
  40.     def __init__(self, apikey, host,
  41.                  port, ssl, webdir):
  42.  
  43.         ssl = 's' if ssl else ''
  44.         self.url = 'http%s://%s:%s%sapi?output=json&apikey=%s&' % (ssl, host, port, webdir, apikey)
  45.  
  46.     def changespeed(self, speed):
  47.         return self.request('mode=config&name=speedlimit&value=' + speed)
  48.  
  49.     def pause_for(self, sec):
  50.         return self.request('mode=config&name=set_pause&value=' + sec)
  51.  
  52.     def request(self, s):
  53.         try:
  54.             requests.get(self.url + s)
  55.         except Exception as e:
  56.             print e
  57.  
  58.  
  59. class Nzbget(object):
  60.     def __init__(self, username, password, host, port, ssl, webdir):
  61.         if username and password:
  62.             authstring = '%s:%s@' % (username, password)
  63.         else:
  64.             authstring = ''
  65.  
  66.         ssl = 's' if ssl else ''
  67.  
  68.         self.url = 'http%s://%s%s:%s%sjsonrpc' % (ssl, authstring, host, port, webdir)
  69.  
  70.         self.action = jsonrpc.ServerProxy(self.url)
  71.  
  72.     def pause(self):
  73.         return self.action.pause()
  74.  
  75.     def changespeed(self, speed):
  76.         return self.action.rate(int(speed))
  77.  
  78.     def pause_for(self, sec):
  79.         self.action.pause()
  80.         self.action.scheduleresume(int(sec) * 60)  # convert from minutes to sec
  81.  
  82.  
  83. if i_use == 'sabnzbd':
  84.     sab = Sab(host=sabnzbd_host, port=sabnzbd_port, apikey=sabnzbd_apikey, ssl=sabnzbd_ssl, webdir=sabnzbd_webdir)
  85.     if duration:
  86.         sab.pause_for(str(duration))
  87.     else:
  88.         sab.changespeed(str(reduce_speed_to))
  89. else:
  90.     nzbget = Nzbget(host=nzbget_host, port=nzbget_port,
  91.                     username=nzbget_username, password=nzbget_password,
  92.                     ssl=nzbget_ssl, webdir=nzbget_webdir)
  93.     if duration:
  94.         nzbget.pause_for(duration)
  95.     else:
  96.         nzbget.changespeed(reduce_speed_to)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement