La Cuarta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
require 'net/http' | |
require 'fileutils.rb' | |
fecha = (Time.now - (60*60*24)).strftime("%Y/%m/%d") | |
def remote_file_exists?(url) | |
url = URI.parse(url) | |
Net::HTTP.start(url.host, url.port) do |http| | |
return http.head(url.request_uri).code == "200" | |
end | |
end | |
puts "Creando directorio temporal de descarga" | |
FileUtils.mkdir("/tmp/lacuarta/") | |
count = 1 | |
loop do | |
file = "%03d.pdf" % count.to_s | |
url = "http://papeldigital.info/lacuarta/#{fecha}/01/paginas/" + file | |
if remote_file_exists?(url) == true | |
open("/tmp/lacuarta/" + File.basename(url), "wb") do |file| | |
open(url) do |uri| | |
puts "Descargando #{url}" | |
file.write(uri.read) | |
end | |
end | |
else | |
break | |
end | |
count += 1 | |
end | |
out = "LACUARTA_" + (Time.now - (60*60*24)).strftime("%Y-%m-%d") + ".pdf" | |
puts "Combinando PDF" | |
system("gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=#{out} /tmp/lacuarta/*.pdf") | |
puts "Eliminando archivos" | |
FileUtils.rm(Dir.glob('/tmp/lacuarta/0*.pdf')) | |
puts "Quitando directorio temporal" | |
FileUtils.rm_rf("/tmp/lacuarta/") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment