Created
February 4, 2017 10:04
-
-
Save kaleguy/cb66ddb2211615b553dc44a472aefe69 to your computer and use it in GitHub Desktop.
Download CSV files via FTP
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
Client = require 'ftp' | |
async = require 'async' | |
remote_path ='/public_html/targetfolder/' | |
local_path = 'data/' | |
fs = require 'fs' | |
c = new Client() | |
c.on 'ready', ()-> | |
c.list remote_path, (err, list)-> | |
if (err) then console.error 'list', err | |
file_funcs = (getFileFunc file.name for file in list) | |
async.series file_funcs, ()-> c.end() | |
getFileFunc = (name) -> | |
(callback)-> | |
if /csv$/.test name | |
c.get remote_path + name, (err, stream)-> | |
console.log 'Getting ', name | |
if err then console.log 'get', err | |
stream.once 'close', callback | |
stream.pipe fs.createWriteStream local_path + name | |
else | |
console.log "Skipping", name | |
callback() | |
c.connect( | |
host : 'ftp.targethost.com' | |
user : 'username@domain.com' | |
password : 'password' | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment