flexget.plugins.output_prowl
Covered: 28 lines
Missed: 43 lines
Skipped 23 lines
Percent: 39 %
 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
 8
log = logging.getLogger('prowl')
10
headers = {'User-Agent': "FlexGet Prowl plugin/%s" % str(__version__), 
11
           'Content-type': "application/x-www-form-urlencoded"}
14
class OutputProwl(object):
15
    """
16
    prowl:
17
      apikey: xxxxxxx
18
      [application: application name, default FlexGet]
19
      [event: event title, default New Release]
20
      [priority: -2 - 2 (2 = highest), default 0]
22
    Configuration parameters are also supported from entries (eg. through set).
23
    """
25
    def validator(self):
26
        from flexget import validator
27
        config = validator.factory('dict')
28
        config.accept('text', key='apikey', required=True)
29
        config.accept('text', key='application')
30
        config.accept('text', key='event')
31
        config.accept('integer', key='priority')
32
        return config
34
    def on_process_start(self, feed):
35
        """                                                                                                                                                                                                     
36
            Register the usable set: keywords.
37
        """ 
38
        set_plugin = get_plugin_by_name('set') 
39
        set_plugin.instance.register_keys({'apikey': 'text', 'application': 'text', \
40
                                           'event': 'text', 'priority': 'integer'})
42
    def get_config(self, feed):
43
        config = feed.config.get('prowl', {})
44
        if isinstance(config, bool):
45
            config = {'enabled': config}
46
        config.setdefault('apikey', '')
47
        config.setdefault('application', 'FlexGet')
48
        config.setdefault('event', 'New release')
49
        config.setdefault('priority', 0)
50
        return config                                                                                                                                                                                           
52
    def on_feed_output(self, feed):
53
        for entry in feed.accepted:
55
            if feed.manager.options.test:
56
                log.info("Would send prowl message about: %s", entry['title'])
57
                continue
60
            config = self.get_config(feed)
61
            apikey = entry.get('apikey', config['apikey'])
62
            application = entry.get('application', config['application'])
63
            event = entry.get('event', config['event'])
64
            priority = entry.get('priority', config['priority'])
65
            description = entry['title']
68
            h = HTTPSConnection('prowl.weks.net')
71
            data = {'priority': priority, 'application': application, 'apikey': apikey, \
72
                    'event': event, 'description': description}
73
            h.request("POST", "/publicapi/add", headers=headers, body=urlencode(data))
76
            response = h.getresponse()
77
            request_status = response.status
80
            if request_status == 200:
81
                log.debug("Prowl message sent")
82
            elif request_status == 400: 
83
                log.error("Bad request, the parameters you provided did not validate")
84
            elif request_status == 401: 
85
                log.error("Not authorized, the API key given is not valid, and does not correspond to a user.")
86
            elif request_status == 406: 
87
                log.error("Not acceptable, your IP address has exceeded the API limit.")
88
            elif request_status == 500: 
89
                log.error("Internal server error, something failed to execute properly on the Prowl side.")
90
            else:
91
                log.error("Unknown error when sending Prowl message")
93
register_plugin(OutputProwl, 'prowl')