From 748a115852d5242421ab58ebc629165edefde315 Mon Sep 17 00:00:00 2001 From: Carlos A Date: Tue, 2 May 2023 12:17:34 -0500 Subject: [PATCH 1/2] Reto #18 - Ruby --- .../ruby/test0n3.rb" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 "Retos/Reto #18 - WEB SCRAPING [Dif\303\255cil]/ruby/test0n3.rb" diff --git "a/Retos/Reto #18 - WEB SCRAPING [Dif\303\255cil]/ruby/test0n3.rb" "b/Retos/Reto #18 - WEB SCRAPING [Dif\303\255cil]/ruby/test0n3.rb" new file mode 100644 index 0000000000..77453e03f2 --- /dev/null +++ "b/Retos/Reto #18 - WEB SCRAPING [Dif\303\255cil]/ruby/test0n3.rb" @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'uri' +require 'net/http' +require 'nokogiri' + +# class GetSchedule, get on date schedule +class GetSchedule + HOLAMUNDO_URL = 'https://holamundo.day/' + + def pick_data + uri = URI(HOLAMUNDO_URL) + res = Net::HTTP.get_response(uri) + print_schedule(res.body) if res.is_a?(Net::HTTPSuccess) + end + + def print_schedule(response) + doc = Nokogiri::HTML(response) + schedule = find_schedules(doc)[0] + + clean_html_snip(schedule).drop(8).map(&:content) + end + + def find_schedules(html_body) + html_body.css('article.notion-page-content-inner').select do |article| + article.content.downcase.match?(/(agenda 8 de mayo)/) + end + end + + def clean_html_snip(html_snip) + html_snip.children.select { |elem| elem.name == 'h1' || elem.name == 'blockquote' } + end +end + +puts GetSchedule.new.pick_data From 1fa6f02edf300c6c98d0a08eac8ebb7dfaa38977 Mon Sep 17 00:00:00 2001 From: Carlos A Date: Tue, 2 May 2023 17:06:48 -0500 Subject: [PATCH 2/2] Reto #18 - Ruby --- .../ruby/test0n3.rb" | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git "a/Retos/Reto #18 - WEB SCRAPING [Dif\303\255cil]/ruby/test0n3.rb" "b/Retos/Reto #18 - WEB SCRAPING [Dif\303\255cil]/ruby/test0n3.rb" index 77453e03f2..aa13a0dd0f 100644 --- "a/Retos/Reto #18 - WEB SCRAPING [Dif\303\255cil]/ruby/test0n3.rb" +++ "b/Retos/Reto #18 - WEB SCRAPING [Dif\303\255cil]/ruby/test0n3.rb" @@ -1,3 +1,8 @@ +# Ruby app, requires to have installed the following gems: +# - URI +# - net/http +# - nokogiri + # frozen_string_literal: true require 'uri' @@ -5,22 +10,24 @@ require 'nokogiri' # class GetSchedule, get on date schedule -class GetSchedule +class ScheduleHolaMundoDay HOLAMUNDO_URL = 'https://holamundo.day/' def pick_data uri = URI(HOLAMUNDO_URL) res = Net::HTTP.get_response(uri) - print_schedule(res.body) if res.is_a?(Net::HTTPSuccess) + show_schedule(res.body) if res.is_a?(Net::HTTPSuccess) end - def print_schedule(response) + def show_schedule(response) doc = Nokogiri::HTML(response) - schedule = find_schedules(doc)[0] + schedules = find_schedules(doc)[0] - clean_html_snip(schedule).drop(8).map(&:content) + clean_html_snip(schedules).drop(8).map(&:content) end + private + def find_schedules(html_body) html_body.css('article.notion-page-content-inner').select do |article| article.content.downcase.match?(/(agenda 8 de mayo)/) @@ -32,4 +39,4 @@ def clean_html_snip(html_snip) end end -puts GetSchedule.new.pick_data +puts ScheduleHolaMundoDay.new.pick_data