flexget.plugins.output.prowl
Covered: 37 lines
Missed: 43 lines
Skipped 27 lines
Percent: 46 %
  1
__version__ = 0.1
  3
from httplib import HTTPSConnection
  4
from urllib import urlencode
  5
import logging
  6
from flexget.plugin import get_plugin_by_name, register_plugin
  7
from flexget.utils.template import RenderError
  9
log = logging.getLogger('prowl')
 11
headers = {'User-Agent': "FlexGet Prowl plugin/%s" % str(__version__),
 12
           'Content-type': "application/x-www-form-urlencoded"}
 15
class OutputProwl(object):
 16
    """
 17
    Send prowl notifications
 19
    Example::
 21
      prowl:
 22
        apikey: xxxxxxx
 23
        [application: application name, default FlexGet]
 24
        [event: event title, default New Release]
 25
        [priority: -2 - 2 (2 = highest), default 0]
 26
        [description: notification to send]
 28
    Configuration parameters are also supported from entries (eg. through set).
 29
    """
 31
    def validator(self):
 32
        from flexget import validator
 33
        config = validator.factory('dict')
 34
        config.accept('text', key='apikey', required=True)
 35
        config.accept('text', key='application')
 36
        config.accept('text', key='event')
 37
        config.accept('integer', key='priority')
 38
        config.accept('text', key='description')
 39
        return config
 41
    def on_process_start(self, feed, config):
 42
        """
 43
            Register the usable set: keywords.
 44
        """
 45
        set_plugin = get_plugin_by_name('set')
 46
        set_plugin.instance.register_keys({'apikey': 'text', 'application': 'text',
 47
                                           'event': 'text', 'priority': 'integer'})
 49
    def prepare_config(self, config):
 50
        if isinstance(config, bool):
 51
            config = {'enabled': config}
 52
        config.setdefault('apikey', '')
 53
        config.setdefault('application', 'FlexGet')
 54
        config.setdefault('event', 'New release')
 55
        config.setdefault('priority', 0)
 56
        return config
 58
    def on_feed_output(self, feed, config):
 59
        config = self.prepare_config(config)
 60
        for entry in feed.accepted:
 62
            if feed.manager.options.test:
 63
                log.info("Would send prowl message about: %s", entry['title'])
 64
                continue
 67
            apikey = entry.get('apikey', config['apikey'])
 68
            application = entry.get('application', config['application'])
 69
            event = entry.get('event', config['event'])
 70
            priority = entry.get('priority', config['priority'])
 71
            description = config.get('description', entry['title'])
 74
            try:
 75
                description = entry.render(description)
 76
            except RenderError, e:
 77
                description = entry['title']
 78
                log.error('Error rendering jinja description: %s' % e)
 81
            h = HTTPSConnection('prowl.weks.net')
 84
            data = {'priority': priority, 'application': application, 'apikey': apikey,
 85
                    'event': event, 'description': description}
 86
            h.request("POST", "/publicapi/add", headers=headers, body=urlencode(data))
 89
            response = h.getresponse()
 90
            request_status = response.status
 93
            if request_status == 200:
 94
                log.debug("Prowl message sent")
 95
            elif request_status == 400:
 96
                log.error("Bad request, the parameters you provided did not validate")
 97
            elif request_status == 401:
 98
                log.error("Not authorized, the API key given is not valid, and does not correspond to a user.")
 99
            elif request_status == 406:
100
                log.error("Not acceptable, your IP address has exceeded the API limit.")
101
            elif request_status == 500:
102
                log.error("Internal server error, something failed to execute properly on the Prowl side.")
103
            else:
104
                log.error("Unknown error when sending Prowl message")
106
register_plugin(OutputProwl, 'prowl', api_ver=2)