website_agent.rb 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. require 'nokogiri'
  2. require 'date'
  3. module Agents
  4. class WebsiteAgent < Agent
  5. include WebRequestConcern
  6. can_dry_run!
  7. can_order_created_events!
  8. default_schedule "every_12h"
  9. UNIQUENESS_LOOK_BACK = 200
  10. UNIQUENESS_FACTOR = 3
  11. description <<-MD
  12. The Website Agent scrapes a website, XML document, or JSON feed and creates Events based on the results.
  13. Specify a `url` and select a `mode` for when to create Events based on the scraped data, either `all` or `on_change`.
  14. `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)
  15. The WebsiteAgent can also scrape based on incoming events. It will scrape the url contained in the `url` key of the incoming event payload, or if you set `url_from_event` it is used as a Liquid template to generate the url to access. If you specify `merge` as the `mode`, it will retain the old payload and update it with the new values.
  16. # Supported Document Types
  17. The `type` value can be `xml`, `html`, `json`, or `text`.
  18. To tell the Agent how to parse the content, specify `extract` as a hash with keys naming the extractions and values of hashes.
  19. 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.
  20. # Scraping HTML and XML
  21. When parsing HTML or XML, these sub-hashes specify how each extraction should be done. The Agent first selects a node set from the document for each extraction key by evaluating either a CSS selector in `css` or an XPath expression in `xpath`. It then evaluates an XPath expression in `value` (default: `.`) on each node in the node set, converting the result into string. Here's an example:
  22. "extract": {
  23. "url": { "css": "#comic img", "value": "@src" },
  24. "title": { "css": "#comic img", "value": "@title" },
  25. "body_text": { "css": "div.main", "value": ".//text()" }
  26. }
  27. "@_attr_" is the XPath expression to extract the value of an attribute named _attr_ from a node, and ".//text()" is to extract all the enclosed texts. To extract the innerHTML, use "./node()"; and to extract the outer HTML, use ".".
  28. You can also use [XPath functions](http://www.w3.org/TR/xpath/#section-String-Functions) like `normalize-space` to strip and squeeze whitespace, `substring-after` to extract part of a text, and `translate` to remove comma from a formatted number, etc. Note that these functions take a string, not a node set, so what you may think would be written as `normalize-space(.//text())` should actually be `normalize-space(.)`.
  29. Beware that when parsing an XML document (i.e. `type` is `xml`) using `xpath` expressions all namespaces are stripped from the document unless a toplevel option `use_namespaces` is set to true.
  30. If the extracted value contains `<![CDATA[content]]` you can get the `content` part if you set `strip_cdata` to true.
  31. # Scraping JSON
  32. When parsing JSON, these sub-hashes specify [JSONPaths](http://goessner.net/articles/JsonPath/) to the values that you care about. For example:
  33. "extract": {
  34. "title": { "path": "results.data[*].title" },
  35. "description": { "path": "results.data[*].description" }
  36. }
  37. # Scraping Text
  38. When parsing text, each sub-hash should contain a `regexp` and `index`. Output text is matched against the regular expression repeatedly from the beginning through to the end, collecting a captured group specified by `index` in each match. Each index should be either an integer or a string name which corresponds to <code>(?&lt;<em>name</em>&gt;...)</code>. For example, to parse lines of <code><em>word</em>: <em>definition</em></code>, the following should work:
  39. "extract": {
  40. "word": { "regexp": "^(.+?): (.+)$", index: 1 },
  41. "definition": { "regexp": "^(.+?): (.+)$", index: 2 }
  42. }
  43. Or if you prefer names to numbers for index:
  44. "extract": {
  45. "word": { "regexp": "^(?<word>.+?): (?<definition>.+)$", index: 'word' },
  46. "definition": { "regexp": "^(?<word>.+?): (?<definition>.+)$", index: 'definition' }
  47. }
  48. To extract the whole content as one event:
  49. "extract": {
  50. "content": { "regexp": "\A(?m:.)*\z", index: 0 }
  51. }
  52. Beware that `.` does not match the newline character (LF) unless the `m` flag is in effect, and `^`/`$` basically match every line beginning/end. See [this document](http://ruby-doc.org/core-#{RUBY_VERSION}/doc/regexp_rdoc.html) to learn the regular expression variant used in this service.
  53. # General Options
  54. Can be configured to use HTTP basic auth by including the `basic_auth` parameter with `"username:password"`, or `["username", "password"]`.
  55. 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.
  56. 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.
  57. Set `force_encoding` to an encoding name if the website is known to respond with a missing, invalid or wrong charset in the Content-Type header. Note that a text content without a charset is taken as encoded in UTF-8 (not ISO-8859-1).
  58. Set `user_agent` to a custom User-Agent name if the website does not like the default value (`#{default_user_agent}`).
  59. The `headers` field is optional. When present, it should be a hash of headers to send with the request.
  60. Set `disable_ssl_verification` to `true` to disable ssl verification.
  61. Set `unzip` to `gzip` to inflate the resource using gzip.
  62. # Liquid Templating
  63. In Liquid templating, the following variable is available:
  64. * `_response_`: A response object with the following keys:
  65. * `status`: HTTP status as integer. (Almost always 200)
  66. * `headers`: Response headers; for example, `{{ _response_.headers.Content-Type }}` expands to the value of the Content-Type header. Keys are insensitive to cases and -/_.
  67. # Ordering Events
  68. #{description_events_order}
  69. MD
  70. event_description do
  71. "Events will have the following fields:\n\n %s" % [
  72. Utils.pretty_print(Hash[options['extract'].keys.map { |key|
  73. [key, "..."]
  74. }])
  75. ]
  76. end
  77. def working?
  78. event_created_within?(options['expected_update_period_in_days']) && !recent_error_logs?
  79. end
  80. def default_options
  81. {
  82. 'expected_update_period_in_days' => "2",
  83. 'url' => "http://xkcd.com",
  84. 'type' => "html",
  85. 'mode' => "on_change",
  86. 'extract' => {
  87. 'url' => { 'css' => "#comic img", 'value' => "@src" },
  88. 'title' => { 'css' => "#comic img", 'value' => "@alt" },
  89. 'hovertext' => { 'css' => "#comic img", 'value' => "@title" }
  90. }
  91. }
  92. end
  93. def validate_options
  94. # Check for required fields
  95. errors.add(:base, "either url or url_from_event is required") unless options['url'].present? || options['url_from_event'].present?
  96. errors.add(:base, "expected_update_period_in_days is required") unless options['expected_update_period_in_days'].present?
  97. validate_extract_options!
  98. # Check for optional fields
  99. if options['mode'].present?
  100. errors.add(:base, "mode must be set to on_change, all or merge") unless %w[on_change all merge].include?(options['mode'])
  101. end
  102. if options['expected_update_period_in_days'].present?
  103. errors.add(:base, "Invalid expected_update_period_in_days format") unless is_positive_integer?(options['expected_update_period_in_days'])
  104. end
  105. if options['uniqueness_look_back'].present?
  106. errors.add(:base, "Invalid uniqueness_look_back format") unless is_positive_integer?(options['uniqueness_look_back'])
  107. end
  108. validate_web_request_options!
  109. end
  110. def validate_extract_options!
  111. extraction_type = (extraction_type() rescue extraction_type(options))
  112. case extract = options['extract']
  113. when Hash
  114. if extract.each_value.any? { |value| !value.is_a?(Hash) }
  115. errors.add(:base, 'extract must be a hash of hashes.')
  116. else
  117. case extraction_type
  118. when 'html', 'xml'
  119. extract.each do |name, details|
  120. case details['css']
  121. when String
  122. # ok
  123. when nil
  124. case details['xpath']
  125. when String
  126. # ok
  127. when nil
  128. errors.add(:base, "When type is html or xml, all extractions must have a css or xpath attribute (bad extraction details for #{name.inspect})")
  129. else
  130. errors.add(:base, "Wrong type of \"xpath\" value in extraction details for #{name.inspect}")
  131. end
  132. else
  133. errors.add(:base, "Wrong type of \"css\" value in extraction details for #{name.inspect}")
  134. end
  135. case details['value']
  136. when String, nil
  137. # ok
  138. else
  139. errors.add(:base, "Wrong type of \"value\" value in extraction details for #{name.inspect}")
  140. end
  141. end
  142. when 'json'
  143. extract.each do |name, details|
  144. case details['path']
  145. when String
  146. # ok
  147. when nil
  148. errors.add(:base, "When type is json, all extractions must have a path attribute (bad extraction details for #{name.inspect})")
  149. else
  150. errors.add(:base, "Wrong type of \"path\" value in extraction details for #{name.inspect}")
  151. end
  152. end
  153. when 'text'
  154. extract.each do |name, details|
  155. case regexp = details['regexp']
  156. when String
  157. begin
  158. re = Regexp.new(regexp)
  159. rescue => e
  160. errors.add(:base, "invalid regexp for #{name.inspect}: #{e.message}")
  161. end
  162. when nil
  163. errors.add(:base, "When type is text, all extractions must have a regexp attribute (bad extraction details for #{name.inspect})")
  164. else
  165. errors.add(:base, "Wrong type of \"regexp\" value in extraction details for #{name.inspect}")
  166. end
  167. case index = details['index']
  168. when Integer, /\A\d+\z/
  169. # ok
  170. when String
  171. if re && !re.names.include?(index)
  172. errors.add(:base, "no named capture #{index.inspect} found in regexp for #{name.inspect})")
  173. end
  174. when nil
  175. errors.add(:base, "When type is text, all extractions must have an index attribute (bad extraction details for #{name.inspect})")
  176. else
  177. errors.add(:base, "Wrong type of \"index\" value in extraction details for #{name.inspect}")
  178. end
  179. end
  180. when /\{/
  181. # Liquid templating
  182. else
  183. errors.add(:base, "Unknown extraction type #{extraction_type.inspect}")
  184. end
  185. end
  186. when nil
  187. unless extraction_type == 'json'
  188. errors.add(:base, 'extract is required for all types except json')
  189. end
  190. else
  191. errors.add(:base, 'extract must be a hash')
  192. end
  193. end
  194. def check
  195. check_urls(interpolated['url'])
  196. end
  197. def check_urls(in_url, payload = {})
  198. return unless in_url.present?
  199. Array(in_url).each do |url|
  200. check_url(url, payload)
  201. end
  202. end
  203. def check_url(url, payload = {})
  204. unless /\Ahttps?:\/\//i === url
  205. error "Ignoring a non-HTTP url: #{url.inspect}"
  206. return
  207. end
  208. log "Fetching #{url}"
  209. response = faraday.get(url)
  210. raise "Failed: #{response.inspect}" unless response.success?
  211. interpolation_context.stack {
  212. interpolation_context['_response_'] = ResponseDrop.new(response)
  213. body = response.body
  214. doc = parse(body)
  215. if extract_full_json?
  216. if store_payload!(previous_payloads(1), doc)
  217. log "Storing new result for '#{name}': #{doc.inspect}"
  218. create_event payload: payload.merge(doc)
  219. end
  220. return
  221. end
  222. output =
  223. case extraction_type
  224. when 'json'
  225. extract_json(doc)
  226. when 'text'
  227. extract_text(doc)
  228. else
  229. extract_xml(doc)
  230. end
  231. num_unique_lengths = interpolated['extract'].keys.map { |name| output[name].length }.uniq
  232. if num_unique_lengths.length != 1
  233. raise "Got an uneven number of matches for #{interpolated['name']}: #{interpolated['extract'].inspect}"
  234. end
  235. old_events = previous_payloads num_unique_lengths.first
  236. num_unique_lengths.first.times do |index|
  237. result = {}
  238. interpolated['extract'].keys.each do |name|
  239. result[name] = output[name][index]
  240. if name.to_s == 'url'
  241. result[name] = (response.env[:url] + result[name]).to_s
  242. end
  243. end
  244. if store_payload!(old_events, result)
  245. log "Storing new parsed result for '#{name}': #{result.inspect}"
  246. create_event payload: payload.merge(result)
  247. end
  248. end
  249. }
  250. rescue => e
  251. error "Error when fetching url: #{e.message}\n#{e.backtrace.join("\n")}"
  252. end
  253. def receive(incoming_events)
  254. incoming_events.each do |event|
  255. interpolate_with(event) do
  256. url_to_scrape =
  257. if url_template = options['url_from_event'].presence
  258. interpolate_options(url_template)
  259. else
  260. event.payload['url']
  261. end
  262. check_urls(url_to_scrape,
  263. interpolated['mode'].to_s == "merge" ? event.payload : {})
  264. end
  265. end
  266. end
  267. private
  268. # This method returns true if the result should be stored as a new event.
  269. # If mode is set to 'on_change', this method may return false and update an existing
  270. # event to expire further in the future.
  271. def store_payload!(old_events, result)
  272. case interpolated['mode'].presence
  273. when 'on_change'
  274. result_json = result.to_json
  275. if found = old_events.find { |event| event.payload.to_json == result_json }
  276. found.update!(expires_at: new_event_expiration_date)
  277. false
  278. else
  279. true
  280. end
  281. when 'all', 'merge', ''
  282. true
  283. else
  284. raise "Illegal options[mode]: #{interpolated['mode']}"
  285. end
  286. end
  287. def previous_payloads(num_events)
  288. if interpolated['uniqueness_look_back'].present?
  289. look_back = interpolated['uniqueness_look_back'].to_i
  290. else
  291. # Larger of UNIQUENESS_FACTOR * num_events and UNIQUENESS_LOOK_BACK
  292. look_back = UNIQUENESS_FACTOR * num_events
  293. if look_back < UNIQUENESS_LOOK_BACK
  294. look_back = UNIQUENESS_LOOK_BACK
  295. end
  296. end
  297. events.order("id desc").limit(look_back) if interpolated['mode'] == "on_change"
  298. end
  299. def extract_full_json?
  300. !interpolated['extract'].present? && extraction_type == "json"
  301. end
  302. def extraction_type(interpolated = interpolated())
  303. (interpolated['type'] || begin
  304. case interpolated['url']
  305. when /\.(rss|xml)$/i
  306. "xml"
  307. when /\.json$/i
  308. "json"
  309. when /\.(txt|text)$/i
  310. "text"
  311. else
  312. "html"
  313. end
  314. end).to_s
  315. end
  316. def use_namespaces?
  317. if value = interpolated.key?('use_namespaces')
  318. boolify(interpolated['use_namespaces'])
  319. else
  320. interpolated['extract'].none? { |name, extraction_details|
  321. extraction_details.key?('xpath')
  322. }
  323. end
  324. end
  325. def extract_each(&block)
  326. interpolated['extract'].each_with_object({}) { |(name, extraction_details), output|
  327. output[name] = block.call(extraction_details)
  328. }
  329. end
  330. def extract_json(doc)
  331. extract_each { |extraction_details|
  332. result = Utils.values_at(doc, extraction_details['path'])
  333. log "Extracting #{extraction_type} at #{extraction_details['path']}: #{result}"
  334. result
  335. }
  336. end
  337. def extract_text(doc)
  338. extract_each { |extraction_details|
  339. regexp = Regexp.new(extraction_details['regexp'])
  340. case index = extraction_details['index']
  341. when /\A\d+\z/
  342. index = index.to_i
  343. end
  344. result = []
  345. doc.scan(regexp) {
  346. result << Regexp.last_match[index]
  347. }
  348. log "Extracting #{extraction_type} at #{regexp}: #{result}"
  349. result
  350. }
  351. end
  352. def extract_xml(doc)
  353. extract_each { |extraction_details|
  354. case
  355. when css = extraction_details['css']
  356. nodes = doc.css(css)
  357. when xpath = extraction_details['xpath']
  358. nodes = doc.xpath(xpath)
  359. else
  360. raise '"css" or "xpath" is required for HTML or XML extraction'
  361. end
  362. case nodes
  363. when Nokogiri::XML::NodeSet
  364. result = nodes.map { |node|
  365. value = node.xpath(extraction_details['value'] || '.')
  366. if extraction_details['strip_cdata']
  367. child = value.first
  368. if child.cdata?
  369. value = child.text
  370. end
  371. end
  372. case value
  373. when Float
  374. # Node#xpath() returns any numeric value as float;
  375. # convert it to integer as appropriate.
  376. value = value.to_i if value.to_i == value
  377. end
  378. value.to_s
  379. }
  380. else
  381. raise "The result of HTML/XML extraction was not a NodeSet"
  382. end
  383. log "Extracting #{extraction_type} at #{xpath || css}: #{result}"
  384. result
  385. }
  386. end
  387. def parse(data)
  388. case type = extraction_type
  389. when "xml"
  390. doc = Nokogiri::XML(data)
  391. # ignore xmlns, useful when parsing atom feeds
  392. doc.remove_namespaces! unless use_namespaces?
  393. doc
  394. when "json"
  395. JSON.parse(data)
  396. when "html"
  397. Nokogiri::HTML(data)
  398. when "text"
  399. data
  400. else
  401. raise "Unknown extraction type: #{type}"
  402. end
  403. end
  404. def is_positive_integer?(value)
  405. Integer(value) >= 0
  406. rescue
  407. false
  408. end
  409. # Wraps Faraday::Response
  410. class ResponseDrop < LiquidDroppable::Drop
  411. def headers
  412. HeaderDrop.new(@object.headers)
  413. end
  414. # Integer value of HTTP status
  415. def status
  416. @object.status
  417. end
  418. end
  419. # Wraps Faraday::Utils::Headers
  420. class HeaderDrop < LiquidDroppable::Drop
  421. def before_method(name)
  422. @object[name.tr('_', '-')]
  423. end
  424. end
  425. end
  426. end