-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
74 lines (60 loc) · 2.17 KB
/
index.js
File metadata and controls
74 lines (60 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env node
const ftp = require("basic-ftp");
const fs = require('fs-extra');
const path = require('path')
let date = require('date-and-time');
const os = require('os');
//OPTIONS
let host = "216.251.155.90";
let user = "shawn";
let password = "wolt777";
let localDir = "Documents/06_current_quote";
let logLocation = "Documents/logs/quoteDownloader/";
//Log Stuff
logLocation = path.join(os.homedir(), logLocation)
fs.ensureDirSync(logLocation)
const logger = require('simple-node-logger').createSimpleLogger(path.join(logLocation, "debug.log"));
log("Program Has Started")
log("Bob is cook")
downloadQuote()
//Functions
async function downloadQuote() {
const client = new ftp.Client()
client.verbose = true;
try {
await client.access( { host, user, password })
let now = new Date();
let dirDate = date.format(now, 'MM MMMM YYYY'); // => '01 January 2019'
await client.cd(`FTP-Share/6-Preservice Quotes/${dirDate}`)
let fileDate = date.format(now, "YY-MMDD") // => '19-0129
let fileList = await client.list() // => [object][object]
let remoteFileName = (await resolveFileName(fileList, fileDate)) // => 19-0116(5-13)Works-Faith 11.mp3
let localFileLocation = path.join(os.homedir(), localDir)
console.log(localFileLocation)
let localFile = path.join(localFileLocation, remoteFileName)
process.chdir(localFileLocation) //Changes the download locaiton
fs.emptyDir(localFileLocation)
log(`Downloading the file ${remoteFileName}...\n`)
log((await client.download(fs.createWriteStream(localFile), remoteFileName)))
} catch (err) {
log(err)
} finally {
client.close()
}
}
// => 226-File successfully transferred
// => 226 13.821 seconds (measured here), 0.54 Mbytes per second
//Takes a list of files as an argument
function resolveFileName(files, fileDate) {
for (let file of files) {
if (file.name.slice(0, 7) == fileDate) {
return file.name
}
}
}
// => 19-0122(5-13)Works-Faith 11.mp3 (SIMILAR)
//Does what is says...
function log(msg) {
logger.info(msg)
}
// => 16:17:07.355 INFO message here