flexget.plugins.filter_exists
Covered: 15 lines
Missed: 23 lines
Skipped 15 lines
Percent: 39 %
 1
import os
 2
import logging
 3
from flexget.plugin import register_plugin, priority, PluginWarning
 5
log = logging.getLogger('exists')
 8
class FilterExists(object):
10
    """
11
        Reject entries that already exist in given path.
13
        Example:
15
        exists: /storage/movies/
16
    """
18
    def validator(self):
19
        from flexget import validator
20
        root = validator.factory()
21
        root.accept('path')
22
        bundle = root.accept('list')
23
        bundle.accept('path')
24
        return root
26
    def get_config(self, feed):
27
        config = feed.config.get('exists', None)
29
        if isinstance(config, basestring):
30
            config = [config]
31
        return config
33
    @priority(-1)
34
    def on_feed_filter(self, feed):
35
        config = self.get_config(feed)
36
        for path in config:
38
            path = str(os.path.expanduser(path))
39
            if not os.path.exists(path):
40
                raise PluginWarning('Path %s does not exist' % path, log)
42
            for root, dirs, files in os.walk(path):
44
                dirs = [x.decode('utf-8', 'ignore') for x in dirs]
45
                files = [x.decode('utf-8', 'ignore') for x in files]
46
                for entry in feed.entries:
47
                    name = entry['title']
48
                    if name in dirs or name in files:
49
                        log.debug('Found %s in %s' % (name, root))
50
                        feed.reject(entry, '%s/%s' % (name, root))
52
register_plugin(FilterExists, 'exists')