/flexget/plugins/urlrewrite_search.py

https://bitbucket.org/edhaker13/flexget · Python · 107 lines · 66 code · 13 blank · 28 comment · 16 complexity · 78762cb25e0fd49d05901aa33c685b93 MD5 · raw file

  1. from __future__ import unicode_literals, division, absolute_import
  2. import logging
  3. from flexget.utils.search import StringComparator
  4. from flexget.plugin import get_plugins_by_group, PluginWarning, PluginError, \
  5. register_parser_option, register_plugin, priority
  6. log = logging.getLogger('urlrewrite_search')
  7. class SearchPlugins(object):
  8. """
  9. Implements --search-plugins
  10. """
  11. def on_process_start(self, task):
  12. if task.manager.options.search_plugins:
  13. task.manager.disable_tasks()
  14. header = '-- Supported search plugins: '
  15. header = header + '-' * (79 - len(header))
  16. print header
  17. for plugin in get_plugins_by_group('search'):
  18. print ' %s' % plugin.name
  19. print '-' * 79
  20. class PluginSearch(object):
  21. """
  22. Search entry from sites. Accepts list of known search plugins, list is in priority order.
  23. Once hit has been found no more searches are performed. Should be used only when
  24. there is no other way to get working download url, ie. when input plugin does not provide
  25. any downloadable urls.
  26. Example::
  27. urlrewrite_search:
  28. - newtorrents
  29. - piratebay
  30. .. note:: Some url rewriters will use search plugins automatically if entry url
  31. points into a search page.
  32. """
  33. def validator(self):
  34. from flexget import validator
  35. search = validator.factory('list')
  36. names = []
  37. for plugin in get_plugins_by_group('search'):
  38. # If the plugin has a validator, get it's validator and make it a
  39. # child of the search plugins
  40. if not hasattr(plugin.instance, 'validator'):
  41. # Create choice validator for plugins without validators later
  42. names.append(plugin.name)
  43. else:
  44. plugin_validator = plugin.instance.validator()
  45. if isinstance(plugin_validator, validator.Validator):
  46. search.accept('dict').accept(plugin_validator, key=plugin.name)
  47. else:
  48. log.error("plugin %s has a validator method, but does not "
  49. "return a validator instance when called with "
  50. "search plugin." % plugin.name)
  51. search.accept('choice').accept_choices(names)
  52. return search
  53. # Run before main urlrewriting
  54. @priority(130)
  55. def on_task_urlrewrite(self, task, config):
  56. # no searches in unit test mode
  57. if task.manager.unit_test:
  58. return
  59. plugins = {}
  60. for plugin in get_plugins_by_group('search'):
  61. plugins[plugin.name] = plugin.instance
  62. # search accepted
  63. for entry in task.accepted:
  64. found = False
  65. # loop through configured searches
  66. for name in config:
  67. search_config = None
  68. if isinstance(name, dict):
  69. # assume the name is the first/only key in the dict.
  70. name, search_config = name.items()[0]
  71. log.verbose('Searching `%s` from %s' % (entry['title'], name))
  72. try:
  73. results = plugins[name].search(entry['title'], StringComparator(cutoff=0.9), search_config)
  74. if results:
  75. url = results[0]['url']
  76. log.debug('Found url: %s' % url)
  77. entry['url'] = url
  78. found = True
  79. break
  80. except (PluginError, PluginWarning) as pw:
  81. log.verbose('Failed: %s' % pw.value)
  82. continue
  83. # Search failed
  84. if not found:
  85. # If I don't have a URL, doesn't matter if I'm immortal...
  86. entry['immortal'] = False
  87. entry.reject('search failed')
  88. register_plugin(PluginSearch, 'urlrewrite_search', api_ver=2)
  89. register_plugin(SearchPlugins, '--search-plugins', builtin=True)
  90. register_parser_option('--search-plugins', action='store_true', dest='search_plugins', default=False,
  91. help='List supported search plugins.')