Skip to content

Instantly share code, notes, and snippets.

@mwlang
Created April 8, 2015 16:08
Show Gist options
  • Save mwlang/0dde34baab8bae7e31ae to your computer and use it in GitHub Desktop.
Save mwlang/0dde34baab8bae7e31ae to your computer and use it in GitHub Desktop.
Downloads FTP files with progress indicator...
require 'yaml'
require 'net/ftp'
require 'net/ftp/list'
require 'fileutils'
require 'ruby-progressbar'
require 'zip'
require 'open3'
def download_with_progress(ftp, file)
if not already_downloaded?(file)
file_chunk = 512 * 1024
pbar = ProgressBar.create(
title: "downloading #{file.basename}",
total: file.filesize + file_chunk,
format: "%t |%B| (%p%% of #{file.filesize.to_human}) %e"
)
ftp.getbinaryfile(file.basename, File.join(downloaded_path, file.basename), file_chunk) { pbar.progress += file_chunk }
pbar.finish
log "Downloaded #{file.basename}"
end
end
desc "downloads any new *.zip files from ftp.bowker.com server"
task :download => [:environment] do
log "Downloading new files..."
ftp = Net::FTP.new
ftp.connect config["bowker"]["server"]
ftp.login config["bowker"]["userid"], config["bowker"]["password"]
ftp.passive = true
ftp.chdir config["bowker"]["path"] if config["bowker"]["path"]
files = ftp.list('Poly*').map{|fn| Net::FTP::List.parse(fn)}.select{|s| s.file?}
files.each{ |file| download_with_progress(ftp, file) }
ftp.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment