2
from flexget.plugin import get_plugin_by_name, register_plugin
3
from flexget.utils.tools import urlopener
5
log = logging.getLogger('sabnzbd')
14
url: http://localhost/sabnzbd/api?
17
Note: url has default value of 'http://localhost:8080/sabnzbd/api?'
30
from flexget import validator
31
config = validator.factory('dict')
32
config.accept('text', key='key', required=True)
33
config.accept('url', key='url', required=True)
34
config.accept('text', key='category')
35
config.accept('text', key='script')
36
config.accept('text', key='pp')
37
config.accept('text', key='password')
38
config.accept('text', key='username')
41
def get_params(self, config):
44
params['apikey'] = config['key']
45
if 'category' in config:
46
params['cat'] = '%s' % config['category']
47
if 'script' in config:
48
params['script'] = config['script']
50
params['pp'] = config['pp']
51
if 'username' in config:
52
params['ma_username'] = config['username']
53
if 'password' in config:
54
params['ma_password'] = config['password']
55
params['mode'] = 'addurl'
58
def on_process_start(self, feed):
60
register the usable set: keywords
62
set = get_plugin_by_name('set')
63
set.instance.register_keys({'category': 'text'})
65
def on_feed_output(self, feed):
69
# convert config into url parameters
70
config = feed.config['sabnzbd']
71
baseurl = config['url']
73
for entry in feed.accepted:
74
if feed.manager.options.test:
75
log.info('Would add into sabnzbd: %s' % entry['title'])
78
params = self.get_params(config)
79
# allow overriding the category
80
if 'category' in entry:
81
# Dirty hack over the next few lines to strip out non-ascii
82
# chars. We're going to urlencode this, which causes
83
# serious issues in python2.x if it's not ascii input.
84
params['cat'] = ''.join([x for x in entry['category'] if ord(x) < 128])
85
params['name'] = ''.join([x for x in entry['url'] if ord(x) < 128])
86
# add cleaner nzb name (undocumented api feature)
87
params['nzbname'] = ''.join([x for x in entry['title'] if ord(x) < 128])
89
request_url = baseurl + urllib.urlencode(params)
90
log.debug('request_url: %s' % request_url)
92
response = urlopener(request_url, log).read()
94
log.critical('Failed to use sabnzbd. Requested %s' % request_url)
95
log.critical('Result was: %s' % e)
96
feed.fail(entry, 'sabnzbd unreachable')
97
if feed.manager.options.debug:
101
if 'error' in response.lower():
102
feed.fail(entry, response.replace('\n', ''))
104
log.info('Added `%s` to SABnzbd' % (entry['title']))
106
register_plugin(OutputSabnzbd, 'sabnzbd')