flexget.plugins.filter_exists_movie
Covered: 22 lines
Missed: 55 lines
Skipped 31 lines
Percent: 28 %
  1
import os
  2
import logging
  3
from flexget.plugin import register_plugin, priority, PluginError, get_plugin_by_name
  4
from flexget.feed import Entry
  6
log = logging.getLogger('exists_movie')
  9
class FilterExistsMovie(object):
 11
    """
 12
        Reject existing movies.
 14
        Example:
 16
        exists_movie: /storage/movies/
 17
    """
 19
    skip = ['cd1', 'cd2', 'subs', 'sample']
 21
    def validator(self):
 22
        from flexget import validator
 23
        root = validator.factory()
 24
        root.accept('path')
 25
        bundle = root.accept('list')
 26
        bundle.accept('path')
 27
        return root
 29
    def get_config(self, feed):
 30
        config = feed.config.get('exists_movie', [])
 32
        if isinstance(config, basestring):
 33
            config = [config]
 34
        return config
 36
    @priority(-1)
 37
    def on_feed_filter(self, feed):
 38
        if not feed.accepted:
 39
            log.debug('nothing accepted, aborting')
 40
            return
 42
        config = self.get_config(feed)
 43
        imdb_lookup = get_plugin_by_name('imdb_lookup').instance.lookup
 45
        incompatible_dirs = 0
 46
        incompatible_entries = 0
 47
        count_entries = 0
 48
        count_dirs = 0
 51
        imdb_urls = []
 53
        for path in config:
 55
            path = str(os.path.expanduser(path))
 56
            if not os.path.exists(path):
 57
                log.critical('Path %s does not exist' % path)
 58
                continue
 60
            feed.verbose_progress('Scanning path %s ...' % path, log)
 63
            for root, dirs, files in os.walk(path):
 65
                dirs = [x.decode('utf-8', 'ignore') for x in dirs]
 70
                for item in dirs:
 71
                    if item.lower() in self.skip:
 72
                        continue
 73
                    count_dirs += 1
 74
                    fake_entry = Entry()
 75
                    fake_entry['title'] = item
 76
                    fake_entry['url'] = 'file://%s/%s' % (path, item)
 77
                    try:
 78
                        imdb_lookup(feed, fake_entry)
 79
                        imdb_url = fake_entry['imdb_url']
 80
                        if imdb_url in imdb_urls:
 81
                            log.debugall('duplicate %s' % fake_entry['title'])
 82
                            continue
 83
                        imdb_urls.append(imdb_url)
 84
                    except PluginError, e:
 85
                        log.debugall('%s lookup failed (%s)' % (fake_entry['title'], e.value))
 86
                        incompatible_dirs += 1
 88
        log.debug('-- Start filtering entries ----------------------------------')
 91
        for entry in feed.accepted:
 92
            count_entries += 1
 93
            if not 'imdb_url' in entry:
 94
                try:
 95
                    imdb_lookup(feed, entry)
 96
                except PluginError, e:
 97
                    log.debugall('entry %s imdb failed (%s)' % (entry['title'], e.value))
 98
                    incompatible_entries += 1
 99
                    continue
100
            if entry['imdb_url'] in imdb_urls:
101
                feed.reject(entry, 'movie exists')
103
        if incompatible_dirs or incompatible_entries:
104
            feed.verbose_progress('There were some incompatible items. %s of %s entries and %s of %s directories could not be verified.' % \
105
                (incompatible_entries, count_entries, incompatible_dirs, count_dirs), log)
107
register_plugin(FilterExistsMovie, 'exists_movie', groups=['exists'])