1
0

website_agent.rb 21 KB

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