flexget.plugins.module_path_by_ext
Covered: 17 lines
Missed: 22 lines
Skipped 13 lines
Percent: 43 %
 1
import logging
 2
import mimetypes
 3
from flexget.plugin import *
 5
log = logging.getLogger('path_by_ext')
 7
class PluginPathByExt:
 8
    """
 9
        Allows specifying path based on content-type
11
        Example:
13
        path_by_ext:
14
          torrent: ~/watch/torrent/
15
          nzb: ~/watch/nzb/
16
    """
18
    def validator(self):
19
        from flexget import validator
20
        config = validator.factory('dict')
21
        config.accept_any_key('any')
22
        return config
24
    def on_feed_modify(self, feed):
25
        self.ext(feed, self.set_path)
27
    def set_path(self, entry, path):
28
        log.debug('Setting %s path to %s' % (entry['title'], path))
29
        entry['path'] = path    
31
    def ext(self, feed, callback):
32
        config = feed.config
33
        for entry in feed.entries:
34
            if 'mime-type' in entry:
36
                if entry['mime-type'] in config:
37
                    callback(entry, config[entry['mime-type']])
39
                ext = mimetypes.types_map.get(entry['mime-type'])
40
                path = config.get(ext) or config.get(ext[1:])
41
                if path:
42
                    callback(entry, path)
43
                else:
44
                    log.debug('Unknown mimetype %s' % entry['mime-type'])
45
            else:
47
                for ext, path in config.iteritems():
48
                    if entry['url'].endswith('.'+ext):
49
                        callback(entry, path)
51
register_plugin(PluginPathByExt, 'path_by_ext')