<commit_before>import logging
from flexget.plugin import *
from flexget.utils.tools import urlopener

log = logging.getLogger('sabnzbd')


class OutputSabnzbd:
    """
        Example:
        
        sabnzbd:
          apikey: 123456
          url: http://localhost/sabnzbd/api?
          category: movies
          
        Note: url has default value of 'http://localhost:8080/sabnzbd/api?'
          
        All parameters:
        
        sabnzbd:
          apikey: ...
          url: ...
          category: ...
          script: ...
          pp: ...
    """

    def validator(self):
        from flexget import validator
        config = validator.factory('dict')
        config.accept('text', key='key', required=True)
        config.accept('url', key='url', required=True)
        config.accept('text', key='category')
        config.accept('text', key='script')
        config.accept('text', key='pp')
        config.accept('text', key='password')
        config.accept('text', key='username')
        return config

    def get_params(self, config):
        params = {}
        if 'key' in config:
            params['apikey'] = config['key']
        if 'category' in config:
            params['cat'] = '%s' % config['category']
        if 'script' in config:
            params['script'] = config['script']
        if 'pp' in config:
            params['pp'] = config['pp']
        if 'username' in config:
            params['ma_username'] = config['username']
        if 'password' in config:
            params['ma_password'] = config['password']
        params['mode'] = 'addurl'
        return params

    def on_process_start(self, feed):
        """
        register the usable set: keywords
        """
        set = get_plugin_by_name('set')
        set.instance.register_keys({'category': 'text'})

    def on_feed_output(self, feed):

        import urllib
        
        # convert config into url parameters
        config = feed.config['sabnzbd']
        baseurl = config['url']
        
        for entry in feed.accepted:
            if feed.manager.options.test:
                log.info('Would add into sabnzbd: %s' % entry['title'])
                continue
                
            params = self.get_params(config)
            # allow overriding the category
            if 'category' in entry:
                params['cat'] = entry['category']
            params['name'] = entry['url']
            # add cleaner nzb name (undocumented api feature)
            params['nzbname'] = entry['title']

            request_url = baseurl + urllib.urlencode(params)
            log.debug('request_url: %s' % request_url)
            try:            
                response = urlopener(request_url, log).read()
            except Exception, e:
                log.critical('Failed to use sabnzbd. Requested %s' % request_url)
                log.critical('Result was: %s' % e)
                feed.fail(entry, 'sabnzbd unreachable')
                if feed.manager.options.debug:
                    log.exception(e)
                continue
            
            if 'error' in response.lower():
                feed.fail(entry, response.replace('\n', ''))
            
register_plugin(OutputSabnzbd, 'sabnzbd')
<commit_msg>sabnzbd plugin now logs when adding an nzb to sabnzbd. fixes #842<commit_after>import logging
from flexget.plugin import *
from flexget.utils.tools import urlopener

log = logging.getLogger('sabnzbd')


class OutputSabnzbd:
    """
        Example:

        sabnzbd:
          apikey: 123456
          url: http://localhost/sabnzbd/api?
          category: movies

        Note: url has default value of 'http://localhost:8080/sabnzbd/api?'

        All parameters:

        sabnzbd:
          apikey: ...
          url: ...
          category: ...
          script: ...
          pp: ...
    """

    def validator(self):
        from flexget import validator
        config = validator.factory('dict')
        config.accept('text', key='key', required=True)
        config.accept('url', key='url', required=True)
        config.accept('text', key='category')
        config.accept('text', key='script')
        config.accept('text', key='pp')
        config.accept('text', key='password')
        config.accept('text', key='username')
        return config

    def get_params(self, config):
        params = {}
        if 'key' in config:
            params['apikey'] = config['key']
        if 'category' in config:
            params['cat'] = '%s' % config['category']
        if 'script' in config:
            params['script'] = config['script']
        if 'pp' in config:
            params['pp'] = config['pp']
        if 'username' in config:
            params['ma_username'] = config['username']
        if 'password' in config:
            params['ma_password'] = config['password']
        params['mode'] = 'addurl'
        return params

    def on_process_start(self, feed):
        """
        register the usable set: keywords
        """
        set = get_plugin_by_name('set')
        set.instance.register_keys({'category': 'text'})

    def on_feed_output(self, feed):

        import urllib

        # convert config into url parameters
        config = feed.config['sabnzbd']
        baseurl = config['url']

        for entry in feed.accepted:
            if feed.manager.options.test:
                log.info('Would add into sabnzbd: %s' % entry['title'])
                continue

            params = self.get_params(config)
            # allow overriding the category
            if 'category' in entry:
                params['cat'] = entry['category']
            params['name'] = entry['url']
            # add cleaner nzb name (undocumented api feature)
            params['nzbname'] = entry['title']

            request_url = baseurl + urllib.urlencode(params)
            log.debug('request_url: %s' % request_url)
            try:
                response = urlopener(request_url, log).read()
            except Exception, e:
                log.critical('Failed to use sabnzbd. Requested %s' % request_url)
                log.critical('Result was: %s' % e)
                feed.fail(entry, 'sabnzbd unreachable')
                if feed.manager.options.debug:
                    log.exception(e)
                continue

            if 'error' in response.lower():
                feed.fail(entry, response.replace('\n', ''))
            else:
                log.info('"%s" added to sabnzbd' % (entry['title']))

register_plugin(OutputSabnzbd, 'sabnzbd')

{'fraction_non_alphanumeric': 0.12589544276787076, 'fraction_numerical': 0.0035055631763450694, 'mean_word_length': 1.7959096719216021, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '951842', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1781, 'n_words': 433}