1 from __future__
import unicode_literals
, division
, absolute_import
6 from flexget
import plugin
7 from flexget
.event
import event
8 from flexget
.config_schema
import one_or_more
9 from flexget
.plugin
import get_plugin_by_name
10 from flexget
.utils
.tools
import TimedDict
12 log
= logging
.getLogger('my_exists_movie')
15 class FilterExistsMovie(object):
17 Reject existing movies.
21 exists_movie: /storage/movies/
24 schema
= one_or_more({'type': 'string', 'format': 'path'}
)
27 self
.cache
= TimedDict(cache_time
='1 hour')
29 def build_config(self
, config
):
30 # if only a single path is passed turn it into a 1 element list
31 if isinstance(config
, basestring
):
36 def on_task_filter(self
, task
, config
):
37 # if not task.accepted:
38 # log.debug('nothing accepted, aborting')
41 config
= self
.build_config(config
)
42 imdb_lookup
= plugin
.get_plugin_by_name('imdb_lookup').instance
44 incompatible_files
= 0
45 incompatible_entries
= 0
49 # list of imdb ids gathered from paths / cache
53 folder
= os
.path
.expanduser(folder
)
54 # see if this path has already been scanned
55 if folder
in self
.cache
:
56 log
.verbose('Using cached scan for %s ...' % folder
)
57 imdb_ids
.extend(self
.cache
[folder
])
62 if not os
.path
.isdir(folder
):
63 log
.critical('Path %s does not exist' % folder
)
66 log
.verbose('Scanning path %s ...' % folder
)
68 # Help debugging by removing a lot of noise
69 #logging.getLogger('movieparser').setLevel(logging.WARNING)
70 #logging.getLogger('imdb_lookup').setLevel(logging.WARNING)
74 # TODO: add also video files?
75 for root
, dirs
, files
in os
.walk(folder
):
77 pattern
= re
.compile(".*\.(avi|mkv|mp4|mpg|webm)")
78 if not re
.search(pattern
, item
):
82 movie
= get_plugin_by_name('parsing').instance
.parse_movie(item
)
85 imdb_id
= imdb_lookup
.imdb_id_lookup(movie_title
=movie
.name
,
88 if imdb_id
in path_ids
:
89 log
.trace('duplicate %s' % item
)
91 if imdb_id
is not None:
92 log
.trace('adding: %s' % imdb_id
)
93 path_ids
.append(imdb_id
)
94 except plugin
.PluginError
as e
:
95 log
.trace('%s lookup failed (%s)' % (item
, e
.value
))
96 incompatible_files
+= 1
98 # store to cache and extend to found list
99 self
.cache
[folder
] = path_ids
100 imdb_ids
.extend(path_ids
)
102 log
.debug('-- Start filtering entries ----------------------------------')
104 # do actual filtering
105 for entry
in task
.accepted
:
107 if not entry
.get('imdb_id', eval_lazy
=False):
109 imdb_lookup
.lookup(entry
)
110 except plugin
.PluginError
as e
:
111 log
.trace('entry %s imdb failed (%s)' % (entry
['title'], e
.value
))
112 incompatible_entries
+= 1
116 if entry
['imdb_id'] in imdb_ids
:
117 entry
.reject('movie exists')
119 if incompatible_dirs
or incompatible_entries
:
120 log
.verbose('There were some incompatible items. %s of %s entries '
121 'and %s of %s directories could not be verified.' %
122 (incompatible_entries
, count_entries
, incompatible_dirs
, count_dirs
))
124 log
.debug('-- Finished filtering entries -------------------------------')
126 @event('plugin.register')
127 def register_plugin():
128 plugin
.register(FilterExistsMovie
, 'my_exists_movie', groups
=['exists'], api_ver
=2)