scraper.rb 3.8 KB

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