flexget.plugins.input_inject
Covered: 34 lines
Missed: 34 lines
Skipped 27 lines
Percent: 50 %
 1
import logging
 2
import yaml
 3
from flexget.feed import Entry
 4
from flexget.plugin import *
 5
from flexget.utils.tools import str_to_boolean
 7
log = logging.getLogger('inject')
10
class InputInject(object):
11
    """
12
        Allows injecting imaginary entry for FlexGet to process.
14
        Syntax:
16
        --inject <TITLE> [URL] [ACCEPTED] [IMMORTAL]
18
        Random url will be generated. All other inputs from freed(s) are disabled.
21
        Example use:
23
        flexget --inject "Some.Series.S02E12.Imaginary" --feed my-series --learn
25
        This would inject imaginary series into a single feed and learn it as a downloaded,
26
        assuming feed accepts the injected entry.
28
    """
30
    def validator(self):
31
        from flexget import validator
32
        return validator.factory('any')
34
    options = {}
36
    @staticmethod
37
    def optik_series(option, opt, value, parser):
38
        """--inject <TITLE> [URL] [ACCEPTED] [IMMORTAL]"""
40
        index = 0
41
        for arg in parser.rargs[:]:
42
            if arg.startswith('-'):
43
                break
44
            index += 1
45
            if index == 1:
46
                InputInject.options['entry'] = {'title': arg}
47
            elif index == 2:
48
                InputInject.options['entry']['url'] = arg
49
            elif '=' in arg:
50
                field, val = arg.split('=')
51
                InputInject.options['entry'][field] = yaml.load(val)
52
            elif index == 3:
53
                if arg.lower() == 'accept':
54
                    InputInject.options['accept'] = True
55
                else:
56
                    InputInject.options['accept'] = str_to_boolean(arg)
57
            elif index == 4:
58
                if arg.lower() == 'force':
59
                    InputInject.options['entry']['immortal'] = True
60
                else:
61
                    InputInject.options['entry']['immortal'] = str_to_boolean(arg)
62
            else:
63
                log.critical('Unknown --inject parameter %s' % arg)
65
    @priority(255)
66
    def on_feed_input(self, feed):
67
        if not InputInject.options:
68
            return
71
        log.info('Disabling the rest of the input phase.')
72
        feed.disable_phase('input')
75
        import string
76
        import random
78
        entry = Entry(InputInject.options['entry'], injected=True)
79
        if not 'url' in entry:
80
            entry['url'] = 'http://localhost/inject/%s' % ''.join([random.choice(string.letters + string.digits) for x in range(1, 30)])
81
        if entry.get('immortal'):
82
            log.debug('Injected entry is immortal')
84
        feed.entries.append(entry)
86
        if InputInject.options.get('accept', False):
87
            log.debug('accepting the injection')
88
            feed.accept(entry, '--inject accepted')
91
register_plugin(InputInject, '--inject', debug=True, builtin=True)
93
register_parser_option('--inject', action='callback', callback=InputInject.optik_series,
94
                       help='Injects entry to all executed feeds: <TITLE> [URL] [ACCEPT] [FORCE]')