1
0

website_agent.rb 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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](https://github.com/huginn/huginn/wiki/Formatting-Events-using-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](https://github.com/huginn/huginn/wiki/Formatting-Events-using-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 except when it has `repeat` set to true. 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. For extractors with `hidden` set to true, they will be excluded from the payloads of events created by the Agent, but can be used and interpolated in the `template` option explained below.
  25. For extractors with `repeat` set to true, their first matches will be included in all extracts. This is useful such as when you want to include the title of a page in all events created from the page.
  26. # Scraping HTML and XML
  27. 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:
  28. "extract": {
  29. "url": { "css": "#comic img", "value": "@src" },
  30. "title": { "css": "#comic img", "value": "@title" },
  31. "body_text": { "css": "div.main", "value": "string(.)" },
  32. "page_title": { "css": "title", "value": "string(.)", "repeat": true }
  33. }
  34. or
  35. "extract": {
  36. "url": { "xpath": "//*[@class="blog-item"]/a/@href", "value": "."
  37. "title": { "xpath": "//*[@class="blog-item"]/a", "value": "normalize-space(.)" },
  38. "description": { "xpath": "//*[@class="blog-item"]/div[0]", "value": "string(.)" }
  39. }
  40. "@_attr_" is the XPath expression to extract the value of an attribute named _attr_ from a node (such as "@href" from a hyperlink), 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 `.`.
  41. You can also use [XPath functions](https://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(., ',', '')`.
  42. 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`.
  43. For extraction with `raw` set to true, each value will be returned as is without any conversion instead of stringifying them. This is useful when you want to extract a number, a boolean value, or an array of strings.
  44. For extraction with `single_array` set to true, all matches will be extracted into an array. This is useful when extracting list elements or multiple parts of a website that can only be matched with the same selector.
  45. # Scraping JSON
  46. When parsing JSON, these sub-hashes specify [JSONPaths](http://goessner.net/articles/JsonPath/) to the values that you care about.
  47. Sample incoming event:
  48. { "results": {
  49. "data": [
  50. {
  51. "title": "Lorem ipsum 1",
  52. "description": "Aliquam pharetra leo ipsum."
  53. "price": 8.95
  54. },
  55. {
  56. "title": "Lorem ipsum 2",
  57. "description": "Suspendisse a pulvinar lacus."
  58. "price": 12.99
  59. },
  60. {
  61. "title": "Lorem ipsum 3",
  62. "description": "Praesent ac arcu tellus."
  63. "price": 8.99
  64. }
  65. ]
  66. }
  67. }
  68. Sample rule:
  69. "extract": {
  70. "title": { "path": "results.data[*].title" },
  71. "description": { "path": "results.data[*].description" }
  72. }
  73. In this example the `*` wildcard character makes the parser to iterate through all items of the `data` array. Three events will be created as a result.
  74. Sample outgoing events:
  75. [
  76. {
  77. "title": "Lorem ipsum 1",
  78. "description": "Aliquam pharetra leo ipsum."
  79. },
  80. {
  81. "title": "Lorem ipsum 2",
  82. "description": "Suspendisse a pulvinar lacus."
  83. },
  84. {
  85. "title": "Lorem ipsum 3",
  86. "description": "Praesent ac arcu tellus."
  87. }
  88. ]
  89. The `extract` option can be skipped for the JSON type, causing the full JSON response to be returned.
  90. # Scraping Text
  91. 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:
  92. "extract": {
  93. "word": { "regexp": "^(.+?): (.+)$", "index": 1 },
  94. "definition": { "regexp": "^(.+?): (.+)$", "index": 2 }
  95. }
  96. Or if you prefer names to numbers for index:
  97. "extract": {
  98. "word": { "regexp": "^(?<word>.+?): (?<definition>.+)$", "index": "word" },
  99. "definition": { "regexp": "^(?<word>.+?): (?<definition>.+)$", "index": "definition" }
  100. }
  101. To extract the whole content as one event:
  102. "extract": {
  103. "content": { "regexp": "\\A(?m:.)*\\z", "index": 0 }
  104. }
  105. 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.
  106. # General Options
  107. Can be configured to use HTTP basic auth by including the `basic_auth` parameter with `"username:password"`, or `["username", "password"]`.
  108. 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.
  109. 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.
  110. Set `force_encoding` to an encoding name (such as `UTF-8` and `ISO-8859-1`) if the website is known to respond with a missing, invalid, or wrong charset in the Content-Type header. Below are the steps used by Huginn to detect the encoding of fetched content:
  111. 1. If `force_encoding` is given, that value is used.
  112. 2. If the Content-Type header contains a charset parameter, that value is used.
  113. 3. When `type` is `html` or `xml`, Huginn checks for the presence of a BOM, XML declaration with attribute "encoding", or an HTML meta tag with charset information, and uses that if found.
  114. 4. Huginn falls back to UTF-8 (not ISO-8859-1).
  115. Set `user_agent` to a custom User-Agent name if the website does not like the default value (`#{default_user_agent}`).
  116. The `headers` field is optional. When present, it should be a hash of headers to send with the request.
  117. Set `disable_ssl_verification` to `true` to disable ssl verification.
  118. Set `unzip` to `gzip` to inflate the resource using gzip.
  119. Set `http_success_codes` to an array of status codes (e.g., `[404, 422]`) to treat HTTP response codes beyond 200 as successes.
  120. If a `template` option is given, its value must be a hash, whose key-value pairs are interpolated after extraction for each iteration and merged with the payload. In the template, keys of extracted data can be interpolated, and some additional variables are also available as explained in the next section. For example:
  121. "template": {
  122. "url": "{{ url | to_uri: _response_.url }}",
  123. "description": "{{ body_text }}",
  124. "last_modified": "{{ _response_.headers.Last-Modified | date: '%FT%T' }}"
  125. }
  126. In the `on_change` mode, change is detected based on the resulted event payload after applying this option. If you want to add some keys to each event but ignore any change in them, set `mode` to `all` and put a DeDuplicationAgent downstream.
  127. # Liquid Templating
  128. In [Liquid](https://github.com/huginn/huginn/wiki/Formatting-Events-using-Liquid) templating, the following variables are available:
  129. * `_url_`: The URL specified to fetch the content from. When parsing `data_from_event`, this is not set.
  130. * `_response_`: A response object with the following keys:
  131. * `status`: HTTP status as integer. (Almost always 200) When parsing `data_from_event`, this is set to the value of the `status` key in the incoming Event, if it is a number or a string convertible to an integer.
  132. * `headers`: Response headers; for example, `{{ _response_.headers.Content-Type }}` expands to the value of the Content-Type header. Keys are insensitive to cases and -/_. When parsing `data_from_event`, this is constructed from the value of the `headers` key in the incoming Event, if it is a hash.
  133. * `url`: The final URL of the fetched page, following redirects. When parsing `data_from_event`, this is set to the value of the `url` key in the incoming Event. Using this in the `template` option, you can resolve relative URLs extracted from a document like `{{ link | to_uri: _response_.url }}` and `{{ content | rebase_hrefs: _response_.url }}`.
  134. # Ordering Events
  135. #{description_events_order}
  136. MD
  137. event_description do
  138. if keys = event_keys
  139. "Events will have the following fields:\n\n %s" % [
  140. Utils.pretty_print(Hash[event_keys.map { |key|
  141. [key, "..."]
  142. }])
  143. ]
  144. else
  145. "Events will be the raw JSON returned by the URL."
  146. end
  147. end
  148. def event_keys
  149. extract = options['extract'] or return nil
  150. extract.each_with_object([]) { |(key, value), keys|
  151. keys << key unless boolify(value['hidden'])
  152. } | (options['template'].presence.try!(:keys) || [])
  153. end
  154. def working?
  155. event_created_within?(options['expected_update_period_in_days']) && !recent_error_logs?
  156. end
  157. def default_options
  158. {
  159. 'expected_update_period_in_days' => "2",
  160. 'url' => "https://xkcd.com",
  161. 'type' => "html",
  162. 'mode' => "on_change",
  163. 'extract' => {
  164. 'url' => { 'css' => "#comic img", 'value' => "@src" },
  165. 'title' => { 'css' => "#comic img", 'value' => "@alt" },
  166. 'hovertext' => { 'css' => "#comic img", 'value' => "@title" }
  167. }
  168. }
  169. end
  170. def validate_options
  171. # Check for required fields
  172. errors.add(:base,
  173. "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?
  174. errors.add(:base,
  175. "expected_update_period_in_days is required") unless options['expected_update_period_in_days'].present?
  176. validate_extract_options!
  177. validate_template_options!
  178. validate_http_success_codes!
  179. # Check for optional fields
  180. if options['mode'].present?
  181. errors.add(:base, "mode must be set to on_change, all or merge") unless %w[on_change all
  182. merge].include?(options['mode'])
  183. end
  184. if options['expected_update_period_in_days'].present?
  185. errors.add(:base,
  186. "Invalid expected_update_period_in_days format") unless is_positive_integer?(options['expected_update_period_in_days'])
  187. end
  188. if options['uniqueness_look_back'].present?
  189. errors.add(:base,
  190. "Invalid uniqueness_look_back format") unless is_positive_integer?(options['uniqueness_look_back'])
  191. end
  192. validate_web_request_options!
  193. end
  194. def validate_http_success_codes!
  195. consider_success = options["http_success_codes"]
  196. if consider_success.present?
  197. if consider_success.class != Array
  198. errors.add(:http_success_codes, "must be an array and specify at least one status code")
  199. elsif consider_success.uniq.count != consider_success.count
  200. errors.add(:http_success_codes, "duplicate http code found")
  201. elsif consider_success.any? { |e| e.to_s !~ /^\d+$/ }
  202. errors.add(:http_success_codes,
  203. "please make sure to use only numeric values for code, ex 404, or \"404\"")
  204. end
  205. end
  206. end
  207. def validate_extract_options!
  208. extraction_type = begin
  209. extraction_type()
  210. rescue StandardError
  211. extraction_type(options)
  212. end
  213. case extract = options['extract']
  214. when Hash
  215. if extract.each_value.any? { |value| !value.is_a?(Hash) }
  216. errors.add(:base, 'extract must be a hash of hashes.')
  217. else
  218. case extraction_type
  219. when 'html', 'xml'
  220. extract.each do |name, details|
  221. details.each do |name,|
  222. case name
  223. when 'css', 'xpath', 'value', 'repeat', 'hidden', 'raw', 'single_array'
  224. # ok
  225. else
  226. errors.add(:base, "Unknown key #{name.inspect} in extraction details")
  227. end
  228. end
  229. case details['css']
  230. when String
  231. # ok
  232. when nil
  233. case details['xpath']
  234. when String
  235. # ok
  236. when nil
  237. errors.add(:base,
  238. "When type is html or xml, all extractions must have a css or xpath attribute (bad extraction details for #{name.inspect})")
  239. else
  240. errors.add(:base, "Wrong type of \"xpath\" value in extraction details for #{name.inspect}")
  241. end
  242. else
  243. errors.add(:base, "Wrong type of \"css\" value in extraction details for #{name.inspect}")
  244. end
  245. case details['value']
  246. when String, nil
  247. # ok
  248. else
  249. errors.add(:base, "Wrong type of \"value\" value in extraction details for #{name.inspect}")
  250. end
  251. end
  252. when 'json'
  253. extract.each do |name, details|
  254. case details['path']
  255. when String
  256. # ok
  257. when nil
  258. errors.add(:base,
  259. "When type is json, all extractions must have a path attribute (bad extraction details for #{name.inspect})")
  260. else
  261. errors.add(:base, "Wrong type of \"path\" value in extraction details for #{name.inspect}")
  262. end
  263. end
  264. when 'text'
  265. extract.each do |name, details|
  266. case regexp = details['regexp']
  267. when String
  268. begin
  269. re = Regexp.new(regexp)
  270. rescue StandardError => e
  271. errors.add(:base, "invalid regexp for #{name.inspect}: #{e.message}")
  272. end
  273. when nil
  274. errors.add(:base,
  275. "When type is text, all extractions must have a regexp attribute (bad extraction details for #{name.inspect})")
  276. else
  277. errors.add(:base, "Wrong type of \"regexp\" value in extraction details for #{name.inspect}")
  278. end
  279. case index = details['index']
  280. when Integer, /\A\d+\z/
  281. # ok
  282. when String
  283. if re && !re.names.include?(index)
  284. errors.add(:base, "no named capture #{index.inspect} found in regexp for #{name.inspect})")
  285. end
  286. when nil
  287. errors.add(:base,
  288. "When type is text, all extractions must have an index attribute (bad extraction details for #{name.inspect})")
  289. else
  290. errors.add(:base, "Wrong type of \"index\" value in extraction details for #{name.inspect}")
  291. end
  292. end
  293. when /\{/
  294. # Liquid templating
  295. else
  296. errors.add(:base, "Unknown extraction type #{extraction_type.inspect}")
  297. end
  298. end
  299. when nil
  300. unless extraction_type == 'json'
  301. errors.add(:base, 'extract is required for all types except json')
  302. end
  303. else
  304. errors.add(:base, 'extract must be a hash')
  305. end
  306. end
  307. def validate_template_options!
  308. template = options['template'].presence or return
  309. unless Hash === template && template.each_key.all?(String)
  310. errors.add(:base, 'template must be a hash of strings.')
  311. end
  312. end
  313. def check
  314. check_urls(interpolated['url'])
  315. end
  316. def check_urls(in_url, existing_payload = {})
  317. return unless in_url.present?
  318. Array(in_url).each do |url|
  319. check_url(url, existing_payload)
  320. end
  321. end
  322. def check_url(url, existing_payload = {})
  323. unless /\Ahttps?:\/\//i === url
  324. error "Ignoring a non-HTTP url: #{url.inspect}"
  325. return
  326. end
  327. uri = Utils.normalize_uri(url)
  328. log "Fetching #{uri}"
  329. response = faraday.get(uri)
  330. raise "Failed: #{response.inspect}" unless consider_response_successful?(response)
  331. interpolation_context.stack {
  332. interpolation_context['_url_'] = uri.to_s
  333. interpolation_context['_response_'] = ResponseDrop.new(response)
  334. handle_data(response.body, response.env[:url], existing_payload)
  335. }
  336. rescue StandardError => e
  337. error "Error when fetching url: #{e.message}\n#{e.backtrace.join("\n")}"
  338. end
  339. def default_encoding
  340. case extraction_type
  341. when 'html', 'xml'
  342. # Let Nokogiri detect the encoding
  343. nil
  344. else
  345. super
  346. end
  347. end
  348. def handle_data(body, url, existing_payload)
  349. # Beware, url may be a URI object, string or nil
  350. doc = parse(body)
  351. if extract_full_json?
  352. if store_payload!(previous_payloads(1), doc)
  353. log "Storing new result for '#{name}': #{doc.inspect}"
  354. create_event payload: existing_payload.merge(doc)
  355. end
  356. return
  357. end
  358. output =
  359. case extraction_type
  360. when 'json'
  361. extract_json(doc)
  362. when 'text'
  363. extract_text(doc)
  364. else
  365. extract_xml(doc)
  366. end
  367. num_tuples = output.size or
  368. raise "At least one non-repeat key is required"
  369. old_events = previous_payloads num_tuples
  370. template = options['template'].presence
  371. output.each do |extracted|
  372. result = extracted.except(*output.hidden_keys)
  373. if template
  374. result.update(interpolate_options(template, extracted))
  375. end
  376. if store_payload!(old_events, result)
  377. log "Storing new parsed result for '#{name}': #{result.inspect}"
  378. create_event payload: existing_payload.merge(result)
  379. end
  380. end
  381. end
  382. def receive(incoming_events)
  383. interpolate_with_each(incoming_events) do |event|
  384. existing_payload = interpolated['mode'].to_s == "merge" ? event.payload : {}
  385. if data_from_event = options['data_from_event'].presence
  386. data = interpolate_options(data_from_event)
  387. if data.present?
  388. handle_event_data(data, event, existing_payload)
  389. else
  390. error "No data was found in the Event payload using the template #{data_from_event}", inbound_event: event
  391. end
  392. else
  393. url_to_scrape =
  394. if url_template = options['url_from_event'].presence
  395. interpolate_options(url_template)
  396. else
  397. interpolated['url']
  398. end
  399. check_urls(url_to_scrape, existing_payload)
  400. end
  401. end
  402. end
  403. private
  404. def consider_response_successful?(response)
  405. response.success? || begin
  406. consider_success = options["http_success_codes"]
  407. consider_success.present? && (consider_success.include?(response.status.to_s) || consider_success.include?(response.status))
  408. end
  409. end
  410. def handle_event_data(data, event, existing_payload)
  411. interpolation_context.stack {
  412. interpolation_context['_response_'] = ResponseFromEventDrop.new(event)
  413. handle_data(data, event.payload['url'].presence, existing_payload)
  414. }
  415. rescue StandardError => e
  416. error "Error when handling event data: #{e.message}\n#{e.backtrace.join("\n")}"
  417. end
  418. # This method returns true if the result should be stored as a new event.
  419. # If mode is set to 'on_change', this method may return false and update an existing
  420. # event to expire further in the future.
  421. def store_payload!(old_events, result)
  422. case interpolated['mode'].presence
  423. when 'on_change'
  424. result_json = result.to_json
  425. if found = old_events.find { |event| event.payload.to_json == result_json }
  426. found.update!(expires_at: new_event_expiration_date)
  427. false
  428. else
  429. true
  430. end
  431. when 'all', 'merge', ''
  432. true
  433. else
  434. raise "Illegal options[mode]: #{interpolated['mode']}"
  435. end
  436. end
  437. def previous_payloads(num_events)
  438. if interpolated['uniqueness_look_back'].present?
  439. look_back = interpolated['uniqueness_look_back'].to_i
  440. else
  441. # Larger of UNIQUENESS_FACTOR * num_events and UNIQUENESS_LOOK_BACK
  442. look_back = UNIQUENESS_FACTOR * num_events
  443. if look_back < UNIQUENESS_LOOK_BACK
  444. look_back = UNIQUENESS_LOOK_BACK
  445. end
  446. end
  447. events.order("id desc").limit(look_back) if interpolated['mode'] == "on_change"
  448. end
  449. def extract_full_json?
  450. !interpolated['extract'].present? && extraction_type == "json"
  451. end
  452. def extraction_type(interpolated = interpolated())
  453. (interpolated['type'] || begin
  454. case interpolated['url']
  455. when /\.(rss|xml)$/i
  456. "xml"
  457. when /\.json$/i
  458. "json"
  459. when /\.(txt|text)$/i
  460. "text"
  461. else
  462. "html"
  463. end
  464. end).to_s
  465. end
  466. def use_namespaces?
  467. if interpolated.key?('use_namespaces')
  468. boolify(interpolated['use_namespaces'])
  469. else
  470. interpolated['extract'].none? { |_name, extraction_details|
  471. extraction_details.key?('xpath')
  472. }
  473. end
  474. end
  475. def extract_each(&block)
  476. interpolated['extract'].each_with_object(Output.new) { |(name, extraction_details), output|
  477. if boolify(extraction_details['repeat'])
  478. values = Repeater.new { |repeater|
  479. block.call(extraction_details, repeater)
  480. }
  481. else
  482. values = []
  483. block.call(extraction_details, values)
  484. end
  485. log "Values extracted: #{values}"
  486. begin
  487. output[name] = values
  488. rescue UnevenSizeError
  489. raise "Got an uneven number of matches for #{interpolated['name']}: #{interpolated['extract'].inspect}"
  490. else
  491. output.hidden_keys << name if boolify(extraction_details['hidden'])
  492. end
  493. }
  494. end
  495. def extract_json(doc)
  496. extract_each { |extraction_details, values|
  497. log "Extracting #{extraction_type} at #{extraction_details['path']}"
  498. Utils.values_at(doc, extraction_details['path']).each { |value|
  499. values << value
  500. }
  501. }
  502. end
  503. def extract_text(doc)
  504. extract_each { |extraction_details, values|
  505. regexp = Regexp.new(extraction_details['regexp'])
  506. log "Extracting #{extraction_type} with #{regexp}"
  507. case index = extraction_details['index']
  508. when /\A\d+\z/
  509. index = index.to_i
  510. end
  511. doc.scan(regexp) {
  512. values << Regexp.last_match[index]
  513. }
  514. }
  515. end
  516. def extract_xml(doc)
  517. extract_each { |extraction_details, values|
  518. case
  519. when css = extraction_details['css']
  520. nodes = doc.css(css)
  521. when xpath = extraction_details['xpath']
  522. nodes = doc.xpath(xpath)
  523. else
  524. raise '"css" or "xpath" is required for HTML or XML extraction'
  525. end
  526. log "Extracting #{extraction_type} at #{xpath || css}"
  527. expr = extraction_details['value'] || '.'
  528. handle_float = ->(value) {
  529. case
  530. when value.nan?
  531. 'NaN'
  532. when value.infinite?
  533. if value > 0
  534. 'Infinity'
  535. else
  536. '-Infinity'
  537. end
  538. when value.to_i == value
  539. # Node#xpath() returns any numeric value as float;
  540. # convert it to integer as appropriate.
  541. value.to_i
  542. else
  543. value
  544. end
  545. }
  546. jsonify =
  547. if boolify(extraction_details['raw'])
  548. ->(value) {
  549. case value
  550. when nil, true, false, String, Integer
  551. value
  552. when Float
  553. handle_float.call(value)
  554. when Nokogiri::XML::NodeSet
  555. value.map(&jsonify)
  556. else
  557. value.to_s
  558. end
  559. }
  560. else
  561. ->(value) {
  562. case value
  563. when Float
  564. handle_float.call(value).to_s
  565. else
  566. value.to_s
  567. end
  568. }
  569. end
  570. case nodes
  571. when Nokogiri::XML::NodeSet
  572. node_values = nodes.map { |node|
  573. jsonify.call(node.xpath(expr))
  574. }
  575. if boolify(extraction_details['single_array'])
  576. values << node_values
  577. else
  578. node_values.each { |value| values << value }
  579. end
  580. else
  581. raise "The result of HTML/XML extraction was not a NodeSet"
  582. end
  583. }
  584. end
  585. def parse(data)
  586. case type = extraction_type
  587. when "xml"
  588. doc = Nokogiri::XML(data)
  589. # ignore xmlns, useful when parsing atom feeds
  590. doc.remove_namespaces! unless use_namespaces?
  591. doc
  592. when "json"
  593. JSON.parse(data)
  594. when "html"
  595. Nokogiri::HTML(data)
  596. when "text"
  597. data
  598. else
  599. raise "Unknown extraction type: #{type}"
  600. end
  601. end
  602. class UnevenSizeError < ArgumentError
  603. end
  604. class Output
  605. def initialize
  606. @hash = {}
  607. @size = nil
  608. @hidden_keys = []
  609. end
  610. attr_reader :size
  611. attr_reader :hidden_keys
  612. def []=(key, value)
  613. case size = value.size
  614. when Integer
  615. if @size && @size != size
  616. raise UnevenSizeError, 'got an uneven size'
  617. end
  618. @size = size
  619. end
  620. @hash[key] = value
  621. end
  622. def each
  623. @size.times.zip(*@hash.values) do |_index, *values|
  624. yield @hash.each_key.lazy.zip(values).to_h
  625. end
  626. end
  627. end
  628. class Repeater < Enumerator
  629. # Repeater.new { |y|
  630. # # ...
  631. # y << value
  632. # } #=> [value, ...]
  633. def initialize(&block)
  634. @value = nil
  635. super(Float::INFINITY) { |y|
  636. loop { y << @value }
  637. }
  638. catch(@done = Object.new) {
  639. block.call(self)
  640. }
  641. end
  642. def <<(value)
  643. @value = value
  644. throw @done
  645. end
  646. def to_s
  647. "[#{@value.inspect}, ...]"
  648. end
  649. end
  650. # Wraps Faraday::Response
  651. class ResponseDrop < LiquidDroppable::Drop
  652. def headers
  653. HeaderDrop.new(@object.headers)
  654. end
  655. # Integer value of HTTP status
  656. def status
  657. @object.status
  658. end
  659. # The URL
  660. def url
  661. @object.env.url.to_s
  662. end
  663. end
  664. class ResponseFromEventDrop < LiquidDroppable::Drop
  665. def headers
  666. headers = begin
  667. Faraday::Utils::Headers.from(@object.payload[:headers])
  668. rescue StandardError
  669. {}
  670. end
  671. HeaderDrop.new(headers)
  672. end
  673. # Integer value of HTTP status
  674. def status
  675. Integer(@object.payload[:status])
  676. rescue StandardError
  677. nil
  678. end
  679. # The URL
  680. def url
  681. @object.payload[:url]
  682. end
  683. end
  684. # Wraps Faraday::Utils::Headers
  685. class HeaderDrop < LiquidDroppable::Drop
  686. def liquid_method_missing(name)
  687. @object[name.tr('_', '-')]
  688. end
  689. end
  690. end
  691. end