Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'net/ftp'
- require 'active_support/time'
- #settings
- delay = 30 #in minutes
- ftp_delay = 1 #in minutes
- ftp_server_address = 'ftp.example.com'
- ftp_user = 'example_user'
- ftp_password = 'example_password'
- ftp_directory = 'bk1'
- save_as = 'bk1_most_recent.jpg'
- ftp_passive = true
- def grab_latest_image(ftp_server_address, ftp_user, ftp_password,
- ftp_passive, save_as, ftp_directory)
- ftp = Net::FTP.new(ftp_server_address, ftp_user, ftp_password)
- ftp.passive = ftp_passive
- files = ftp.chdir(ftp_directory)
- files = ftp.list('*.jpg')
- filename_most_recent = files.last.match /([0-9._-]*.jpg)/
- minutes_for_calc = filename_most_recent.to_s.match /_[0-9]{2}_([0-9]{2})/
- ftp.getbinaryfile(filename_most_recent, save_as)
- ftp.close
- return minutes_for_calc[1]
- end
- def calculate_delay(minutes_for_calc, ftp_delay, delay)
- current_time = Time.now.min
- wait_minutes = (minutes_for_calc.to_i + delay.to_i) - current_time.to_i
- if wait_minutes < 0
- wait_minutes *= -1
- end
- wait_minutes += ftp_delay
- return wait_minutes.minutes
- end
- loop do
- minutes_for_calc = grab_latest_image(ftp_server_address, ftp_user,
- ftp_password, ftp_passive, save_as, ftp_directory)
- calculated_delay = calculate_delay(minutes_for_calc, ftp_delay, delay)
- sleep calculated_delay
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement