website_agent.rb 20 KB

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