Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add verbose mode for progress tracking #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
Downloads
json
node_modules
progress.json
progress.txt
progress*.txt
progress*.json
memories_history.json
21 changes: 14 additions & 7 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ program
.option(
"-f <path>",
"Filepath to memories_history.json",
"./json/memories_history.json"
"./memories_history.json"
)
.option("-o <directory>", "Download directory", "Downloads")
.option("-l", "Preserve location data as file metadata", false);
.option("-l", "Preserve location data as file metadata", false)
.option("-v", "Verbose mode", false);

program.parse();
const options = program.opts();
Expand Down Expand Up @@ -59,7 +60,7 @@ try {
}

var queue = new Queue(maxConcurrentDownloads);
var progress = new Progress(downloads.length, progressBarLength);
var progress = new Progress(downloads.length, progressBarLength, options.v);

function main() {
// Create download directory
Expand All @@ -83,22 +84,24 @@ function main() {
queue
.enqueue(() => getDownloadLink(url, body, fileName, fileTime))
.then((result) => {
progress.cdnLinkSucceeded(true);
progress.cdnLinkSucceeded(true,result[0]);

// Download the file
queue
.enqueue(() =>
downloadMemory(result[0], result[1], result[2], lat, long)
)
.then((success) => {
progress.downloadSucceeded(true);
progress.downloadSucceeded(true,result[0]);
})
.catch((err) => {
progress.downloadSucceeded(false);
progress.downloadSucceeded(false),result[0];
progress.logError(err);
});
})
.catch((err) => {
progress.cdnLinkSucceeded(false);
progress.cdnLinkSucceeded(false,downloads[i]["Download Link"]);
progress.logError("CDN link error: " + downloads[i]["Download Link"]);
});
}
}
Expand Down Expand Up @@ -223,6 +226,9 @@ const downloadMemory = (downloadUrl, fileName, fileTime, lat = "", long = "") =>
});
} else {
console.log("download error", res.statusCode, res.statusMessage);
progress.logError(
`Download error: ${res.statusCode} ${res.statusMessage}`
);
if (maxRetries > 0) {
download(maxRetries - 1);
} else {
Expand All @@ -233,6 +239,7 @@ const downloadMemory = (downloadUrl, fileName, fileTime, lat = "", long = "") =>

req.on("error", function () {
console.log("request error");
progress.logError("Request error" + downloadUrl);
req.destroy();
if (maxRetries > 0) {
download(maxRetries - 1);
Expand Down
71 changes: 68 additions & 3 deletions progress.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,87 @@
import fs from 'fs';

export default class Progress {
constructor(total, progressBarLength) {
constructor(total, progressBarLength, verbose) {
this.total = total;
this.verbose = verbose;
this.progressBarLength = progressBarLength;
this.cdnLinks = { success: 0, fails: 0 };
this.downloads = { success: 0, fails: 0 };
this.printProgress(true);
let progressFile = './progress.json';
let logfile = './progress.txt';
if(verbose) {
let counter = 1;
while (fs.existsSync(progressFile)) {
progressFile = `./progress${counter}.json`;
counter++;
}
fs.writeFileSync(progressFile, JSON.stringify(this, null, 2));
let logCounter = 1;
while (fs.existsSync(logfile)) {
logfile = `./progress${logCounter}.txt`;
logCounter++;
}
}
this.logFile = logfile;
this.processFile = progressFile;
}

cdnLinkSucceeded(didSucceed) {
cdnLinkSucceeded(didSucceed, url) {
if(this.verbose){
if (didSucceed) {
const progressData = JSON.parse(fs.readFileSync(this.processFile));
if (!progressData.cdnLinks.CDNSuccessUrls) {
progressData.cdnLinks.CDNSuccessUrls = [];
}
progressData.cdnLinks.CDNSuccessUrls.push(url);
progressData.cdnLinks.success++;
fs.writeFileSync(this.processFile, JSON.stringify(progressData, null, 2));

} else {
const progressData = JSON.parse(fs.readFileSync(this.processFile));
if (!progressData.cdnLinks.CDNFailedUrls) {
progressData.cdnLinks.CDNFailedUrls = [];
}
progressData.cdnLinks.CDNFailedUrls.push(url);
progressData.cdnLinks.fails++;
fs.writeFileSync(this.processFile, JSON.stringify(progressData, null, 2));
}
}
didSucceed ? this.cdnLinks.success++ : this.cdnLinks.fails++;
this.printProgress();
}

downloadSucceeded(didSucceed) {
downloadSucceeded(didSucceed, url) {
if(this.verbose){
if (didSucceed) {
const progressData = JSON.parse(fs.readFileSync(this.processFile));
if (!progressData.downloads.downloadSuccessUrls) {
progressData.downloads.downloadSuccessUrls = [];
}
progressData.downloads.downloadSuccessUrls.push(url);
progressData.downloads.success++;
fs.writeFileSync(this.processFile, JSON.stringify(progressData, null, 2));
} else {
const progressData = JSON.parse(fs.readFileSync(this.processFile));
if (!progressData.downloads.downloadFailedUrls) {
progressData.downloads.downloadFailedUrls = [];
}
progressData.downloads.downloadFailedUrls.push(url);
progressData.downloads.fails++;
fs.writeFileSync(this.processFile, JSON.stringify(progressData, null, 2));
}
}
didSucceed ? this.downloads.success++ : this.downloads.fails++;
this.printProgress();
}

logError(message) {
const time = new Date().toISOString();
const logMessage = `${time}: ${message}\n`;
fs.appendFileSync(this.logFile, logMessage);
}

printProgress(firstRun) {
if (!firstRun) process.stdout.moveCursor(0, -2);

Expand Down