__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):
    """
    Send prowl notifications

    Example::

      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)

@@ -16,87 +16,60 @@
 .1%0A%0A
-from httplib import HTTPSConnection%0Afrom urllib import urlencode%0Aimport logging
+import logging%0Afrom requests import RequestException
 %0Afro
@@ -235,17 +235,17 @@
 Agent': 
-%22
+'
 FlexGet 
@@ -259,17 +259,17 @@
 lugin/%25s
-%22
+'
  %25 str(_
@@ -283,72 +283,8 @@
 n__)
-,%0A           'Content-type': %22application/x-www-form-urlencoded%22
 %7D%0A%0A%0A
@@ -2655,107 +2655,52 @@
     
-# Open connection%0A            h = HTTPSConnection('prowl.weks.net')%0A%0A            # Send the request
+url = 'https://prowl.weks.net/publicapi/add'
 %0A   
@@ -2864,158 +2864,269 @@
     
-h.request(%22POST%22, %22/publicapi/add%22, headers=headers, body=urlencode(data))%0A%0A            # Check if it succeeded%0A            response = h.getresponse()
+try:%0A                response = feed.requests.post(url, headers=headers, data=data, raise_status=False)%0A            except RequestException, e:%0A                log.error('Error with request: %25s' %25 e)%0A                continue%0A%0A            # Check if it succeeded
 %0A   
@@ -3166,16 +3166,21 @@
 e.status
+_code
 %0A%0A      

Convert prowl plugin to use requests. Fix a crash with prowl plugin when unicode data needs to be sent to the server. fix #1208