website_agent.rb 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. require 'nokogiri'
  2. require 'faraday'
  3. require 'faraday_middleware'
  4. require 'date'
  5. module Agents
  6. class WebsiteAgent < Agent
  7. include WebRequestConcern
  8. default_schedule "every_12h"
  9. UNIQUENESS_LOOK_BACK = 200
  10. UNIQUENESS_FACTOR = 3
  11. description <<-MD
  12. The WebsiteAgent 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 `type` value can be `xml`, `html`, `json`, or `text`.
  16. To tell the Agent how to parse the content, specify `extract` as a hash with keys naming the extractions and values of hashes.
  17. 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` on each node in the node set, converting the result into string. Here's an example:
  18. "extract": {
  19. "url": { "css": "#comic img", "value": "@src" },
  20. "title": { "css": "#comic img", "value": "@title" },
  21. "body_text": { "css": "div.main", "value": ".//text()" }
  22. }
  23. "@_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. 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-text(.//text())` should actually be `normalize-text(.)`.
  24. When parsing JSON, these sub-hashes specify [JSONPaths](http://goessner.net/articles/JsonPath/) to the values that you care about. For example:
  25. "extract": {
  26. "title": { "path": "results.data[*].title" },
  27. "description": { "path": "results.data[*].description" }
  28. }
  29. 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 `(?<_name_>...)`. For example, to parse lines of `_word_: _definition_`, the following should work:
  30. "extract": {
  31. "word": { "regexp": "^(.+?): (.+)$", index: 1 },
  32. "definition": { "regexp": "^(.+?): (.+)$", index: 2 },
  33. }
  34. Or if you prefer names to numbers for index:
  35. "extract": {
  36. "word": { "regexp": "^(?<word>.+?): (?<definition>.+)$", index: 'word' },
  37. "definition": { "regexp": "^(?<word>.+?): (?<definition>.+)$", index: 'definition' },
  38. }
  39. To extract the whole content as one event:
  40. "extract": {
  41. "content": { "regexp": "\A(?:.|\n)*\z", index: 0 },
  42. }
  43. 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.
  44. Can be configured to use HTTP basic auth by including the `basic_auth` parameter with `"username:password"`, or `["username", "password"]`.
  45. 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.
  46. 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.
  47. Set `force_encoding` to an encoding name if the website does not return a Content-Type header with a proper charset.
  48. Set `user_agent` to a custom User-Agent name if the website does not like the default value (`#{default_user_agent}`).
  49. The `headers` field is optional. When present, it should be a hash of headers to send with the request.
  50. The WebsiteAgent can also scrape based on incoming events. It will scrape the url contained in the `url` key of the incoming event payload.
  51. MD
  52. event_description do
  53. "Events will have the fields you specified. Your options look like:\n\n #{Utils.pretty_print interpolated['extract']}"
  54. end
  55. def working?
  56. event_created_within?(interpolated['expected_update_period_in_days']) && !recent_error_logs?
  57. end
  58. def default_options
  59. {
  60. 'expected_update_period_in_days' => "2",
  61. 'url' => "http://xkcd.com",
  62. 'type' => "html",
  63. 'mode' => "on_change",
  64. 'extract' => {
  65. 'url' => { 'css' => "#comic img", 'value' => "@src" },
  66. 'title' => { 'css' => "#comic img", 'value' => "@alt" },
  67. 'hovertext' => { 'css' => "#comic img", 'value' => "@title" }
  68. }
  69. }
  70. end
  71. def validate_options
  72. # Check for required fields
  73. errors.add(:base, "url and expected_update_period_in_days are required") unless options['expected_update_period_in_days'].present? && options['url'].present?
  74. if !options['extract'].present? && extraction_type != "json"
  75. errors.add(:base, "extract is required for all types except json")
  76. end
  77. # Check for optional fields
  78. if options['mode'].present?
  79. errors.add(:base, "mode must be set to on_change or all") unless %w[on_change all].include?(options['mode'])
  80. end
  81. if options['expected_update_period_in_days'].present?
  82. errors.add(:base, "Invalid expected_update_period_in_days format") unless is_positive_integer?(options['expected_update_period_in_days'])
  83. end
  84. if options['uniqueness_look_back'].present?
  85. errors.add(:base, "Invalid uniqueness_look_back format") unless is_positive_integer?(options['uniqueness_look_back'])
  86. end
  87. if (encoding = options['force_encoding']).present?
  88. case encoding
  89. when String
  90. begin
  91. Encoding.find(encoding)
  92. rescue ArgumentError
  93. errors.add(:base, "Unknown encoding: #{encoding.inspect}")
  94. end
  95. else
  96. errors.add(:base, "force_encoding must be a string")
  97. end
  98. end
  99. validate_web_request_options!
  100. end
  101. def check
  102. check_url interpolated['url']
  103. end
  104. def check_url(in_url)
  105. return unless in_url.present?
  106. Array(in_url).each do |url|
  107. log "Fetching #{url}"
  108. response = faraday.get(url)
  109. if response.success?
  110. body = response.body
  111. if (encoding = interpolated['force_encoding']).present?
  112. body = body.encode(Encoding::UTF_8, encoding)
  113. end
  114. doc = parse(body)
  115. if extract_full_json?
  116. if store_payload!(previous_payloads(1), doc)
  117. log "Storing new result for '#{name}': #{doc.inspect}"
  118. create_event :payload => doc
  119. end
  120. else
  121. output = {}
  122. interpolated['extract'].each do |name, extraction_details|
  123. case extraction_type
  124. when "text"
  125. regexp = Regexp.new(extraction_details['regexp'])
  126. result = []
  127. doc.scan(regexp) {
  128. result << Regexp.last_match[extraction_details['index']]
  129. }
  130. log "Extracting #{extraction_type} at #{regexp}: #{result}"
  131. when "json"
  132. result = Utils.values_at(doc, extraction_details['path'])
  133. log "Extracting #{extraction_type} at #{extraction_details['path']}: #{result}"
  134. else
  135. case
  136. when css = extraction_details['css']
  137. nodes = doc.css(css)
  138. when xpath = extraction_details['xpath']
  139. doc.remove_namespaces! # ignore xmlns, useful when parsing atom feeds
  140. nodes = doc.xpath(xpath)
  141. else
  142. error '"css" or "xpath" is required for HTML or XML extraction'
  143. return
  144. end
  145. case nodes
  146. when Nokogiri::XML::NodeSet
  147. result = nodes.map { |node|
  148. case value = node.xpath(extraction_details['value'])
  149. when Float
  150. # Node#xpath() returns any numeric value as float;
  151. # convert it to integer as appropriate.
  152. value = value.to_i if value.to_i == value
  153. end
  154. value.to_s
  155. }
  156. else
  157. error "The result of HTML/XML extraction was not a NodeSet"
  158. return
  159. end
  160. log "Extracting #{extraction_type} at #{xpath || css}: #{result}"
  161. end
  162. output[name] = result
  163. end
  164. num_unique_lengths = interpolated['extract'].keys.map { |name| output[name].length }.uniq
  165. if num_unique_lengths.length != 1
  166. error "Got an uneven number of matches for #{interpolated['name']}: #{interpolated['extract'].inspect}"
  167. return
  168. end
  169. old_events = previous_payloads num_unique_lengths.first
  170. num_unique_lengths.first.times do |index|
  171. result = {}
  172. interpolated['extract'].keys.each do |name|
  173. result[name] = output[name][index]
  174. if name.to_s == 'url'
  175. result[name] = (response.env[:url] + result[name]).to_s
  176. end
  177. end
  178. if store_payload!(old_events, result)
  179. log "Storing new parsed result for '#{name}': #{result.inspect}"
  180. create_event :payload => result
  181. end
  182. end
  183. end
  184. else
  185. error "Failed: #{response.inspect}"
  186. end
  187. end
  188. end
  189. def receive(incoming_events)
  190. incoming_events.each do |event|
  191. url_to_scrape = event.payload['url']
  192. check_url(url_to_scrape) if url_to_scrape =~ /^https?:\/\//i
  193. end
  194. end
  195. private
  196. # This method returns true if the result should be stored as a new event.
  197. # If mode is set to 'on_change', this method may return false and update an existing
  198. # event to expire further in the future.
  199. def store_payload!(old_events, result)
  200. if !interpolated['mode'].present?
  201. return true
  202. elsif interpolated['mode'].to_s == "all"
  203. return true
  204. elsif interpolated['mode'].to_s == "on_change"
  205. result_json = result.to_json
  206. old_events.each do |old_event|
  207. if old_event.payload.to_json == result_json
  208. old_event.expires_at = new_event_expiration_date
  209. old_event.save!
  210. return false
  211. end
  212. end
  213. return true
  214. end
  215. raise "Illegal options[mode]: " + interpolated['mode'].to_s
  216. end
  217. def previous_payloads(num_events)
  218. if interpolated['uniqueness_look_back'].present?
  219. look_back = interpolated['uniqueness_look_back'].to_i
  220. else
  221. # Larger of UNIQUENESS_FACTOR * num_events and UNIQUENESS_LOOK_BACK
  222. look_back = UNIQUENESS_FACTOR * num_events
  223. if look_back < UNIQUENESS_LOOK_BACK
  224. look_back = UNIQUENESS_LOOK_BACK
  225. end
  226. end
  227. events.order("id desc").limit(look_back) if interpolated['mode'].present? && interpolated['mode'].to_s == "on_change"
  228. end
  229. def extract_full_json?
  230. !interpolated['extract'].present? && extraction_type == "json"
  231. end
  232. def extraction_type
  233. (interpolated['type'] || begin
  234. case interpolated['url']
  235. when /\.(rss|xml)$/i
  236. "xml"
  237. when /\.json$/i
  238. "json"
  239. when /\.(txt|text)$/i
  240. "text"
  241. else
  242. "html"
  243. end
  244. end).to_s
  245. end
  246. def parse(data)
  247. case extraction_type
  248. when "xml"
  249. Nokogiri::XML(data)
  250. when "json"
  251. JSON.parse(data)
  252. when "html"
  253. Nokogiri::HTML(data)
  254. when "text"
  255. data
  256. else
  257. raise "Unknown extraction type #{extraction_type}"
  258. end
  259. end
  260. def is_positive_integer?(value)
  261. begin
  262. Integer(value) >= 0
  263. rescue
  264. false
  265. end
  266. end
  267. end
  268. end