2
from flexget.utils.search import StringComparator
3
from flexget.plugin import get_plugins_by_group, PluginWarning, PluginError, \
4
register_parser_option, register_plugin
6
log = logging.getLogger('urlrewrite_search')
9
class SearchPlugins(object):
12
Implements --search-plugins
15
def on_process_start(self, feed):
16
if feed.manager.options.search_plugins:
17
feed.manager.disable_feeds()
18
header = '-- Supported search plugins: '
19
header = header + '-' * (79 - len(header))
21
for plugin in get_plugins_by_group('search'):
22
print ' %s' % plugin.name
26
class PluginSearch(object):
28
Search entry from sites. Accepts list of known search plugins, list is in priority order.
29
Once hit has been found no more searches are performed. Should be used only when
30
there is no other way to get working download url, ie. when input plugin does not provide
31
any downloadable urls.
39
.. note:: Some url rewriters will use search plugins automatically if enry url
40
points into a search page.
44
from flexget import validator
45
search = validator.factory('list')
47
for plugin in get_plugins_by_group('search'):
48
# If the plugin has a validator, get it's validator and make it a
49
# child of the search plugins
50
if not hasattr(plugin.instance, 'validator'):
51
# Create choice validator for plugins without validators later
52
names.append(plugin.name)
54
plugin_validator = plugin.instance.validator()
55
if isinstance(plugin_validator, validator.Validator):
56
search.accept('dict').accept(plugin_validator, key=plugin.name)
58
log.error("plugin %s has a validator method, but does not "
59
"return a validator instance when called with "
60
"search plugin." % plugin.name)
61
search.accept('choice').accept_choices(names)
64
def on_feed_urlrewrite(self, feed, config):
65
# no searches in unit test mode
66
if feed.manager.unit_test:
70
for plugin in get_plugins_by_group('search'):
71
plugins[plugin.name] = plugin.instance
74
for entry in feed.accepted:
76
# loop through configured searches
79
if isinstance(name, dict):
80
# assume the name is the first/only key in the dict.
81
name, search_config = name.items()[0]
82
log.verbose('Searching `%s` from %s' % (entry['title'], name))
84
results = plugins[name].search(entry['title'], StringComparator(cutoff=0.9), search_config)
86
url = results[0]['url']
87
log.debug('Found url: %s' % url)
91
except (PluginError, PluginWarning), pw:
92
log.verbose('Failed: %s' % pw.value)
97
# If I don't have a URL, doesn't matter if I'm immortal...
98
entry['immortal'] = False
99
feed.reject(entry, 'search failed')
101
register_plugin(PluginSearch, 'urlrewrite_search', api_ver=2)
102
register_plugin(SearchPlugins, '--search-plugins', builtin=True)
103
register_parser_option('--search-plugins', action='store_true', dest='search_plugins', default=False,
104
help='List supported search plugins.')