Last active
December 11, 2015 15:29
-
-
Save mamantoha/4621371 to your computer and use it in GitHub Desktop.
Скрипт для скачування оновлень для програми OPZ (Податкова звітність)
This file contains hidden or 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
# encoding: utf-8 | |
# Скрипт для скачування оновлень для програми OPZ (Податкова звітність) | |
require 'net/ftp' | |
require 'logger' | |
local_path = '/home/ftp/opz' | |
log_file = File.open(local_path + '/' + 'opz.log', File::WRONLY | File::APPEND | File::CREAT) | |
logger = Logger.new(log_file) | |
logger.datetime_format = "%Y-%m-%d %H:%M:%S" | |
logger.info("Starting script") | |
base_url = 'ftp.sta.gov.ua' | |
ftp_dir = 'electornna_zvitnist_install&update' | |
Dir.chdir(local_path) | |
local_files = Dir['*'] | |
local_files = local_files.inject([]) do |memo, file| | |
memo << { name: file, size: File.size(file) } | |
end | |
ftp = Net::FTP.new(base_url) | |
ftp.login | |
ftp.chdir(ftp_dir) | |
ftp_files = ftp.nlst('*') | |
ftp_files = ftp_files.inject([]) do |memo, file| | |
memo << { name: file, size: ftp.size(file), mtime: ftp.mtime(file) } | |
end.sort_by{ |file| file[:mtime] } | |
ftp_files.each do |ftp_file| | |
if (local_file = local_files.detect{ |file| file[:name] == ftp_file[:name] }) && (local_file[:size] == ftp_file[:size]) | |
puts "Skip `#{ftp_file[:name]}`" | |
else | |
print "Downloading `#{ftp_file[:name]}`" | |
ftp.getbinaryfile(ftp_file[:name], Dir.pwd + '/' + ftp_file[:name], 1024) | |
puts "\rDownloading `#{ftp_file[:name]}` - OK" | |
logger.info("Download #{ftp_file[:name]}") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment