Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reto #18 - Ruby #3321

Merged
merged 2 commits into from
May 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Retos/Reto #18 - WEB SCRAPING [Difícil]/ruby/test0n3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Ruby app, requires to have installed the following gems:
# - URI
# - net/http
# - nokogiri

# frozen_string_literal: true

require 'uri'
require 'net/http'
require 'nokogiri'

# class GetSchedule, get on date schedule
class ScheduleHolaMundoDay
HOLAMUNDO_URL = 'https://holamundo.day/'

def pick_data
uri = URI(HOLAMUNDO_URL)
res = Net::HTTP.get_response(uri)
show_schedule(res.body) if res.is_a?(Net::HTTPSuccess)
end

def show_schedule(response)
doc = Nokogiri::HTML(response)
schedules = find_schedules(doc)[0]

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)/)
end
end

def clean_html_snip(html_snip)
html_snip.children.select { |elem| elem.name == 'h1' || elem.name == 'blockquote' }
end
end

puts ScheduleHolaMundoDay.new.pick_data