<commit_before>__version__ = 0.1

from httplib import HTTPSConnection
from urllib import urlencode
import logging
from flexget.plugin import get_plugin_by_name, register_plugin
from flexget.utils.template import RenderError

log = logging.getLogger('prowl')

headers = {'User-Agent': "FlexGet Prowl plugin/%s" % str(__version__), 
           'Content-type': "application/x-www-form-urlencoded"}


class OutputProwl(object):
    """
    prowl:
      apikey: xxxxxxx
      [application: application name, default FlexGet]
      [event: event title, default New Release]
      [priority: -2 - 2 (2 = highest), default 0]
      [description: notification to send]
      
    Configuration parameters are also supported from entries (eg. through set).
    """
    
    def validator(self):
        from flexget import validator
        config = validator.factory('dict')
        config.accept('text', key='apikey', required=True)
        config.accept('text', key='application')
        config.accept('text', key='event')
        config.accept('integer', key='priority')
        config.accept('text', key='description')
        return config

    def on_process_start(self, feed):
        """                                                                                                                                                                                                     
            Register the usable set: keywords.
        """ 
        set_plugin = get_plugin_by_name('set') 
        set_plugin.instance.register_keys({'apikey': 'text', 'application': 'text',
                                           'event': 'text', 'priority': 'integer'})

    def get_config(self, feed):
        config = feed.config.get('prowl', {})
        if isinstance(config, bool):
            config = {'enabled': config}
        config.setdefault('apikey', '')
        config.setdefault('application', 'FlexGet')
        config.setdefault('event', 'New release')
        config.setdefault('priority', 0)
        return config                                                                                                                                                                                           

    def on_feed_output(self, feed):
        for entry in feed.accepted:

            if feed.manager.options.test:
                log.info("Would send prowl message about: %s", entry['title'])
                continue

            # get the parameters
            config = self.get_config(feed)
            apikey = entry.get('apikey', config['apikey'])
            application = entry.get('application', config['application'])
            event = entry.get('event', config['event'])
            priority = entry.get('priority', config['priority'])
            description = config.get('description', entry['title'])

            # If description has jinja template, render it
            try:
                description = entry.render(description)
            except RenderError, e:
                description = entry['title']
                log.error('Error rendering jinja description: %s' % e)

            # Open connection
            h = HTTPSConnection('prowl.weks.net')
            
            # Send the request
            data = {'priority': priority, 'application': application, 'apikey': apikey,
                    'event': event, 'description': description}
            h.request("POST", "/publicapi/add", headers=headers, body=urlencode(data))

            # Check if it succeeded
            response = h.getresponse()
            request_status = response.status

            # error codes and messages from http://prowl.weks.net/api.php
            if request_status == 200:
                log.debug("Prowl message sent")
            elif request_status == 400: 
                log.error("Bad request, the parameters you provided did not validate")
            elif request_status == 401: 
                log.error("Not authorized, the API key given is not valid, and does not correspond to a user.")
            elif request_status == 406: 
                log.error("Not acceptable, your IP address has exceeded the API limit.")
            elif request_status == 500: 
                log.error("Internal server error, something failed to execute properly on the Prowl side.")
            else:
                log.error("Unknown error when sending Prowl message")

register_plugin(OutputProwl, 'prowl')<commit_msg>Change prowl plugin to api version 2.<commit_after>__version__ = 0.1

from httplib import HTTPSConnection
from urllib import urlencode
import logging
from flexget.plugin import get_plugin_by_name, register_plugin
from flexget.utils.template import RenderError

log = logging.getLogger('prowl')

headers = {'User-Agent': "FlexGet Prowl plugin/%s" % str(__version__), 
           'Content-type': "application/x-www-form-urlencoded"}


class OutputProwl(object):
    """
    prowl:
      apikey: xxxxxxx
      [application: application name, default FlexGet]
      [event: event title, default New Release]
      [priority: -2 - 2 (2 = highest), default 0]
      [description: notification to send]
      
    Configuration parameters are also supported from entries (eg. through set).
    """
    
    def validator(self):
        from flexget import validator
        config = validator.factory('dict')
        config.accept('text', key='apikey', required=True)
        config.accept('text', key='application')
        config.accept('text', key='event')
        config.accept('integer', key='priority')
        config.accept('text', key='description')
        return config

    def on_process_start(self, feed, config):
        """                                                                                                                                                                                                     
            Register the usable set: keywords.
        """ 
        set_plugin = get_plugin_by_name('set') 
        set_plugin.instance.register_keys({'apikey': 'text', 'application': 'text',
                                           'event': 'text', 'priority': 'integer'})

    def prepare_config(self, config):
        if isinstance(config, bool):
            config = {'enabled': config}
        config.setdefault('apikey', '')
        config.setdefault('application', 'FlexGet')
        config.setdefault('event', 'New release')
        config.setdefault('priority', 0)
        return config                                                                                                                                                                                           

    def on_feed_output(self, feed, config):
        config = self.prepare_config(config)
        for entry in feed.accepted:

            if feed.manager.options.test:
                log.info("Would send prowl message about: %s", entry['title'])
                continue

            # get the parameters
            apikey = entry.get('apikey', config['apikey'])
            application = entry.get('application', config['application'])
            event = entry.get('event', config['event'])
            priority = entry.get('priority', config['priority'])
            description = config.get('description', entry['title'])

            # If description has jinja template, render it
            try:
                description = entry.render(description)
            except RenderError, e:
                description = entry['title']
                log.error('Error rendering jinja description: %s' % e)

            # Open connection
            h = HTTPSConnection('prowl.weks.net')
            
            # Send the request
            data = {'priority': priority, 'application': application, 'apikey': apikey,
                    'event': event, 'description': description}
            h.request("POST", "/publicapi/add", headers=headers, body=urlencode(data))

            # Check if it succeeded
            response = h.getresponse()
            request_status = response.status

            # error codes and messages from http://prowl.weks.net/api.php
            if request_status == 200:
                log.debug("Prowl message sent")
            elif request_status == 400: 
                log.error("Bad request, the parameters you provided did not validate")
            elif request_status == 401: 
                log.error("Not authorized, the API key given is not valid, and does not correspond to a user.")
            elif request_status == 406: 
                log.error("Not acceptable, your IP address has exceeded the API limit.")
            elif request_status == 500: 
                log.error("Internal server error, something failed to execute properly on the Prowl side.")
            else:
                log.error("Unknown error when sending Prowl message")

register_plugin(OutputProwl, 'prowl', api_ver=2)
{'fraction_non_alphanumeric': 0.1016018819312199, 'fraction_numerical': 0.005152906911616444, 'mean_word_length': 1.7277726856095326, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6251336', 'n_tokens_mistral': 2144, 'n_tokens_neox': 2033, 'n_words': 598}