scraper.rb 3.4 KB

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