#!/usr/bin/env ruby require 'net/http' require 'uri' require 'nokogiri' AESAN_HOST = 'https://www.aesan.gob.es' AESAN_NEWS_URI = "#{AESAN_HOST}/AECOSAN/web/seguridad_alimentaria/subseccion/otras_alertas_alimentarias.htm" READ_TITLES_FILE = './aesan_read.txt' def read_title(file_path, line) unless File.exist?(file_path) File.open(file_path, 'w') {} end file_content = File.readlines(file_path).map(&:chomp) if file_content.include?(line) return false else File.open(file_path, 'a') do |file| file.puts(line) end return true end end def fetch_first_paragraph(url) response = nil error = nil (1..3).each do response = Net::HTTP.get_response(URI.parse(url)) break rescue StandardError => error sleep 5 end if response.nil? abort "Error al descargar URL tras 3 intentos: #{error}" end if response.is_a?(Net::HTTPSuccess) document = Nokogiri::HTML(response.body) ps = document.xpath('//section[@class="theContent"]/p/a').filter_map do |p| title = p.text.gsub(/^\s+|\s+$/, '') "* #{title} - #{AESAN_HOST}#{p['href']}" if read_title(READ_TITLES_FILE, title) end if ps.any? if ps.length == 1 puts "Se ha publicado una nueva alerta alimentaria en AESAN:\n" else puts "Se han publicado una o más alertas alimentarias en AESAN:\n" end puts ps end else abort "Error al descargar página: #{response.code} #{response.message}" end end fetch_first_paragraph(AESAN_NEWS_URI)