diff options
author | Guillermo Ramos | 2024-06-02 20:23:23 +0200 |
---|---|---|
committer | Guillermo Ramos | 2024-06-02 20:23:23 +0200 |
commit | 9035dd0feeee4815d1459cab399d021c9c500614 (patch) | |
tree | dff92b68c8a98abc8ca95553a0840c4b7e34d5c0 | |
parent | 932c7c3aeba300f7998473e081f8b139d347de7c (diff) | |
download | bots-9035dd0feeee4815d1459cab399d021c9c500614.tar.gz |
+aesan_alerts
-rwxr-xr-x | aesan_alerts.rb | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/aesan_alerts.rb b/aesan_alerts.rb new file mode 100755 index 0000000..b909ee7 --- /dev/null +++ b/aesan_alerts.rb @@ -0,0 +1,54 @@ +#!/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 = Net::HTTP.get_response(URI.parse(url)) + + 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 + else + warn "No se encontró ningún resultado" + end + else + warn "Error al acceder a la página: #{response.code} #{response.message}" + end +end + +fetch_first_paragraph(AESAN_NEWS_URI) |