flexget.plugins.input.inject
Covered: 31 lines
Missed: 34 lines
Skipped 26 lines
Percent: 47 %
 1
import logging
 2
import yaml
 3
from flexget.entry 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
    options = {}
32
    @staticmethod
33
    def optik_series(option, opt, value, parser):
34
        """--inject <TITLE> [URL] [ACCEPTED] [IMMORTAL]"""
36
        index = 0
37
        for arg in parser.rargs[:]:
38
            if arg.startswith('-'):
39
                break
40
            index += 1
41
            if index == 1:
42
                InputInject.options['entry'] = {'title': arg}
43
            elif index == 2:
44
                InputInject.options['entry']['url'] = arg
45
            elif '=' in arg:
46
                field, val = arg.split('=')
47
                InputInject.options['entry'][field] = yaml.load(val)
48
            elif index == 3:
49
                if arg.lower() == 'accept':
50
                    InputInject.options['accept'] = True
51
                else:
52
                    InputInject.options['accept'] = str_to_boolean(arg)
53
            elif index == 4:
54
                if arg.lower() == 'force':
55
                    InputInject.options['entry']['immortal'] = True
56
                else:
57
                    InputInject.options['entry']['immortal'] = str_to_boolean(arg)
58
            else:
59
                log.critical('Unknown --inject parameter %s' % arg)
61
    @priority(255)
62
    def on_feed_input(self, feed):
63
        if not InputInject.options:
64
            return
67
        log.info('Disabling the rest of the input phase.')
68
        feed.disable_phase('input')
71
        import string
72
        import random
74
        entry = Entry(InputInject.options['entry'], injected=True)
75
        if not 'url' in entry:
76
            entry['url'] = 'http://localhost/inject/%s' % ''.join([random.choice(string.letters + string.digits) for x in range(1, 30)])
77
        if entry.get('immortal'):
78
            log.debug('Injected entry is immortal')
80
        feed.entries.append(entry)
82
        if InputInject.options.get('accept', False):
83
            log.debug('accepting the injection')
84
            feed.accept(entry, '--inject accepted')
87
register_plugin(InputInject, '--inject', debug=True, builtin=True)
89
register_parser_option('--inject', action='callback', callback=InputInject.optik_series,
90
                       help='Injects entry to all executed feeds: <TITLE> [URL] [ACCEPT] [FORCE]')