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