-
Notifications
You must be signed in to change notification settings - Fork 8
/
content_sort.py
57 lines (48 loc) · 2.05 KB
/
content_sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from __future__ import unicode_literals, division, absolute_import
import logging
import posixpath
from fnmatch import fnmatch
from flexget import plugin
from flexget.event import event
log = logging.getLogger('content_sort')
class FilterContentSort(object):
"""
Set the move_completed_path attribute based on torrent contents. Earlier defined types take predence
Example:
content_sort:
'*.rar': '/path/to/move'
'*.mkv': '/other/path'
"""
schema = {
'type': 'object',
'additionalProperties': {'type': 'string'}
}
def process_entry(self, task, entry, config):
if 'content_files' in entry:
files = entry['content_files']
log.debug('%s files: %s' % (entry['title'], files))
for mask, path in config.items():
log.debug('Checing for: %s' % mask)
for file in files:
log.debug('\t in: %s' % file)
if fnmatch(file, mask):
conf = {'move_completed_path': path }
log.debug('adding set: info to entry:\'%s\' %s' % (entry['title'], conf))
entry.update(conf)
def parse_torrent_files(self, entry):
if 'torrent' in entry and 'content_files' not in entry:
files = [posixpath.join(item['path'], item['name']) for item in entry['torrent'].get_filelist()]
if files:
entry['content_files'] = files
@plugin.priority(149)
def on_task_modify(self, task, config):
if task.options.test or task.options.learn:
log.info('Plugin is partially disabled with --test and --learn because content filename information may not be available')
return
for entry in task.accepted:
# TODO: I don't know if we can parse filenames from nzbs, just do torrents for now
self.parse_torrent_files(entry)
self.process_entry(task, entry, config)
@event('plugin.register')
def register_plugin():
plugin.register(FilterContentSort, 'content_sort', api_ver=2)