website_agent.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. nodes = doc.xpath(xpath)
  126. else
  127. error '"css" or "xpath" is required for HTML or XML extraction'
  128. return
  129. end
  130. unless Nokogiri::XML::NodeSet === nodes
  131. error "The result of HTML/XML extraction was not a NodeSet"
  132. return
  133. end
  134. result = nodes.map { |node|
  135. if extraction_details['attr']
  136. node.attr(extraction_details['attr'])
  137. elsif extraction_details['text']
  138. node.text()
  139. else
  140. error '"attr" or "text" is required on HTML or XML extraction patterns'
  141. return
  142. end
  143. }
  144. log "Extracting #{extraction_type} at #{xpath || css}: #{result}"
  145. end
  146. output[name] = result
  147. end
  148. num_unique_lengths = interpolated['extract'].keys.map { |name| output[name].length }.uniq
  149. if num_unique_lengths.length != 1
  150. error "Got an uneven number of matches for #{interpolated['name']}: #{interpolated['extract'].inspect}"
  151. return
  152. end
  153. old_events = previous_payloads num_unique_lengths.first
  154. num_unique_lengths.first.times do |index|
  155. result = {}
  156. interpolated['extract'].keys.each do |name|
  157. result[name] = output[name][index]
  158. if name.to_s == 'url'
  159. result[name] = (response.env[:url] + result[name]).to_s
  160. end
  161. end
  162. if store_payload!(old_events, result)
  163. log "Storing new parsed result for '#{name}': #{result.inspect}"
  164. create_event :payload => result
  165. end
  166. end
  167. end
  168. else
  169. error "Failed: #{response.inspect}"
  170. end
  171. end
  172. end
  173. def receive(incoming_events)
  174. incoming_events.each do |event|
  175. url_to_scrape = event.payload['url']
  176. check_url(url_to_scrape) if url_to_scrape =~ /^https?:\/\//i
  177. end
  178. end
  179. private
  180. # This method returns true if the result should be stored as a new event.
  181. # If mode is set to 'on_change', this method may return false and update an existing
  182. # event to expire further in the future.
  183. def store_payload!(old_events, result)
  184. if !interpolated['mode'].present?
  185. return true
  186. elsif interpolated['mode'].to_s == "all"
  187. return true
  188. elsif interpolated['mode'].to_s == "on_change"
  189. result_json = result.to_json
  190. old_events.each do |old_event|
  191. if old_event.payload.to_json == result_json
  192. old_event.expires_at = new_event_expiration_date
  193. old_event.save!
  194. return false
  195. end
  196. end
  197. return true
  198. end
  199. raise "Illegal options[mode]: " + interpolated['mode'].to_s
  200. end
  201. def previous_payloads(num_events)
  202. if interpolated['uniqueness_look_back'].present?
  203. look_back = interpolated['uniqueness_look_back'].to_i
  204. else
  205. # Larger of UNIQUENESS_FACTOR * num_events and UNIQUENESS_LOOK_BACK
  206. look_back = UNIQUENESS_FACTOR * num_events
  207. if look_back < UNIQUENESS_LOOK_BACK
  208. look_back = UNIQUENESS_LOOK_BACK
  209. end
  210. end
  211. events.order("id desc").limit(look_back) if interpolated['mode'].present? && interpolated['mode'].to_s == "on_change"
  212. end
  213. def extract_full_json?
  214. !interpolated['extract'].present? && extraction_type == "json"
  215. end
  216. def extraction_type
  217. (interpolated['type'] || begin
  218. if interpolated['url'] =~ /\.(rss|xml)$/i
  219. "xml"
  220. elsif interpolated['url'] =~ /\.json$/i
  221. "json"
  222. else
  223. "html"
  224. end
  225. end).to_s
  226. end
  227. def parse(data)
  228. case extraction_type
  229. when "xml"
  230. Nokogiri::XML(data)
  231. when "json"
  232. JSON.parse(data)
  233. when "html"
  234. Nokogiri::HTML(data)
  235. else
  236. raise "Unknown extraction type #{extraction_type}"
  237. end
  238. end
  239. def is_positive_integer?(value)
  240. begin
  241. Integer(value) >= 0
  242. rescue
  243. false
  244. end
  245. end
  246. def faraday
  247. @faraday ||= Faraday.new { |builder|
  248. builder.headers = headers if headers.length > 0
  249. if (user_agent = interpolated['user_agent']).present?
  250. builder.headers[:user_agent] = user_agent
  251. end
  252. builder.use FaradayMiddleware::FollowRedirects
  253. builder.request :url_encoded
  254. if userinfo = basic_auth_credentials()
  255. builder.request :basic_auth, *userinfo
  256. end
  257. case backend = faraday_backend
  258. when :typhoeus
  259. require 'typhoeus/adapters/faraday'
  260. end
  261. builder.adapter backend
  262. }
  263. end
  264. def faraday_backend
  265. ENV.fetch('FARADAY_HTTP_BACKEND', 'typhoeus').to_sym
  266. end
  267. def basic_auth_credentials
  268. case value = interpolated['basic_auth']
  269. when nil, ''
  270. return nil
  271. when Array
  272. return value if value.size == 2
  273. when /:/
  274. return value.split(/:/, 2)
  275. end
  276. raise "bad value for basic_auth: #{value.inspect}"
  277. end
  278. def headers
  279. interpolated['headers'].presence || {}
  280. end
  281. end
  282. end