website_agent.rb 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. require 'nokogiri'
  2. require 'faraday'
  3. require 'faraday_middleware'
  4. require 'date'
  5. module Agents
  6. class WebsiteAgent < Agent
  7. default_schedule "every_12h"
  8. UNIQUENESS_LOOK_BACK = 200
  9. UNIQUENESS_FACTOR = 3
  10. description <<-MD
  11. The WebsiteAgent scrapes a website, XML document, or JSON feed and creates Events based on the results.
  12. Specify a `url` and select a `mode` for when to create Events based on the scraped data, either `all` or `on_change`.
  13. `url` can be a single url, or an array of urls (for example, for multiple pages with the exact same structure but different content to scrape)
  14. The `type` value can be `xml`, `html`, or `json`.
  15. To tell the Agent how to parse the content, specify `extract` as a hash with keys naming the extractions and values of hashes.
  16. When parsing HTML or XML, these sub-hashes specify how to extract with either a `css` CSS selector or a `xpath` XPath expression and either `"text": true` or `attr` pointing to an attribute name to grab. An example:
  17. "extract": {
  18. "url": { "css": "#comic img", "attr": "src" },
  19. "title": { "css": "#comic img", "attr": "title" },
  20. "body_text": { "css": "div.main", "text": true }
  21. }
  22. When parsing JSON, these sub-hashes specify [JSONPaths](http://goessner.net/articles/JsonPath/) to the values that you care about. For example:
  23. "extract": {
  24. "title": { "path": "results.data[*].title" },
  25. "description": { "path": "results.data[*].description" }
  26. }
  27. Note that for all of the formats, whatever you extract MUST have the same number of matches for each extractor. E.g., if you're extracting rows, all extractors must match all rows. For generating CSS selectors, something like [SelectorGadget](http://selectorgadget.com) may be helpful.
  28. Can be configured to use HTTP basic auth by including the `basic_auth` parameter with `"username:password"`, or `["username", "password"]`.
  29. Set `expected_update_period_in_days` to the maximum amount of time that you'd expect to pass between Events being created by this Agent. This is only used to set the "working" status.
  30. Set `uniqueness_look_back` to limit the number of events checked for uniqueness (typically for performance). This defaults to the larger of #{UNIQUENESS_LOOK_BACK} or #{UNIQUENESS_FACTOR}x the number of detected received results.
  31. Set `force_encoding` to an encoding name if the website does not return a Content-Type header with a proper charset.
  32. Set `user_agent` to a custom User-Agent name if the website does not like the default value ("Faraday v#{Faraday::VERSION}").
  33. The `headers` field is optional. When present, it should be a hash of headers to send with the request.
  34. The WebsiteAgent can also scrape based on incoming events. It will scrape the url contained in the `url` key of the incoming event payload.
  35. MD
  36. event_description do
  37. "Events will have the fields you specified. Your options look like:\n\n #{Utils.pretty_print interpolated['extract']}"
  38. end
  39. def working?
  40. event_created_within?(interpolated['expected_update_period_in_days']) && !recent_error_logs?
  41. end
  42. def default_options
  43. {
  44. 'expected_update_period_in_days' => "2",
  45. 'url' => "http://xkcd.com",
  46. 'type' => "html",
  47. 'mode' => "on_change",
  48. 'extract' => {
  49. 'url' => { 'css' => "#comic img", 'attr' => "src" },
  50. 'title' => { 'css' => "#comic img", 'attr' => "alt" },
  51. 'hovertext' => { 'css' => "#comic img", 'attr' => "title" }
  52. }
  53. }
  54. end
  55. def validate_options
  56. # Check for required fields
  57. errors.add(:base, "url and expected_update_period_in_days are required") unless options['expected_update_period_in_days'].present? && options['url'].present?
  58. if !options['extract'].present? && extraction_type != "json"
  59. errors.add(:base, "extract is required for all types except json")
  60. end
  61. # Check for optional fields
  62. if options['mode'].present?
  63. errors.add(:base, "mode must be set to on_change or all") unless %w[on_change all].include?(options['mode'])
  64. end
  65. if options['expected_update_period_in_days'].present?
  66. errors.add(:base, "Invalid expected_update_period_in_days format") unless is_positive_integer?(options['expected_update_period_in_days'])
  67. end
  68. if options['uniqueness_look_back'].present?
  69. errors.add(:base, "Invalid uniqueness_look_back format") unless is_positive_integer?(options['uniqueness_look_back'])
  70. end
  71. if (encoding = options['force_encoding']).present?
  72. case encoding
  73. when String
  74. begin
  75. Encoding.find(encoding)
  76. rescue ArgumentError
  77. errors.add(:base, "Unknown encoding: #{encoding.inspect}")
  78. end
  79. else
  80. errors.add(:base, "force_encoding must be a string")
  81. end
  82. end
  83. if options['user_agent'].present?
  84. errors.add(:base, "user_agent must be a string") unless options['user_agent'].is_a?(String)
  85. end
  86. unless headers.is_a?(Hash)
  87. errors.add(:base, "if provided, headers must be a hash")
  88. end
  89. begin
  90. basic_auth_credentials()
  91. rescue => e
  92. errors.add(:base, e.message)
  93. end
  94. end
  95. def check
  96. check_url interpolated['url']
  97. end
  98. def check_url(in_url)
  99. return unless in_url.present?
  100. Array(in_url).each do |url|
  101. log "Fetching #{url}"
  102. response = faraday.get(url)
  103. if response.success?
  104. body = response.body
  105. if (encoding = interpolated['force_encoding']).present?
  106. body = body.encode(Encoding::UTF_8, encoding)
  107. end
  108. doc = parse(body)
  109. if extract_full_json?
  110. if store_payload!(previous_payloads(1), doc)
  111. log "Storing new result for '#{name}': #{doc.inspect}"
  112. create_event :payload => doc
  113. end
  114. else
  115. output = {}
  116. interpolated['extract'].each do |name, extraction_details|
  117. if extraction_type == "json"
  118. result = Utils.values_at(doc, extraction_details['path'])
  119. log "Extracting #{extraction_type} at #{extraction_details['path']}: #{result}"
  120. else
  121. case
  122. when css = extraction_details['css']
  123. nodes = doc.css(css)
  124. when xpath = extraction_details['xpath']
  125. doc.remove_namespaces! # ignore xmlns, useful when parsing atom feeds
  126. nodes = doc.xpath(xpath)
  127. else
  128. error '"css" or "xpath" is required for HTML or XML extraction'
  129. return
  130. end
  131. unless Nokogiri::XML::NodeSet === nodes
  132. error "The result of HTML/XML extraction was not a NodeSet"
  133. return
  134. end
  135. result = nodes.map { |node|
  136. if extraction_details['attr']
  137. node.attr(extraction_details['attr'])
  138. elsif extraction_details['text']
  139. node.text()
  140. else
  141. error '"attr" or "text" is required on HTML or XML extraction patterns'
  142. return
  143. end
  144. }
  145. log "Extracting #{extraction_type} at #{xpath || css}: #{result}"
  146. end
  147. output[name] = result
  148. end
  149. num_unique_lengths = interpolated['extract'].keys.map { |name| output[name].length }.uniq
  150. if num_unique_lengths.length != 1
  151. error "Got an uneven number of matches for #{interpolated['name']}: #{interpolated['extract'].inspect}"
  152. return
  153. end
  154. old_events = previous_payloads num_unique_lengths.first
  155. num_unique_lengths.first.times do |index|
  156. result = {}
  157. interpolated['extract'].keys.each do |name|
  158. result[name] = output[name][index]
  159. if name.to_s == 'url'
  160. result[name] = (response.env[:url] + result[name]).to_s
  161. end
  162. end
  163. if store_payload!(old_events, result)
  164. log "Storing new parsed result for '#{name}': #{result.inspect}"
  165. create_event :payload => result
  166. end
  167. end
  168. end
  169. else
  170. error "Failed: #{response.inspect}"
  171. end
  172. end
  173. end
  174. def receive(incoming_events)
  175. incoming_events.each do |event|
  176. url_to_scrape = event.payload['url']
  177. check_url(url_to_scrape) if url_to_scrape =~ /^https?:\/\//i
  178. end
  179. end
  180. private
  181. # This method returns true if the result should be stored as a new event.
  182. # If mode is set to 'on_change', this method may return false and update an existing
  183. # event to expire further in the future.
  184. def store_payload!(old_events, result)
  185. if !interpolated['mode'].present?
  186. return true
  187. elsif interpolated['mode'].to_s == "all"
  188. return true
  189. elsif interpolated['mode'].to_s == "on_change"
  190. result_json = result.to_json
  191. old_events.each do |old_event|
  192. if old_event.payload.to_json == result_json
  193. old_event.expires_at = new_event_expiration_date
  194. old_event.save!
  195. return false
  196. end
  197. end
  198. return true
  199. end
  200. raise "Illegal options[mode]: " + interpolated['mode'].to_s
  201. end
  202. def previous_payloads(num_events)
  203. if interpolated['uniqueness_look_back'].present?
  204. look_back = interpolated['uniqueness_look_back'].to_i
  205. else
  206. # Larger of UNIQUENESS_FACTOR * num_events and UNIQUENESS_LOOK_BACK
  207. look_back = UNIQUENESS_FACTOR * num_events
  208. if look_back < UNIQUENESS_LOOK_BACK
  209. look_back = UNIQUENESS_LOOK_BACK
  210. end
  211. end
  212. events.order("id desc").limit(look_back) if interpolated['mode'].present? && interpolated['mode'].to_s == "on_change"
  213. end
  214. def extract_full_json?
  215. !interpolated['extract'].present? && extraction_type == "json"
  216. end
  217. def extraction_type
  218. (interpolated['type'] || begin
  219. if interpolated['url'] =~ /\.(rss|xml)$/i
  220. "xml"
  221. elsif interpolated['url'] =~ /\.json$/i
  222. "json"
  223. else
  224. "html"
  225. end
  226. end).to_s
  227. end
  228. def parse(data)
  229. case extraction_type
  230. when "xml"
  231. Nokogiri::XML(data)
  232. when "json"
  233. JSON.parse(data)
  234. when "html"
  235. Nokogiri::HTML(data)
  236. else
  237. raise "Unknown extraction type #{extraction_type}"
  238. end
  239. end
  240. def is_positive_integer?(value)
  241. begin
  242. Integer(value) >= 0
  243. rescue
  244. false
  245. end
  246. end
  247. def faraday
  248. @faraday ||= Faraday.new { |builder|
  249. builder.headers = headers if headers.length > 0
  250. if (user_agent = interpolated['user_agent']).present?
  251. builder.headers[:user_agent] = user_agent
  252. end
  253. builder.use FaradayMiddleware::FollowRedirects
  254. builder.request :url_encoded
  255. if userinfo = basic_auth_credentials()
  256. builder.request :basic_auth, *userinfo
  257. end
  258. case backend = faraday_backend
  259. when :typhoeus
  260. require 'typhoeus/adapters/faraday'
  261. end
  262. builder.adapter backend
  263. }
  264. end
  265. def faraday_backend
  266. ENV.fetch('FARADAY_HTTP_BACKEND', 'typhoeus').to_sym
  267. end
  268. def basic_auth_credentials
  269. case value = interpolated['basic_auth']
  270. when nil, ''
  271. return nil
  272. when Array
  273. return value if value.size == 2
  274. when /:/
  275. return value.split(/:/, 2)
  276. end
  277. raise "bad value for basic_auth: #{value.inspect}"
  278. end
  279. def headers
  280. interpolated['headers'].presence || {}
  281. end
  282. end
  283. end