scraper.rb 3.9 KB

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