3
from flexget.plugin import get_plugin_by_name, register_plugin
4
from flexget.utils.tools import urlopener
6
log = logging.getLogger('sabnzbd')
9
class OutputSabnzbd(object):
15
url: http://localhost/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('integer', key='priority')
38
config.accept('text', key='password')
39
config.accept('text', key='username')
42
def get_params(self, config):
45
params['apikey'] = config['key']
46
if 'category' in config:
47
params['cat'] = '%s' % config['category']
48
if 'script' in config:
49
params['script'] = config['script']
51
params['pp'] = config['pp']
52
if 'priority' in config:
53
params['priority'] = config['priority']
54
if 'username' in config:
55
params['ma_username'] = config['username']
56
if 'password' in config:
57
params['ma_password'] = config['password']
58
params['mode'] = 'addurl'
61
def on_process_start(self, feed, config):
63
register the usable set: keywords
65
set = get_plugin_by_name('set')
66
set.instance.register_keys({'category': 'text'})
68
def on_feed_output(self, feed, config):
69
for entry in feed.accepted:
70
if feed.manager.options.test:
71
log.info('Would add into sabnzbd: %s' % entry['title'])
74
params = self.get_params(config)
75
# allow overriding the category
76
if 'category' in entry:
77
# Dirty hack over the next few lines to strip out non-ascii
78
# chars. We're going to urlencode this, which causes
79
# serious issues in python2.x if it's not ascii input.
80
params['cat'] = ''.join([x for x in entry['category'] if ord(x) < 128])
81
params['name'] = ''.join([x for x in entry['url'] if ord(x) < 128])
82
# add cleaner nzb name (undocumented api feature)
83
params['nzbname'] = ''.join([x for x in entry['title'] if ord(x) < 128])
85
request_url = config['url'] + urllib.urlencode(params)
86
log.debug('request_url: %s' % request_url)
88
response = urlopener(request_url, log).read()
90
log.critical('Failed to use sabnzbd. Requested %s' % request_url)
91
log.critical('Result was: %s' % e)
92
feed.fail(entry, 'sabnzbd unreachable')
93
if feed.manager.options.debug:
97
if 'error' in response.lower():
98
feed.fail(entry, response.replace('\n', ''))
100
log.info('Added `%s` to SABnzbd' % (entry['title']))
102
register_plugin(OutputSabnzbd, 'sabnzbd', api_ver=2)