scraper.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. require 'set'
  2. require 'html/pipeline'
  3. module Docs
  4. class Scraper < Doc
  5. class << self
  6. attr_accessor :base_url, :root_path, :html_filters, :text_filters, :options
  7. def inherited(subclass)
  8. super
  9. subclass.class_eval do
  10. extend AutoloadHelper
  11. autoload_all "docs/filters/#{to_s.demodulize.underscore}", 'filter'
  12. end
  13. subclass.options = options.deep_dup
  14. subclass.html_filters = html_filters.inheritable_copy
  15. subclass.text_filters = text_filters.inheritable_copy
  16. end
  17. def filters
  18. html_filters.to_a + text_filters.to_a
  19. end
  20. end
  21. include Instrumentable
  22. self.html_filters = FilterStack.new
  23. self.text_filters = FilterStack.new
  24. self.options = {}
  25. html_filters.push 'container', 'clean_html', 'normalize_urls', 'internal_urls', 'normalize_paths'
  26. text_filters.push 'inner_html', 'clean_text', 'attribution'
  27. def base_url
  28. @base_url ||= URL.parse self.class.base_url
  29. end
  30. def root_url
  31. @root_url ||= root_path? ? URL.parse(File.join(base_url.to_s, root_path)) : base_url.normalize
  32. end
  33. def root_path
  34. self.class.root_path
  35. end
  36. def root_path?
  37. root_path.present? && root_path != '/'
  38. end
  39. def build_page(path)
  40. response = request_one url_for(path)
  41. result = handle_response(response)
  42. yield result if block_given?
  43. result
  44. end
  45. def build_pages
  46. requested_urls = Set.new [root_url.to_s.downcase]
  47. instrument 'running.scraper', urls: requested_urls.to_a
  48. request_all root_url.to_s do |response|
  49. next unless data = handle_response(response)
  50. yield data
  51. next unless data[:internal_urls].present?
  52. next_urls = data[:internal_urls].select { |url| requested_urls.add?(url.downcase) }
  53. instrument 'queued.scraper', urls: next_urls
  54. next_urls
  55. end
  56. end
  57. def options
  58. @options ||= self.class.options.deep_dup.tap do |options|
  59. options.merge! base_url: base_url, root_path: root_path, root_url: root_url
  60. (options[:skip] ||= []).concat ['', '/'] if root_path?
  61. if options[:only] || options[:only_patterns]
  62. (options[:only] ||= []).concat root_path? ? [root_path] : ['', '/']
  63. end
  64. options.freeze
  65. end
  66. end
  67. def pipeline
  68. @pipeline ||= ::HTML::Pipeline.new(self.class.filters).tap do |pipeline|
  69. pipeline.instrumentation_service = Docs
  70. end
  71. end
  72. private
  73. def request_one(url)
  74. raise NotImplementedError
  75. end
  76. def request_all(url, &block)
  77. raise NotImplementedError
  78. end
  79. def process_response?(response)
  80. raise NotImplementedError
  81. end
  82. def url_for(path)
  83. if path.empty? || path == '/'
  84. root_url.to_s
  85. else
  86. File.join(base_url.to_s, path)
  87. end
  88. end
  89. def handle_response(response)
  90. if process_response?(response)
  91. instrument 'process_response.scraper', response: response do
  92. process_response(response)
  93. end
  94. else
  95. instrument 'ignore_response.scraper', response: response
  96. end
  97. end
  98. def process_response(response)
  99. pipeline.call parse(response.body), pipeline_context(response), data = {}
  100. data
  101. end
  102. def pipeline_context(response)
  103. options.merge url: response.url
  104. end
  105. def parse(string)
  106. Parser.new(string).html
  107. end
  108. end
  109. end