Skip to content

Commit

Permalink
Merge pull request #47 from dills122/pretty-output
Browse files Browse the repository at this point in the history
added logger class
  • Loading branch information
dills122 authored Jun 16, 2019
2 parents 944a264 + 4398c0d commit b9033a6
Show file tree
Hide file tree
Showing 18 changed files with 188 additions and 73 deletions.
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
type: 'boolean',
alias: 'f',
default: false
},
pretty: {
type: 'boolean',
alias: 'p',
default: true
}
}
});
Expand All @@ -46,7 +51,8 @@
let fileOutputEnabled = !!flags.f || flags.file;
let processor = Processor.create({
filePath: filePath,
queryingEnabled: !!flags.q || flags.query
queryingEnabled: !!flags.q || flags.query,
isPretty: !!flags.p || flags.pretty
});
processor.execute((err) => {
if (err) console.log(err);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"dependencies": {
"@hapi/joi": "^15.0.3",
"app-root-path": "^2.2.1",
"bunyan": "^1.8.12",
"fuzzyset.js": "0.0.8",
"image-hash": "^3.3.7",
"image-size": "^0.7.3",
Expand Down
8 changes: 5 additions & 3 deletions src/export-results/process-hashes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
const {
promisify
} = require('util');
const logger = require('../logger/log');

const HashImage = promisify(Hash.HashImage);
const GetHashes = promisify(CardHashes.GetHashes);
Expand All @@ -32,14 +33,15 @@ function ProcessHashes(params) {
error: 'malformed parameters'
};
}
this.logger = params.logger;
this.localCardPath = params.localCardPath;
this.cards = params.cards;
this.name = params.name;
this.queryingEnabled = params.queryingEnabled;
}

ProcessHashes.prototype.compareDbHashes = async function () {
console.log(`process-hashes::compareDbHashes: Compare DB Hashes`);
this.logger.info(`process-hashes::compareDbHashes: Compare DB Hashes`);
try {
let localCardHash = await HashImage(this.localCardPath);
let hashes = await GetHashes(this.name);
Expand All @@ -56,7 +58,7 @@ ProcessHashes.prototype.compareDbHashes = async function () {
}
});
if (matches.length === 0) {
console.log(`process-hashes::compareDbHashes: No DB Hash Match Found ${this.name}`);
this.logger.info(`process-hashes::compareDbHashes: No DB Hash Match Found ${this.name}`);
return {
error: 'No Matches Found'
};
Expand Down Expand Up @@ -106,7 +108,7 @@ ProcessHashes.prototype.compareRemoteImages = async function () {
});
return bestMatches;
} catch (error) {
console.log(error);
this.logger.error(error);
}
}

Expand Down
35 changes: 18 additions & 17 deletions src/export-results/process-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function ProcessResults(params) {
error: 'malformed parameters'
};
}
this.logger = params.logger;
this.name = params.name;
this.filePath = params.filePath;
this.queryingEnabled = params.queryingEnabled;
Expand All @@ -37,9 +38,9 @@ ProcessResults.prototype.execute = async function (filePath) {
error: 'No results found'
};
}
console.log('process-results::execute: Cards Found, Finding Best Match');
this.logger.info('process-results::execute: Cards Found, Finding Best Match');
if (cards.length === 1) {
console.log(`process-results::execute: One Card Found ${JSON.stringify(cards)}`)
this.logger.info(`process-results::execute: One Card Found ${JSON.stringify(cards)}`)
await this._gatherResults(cards[0]);
return {};
}
Expand All @@ -48,19 +49,19 @@ ProcessResults.prototype.execute = async function (filePath) {
name: this.name,
localCardPath: filePath,
cards: cards,
queryingEnabled: this.queryingEnabled
queryingEnabled: this.queryingEnabled,
logger: this.logger
});
let dbResults = await processHashes.compareDbHashes();
if (dbResults.value) {
let card = _.find(cards, {
set_name: dbResults.value.setName
});
console.log(`process-results::execute::DBMatches One Card Found ${JSON.stringify(card)}`)
this.logger.info(`process-results::execute::DBMatches One Card Found`, card);
await this._gatherResults(card);
return {};
}
let result = await this._compareImageHashResults(cards, filePath);
console.log(result);
if (result.error) {
return {
error: result.error
Expand All @@ -70,12 +71,12 @@ ProcessResults.prototype.execute = async function (filePath) {
let card = _.find(cards, {
set_name: result.value.setName
});
console.log(`process-results::execute::BestMatches One Card Found ${JSON.stringify(card)}`)
this.logger.info(`process-results::execute::BestMatches One Card Found`, card);
await this._gatherResults(card);
return {};
}
if (result.sets) {
console.log(`process-results::execute: More Than One Match Found ${JSON.stringify(result.sets)}`)
this.logger.info(`process-results::execute: More Than One Match Found ${JSON.stringify(result.sets)}`);
return {
sets: result.sets
};
Expand All @@ -85,7 +86,7 @@ ProcessResults.prototype.execute = async function (filePath) {
error: 'Couldn\'t find any cards'
};
} catch (error) {
console.log(error);
this.logger.error(error);
}
};
//Need to convert image hash comparison to use the art and flavor image areas for comparison
Expand All @@ -106,35 +107,35 @@ ProcessResults.prototype._compareImageHashResults = async function (results, fil
match.stringCompare >= 1;
});
if (exactMatches > 1) {
console.log(`process-results::_compareImageHashResults: More Than One Exact Match Found ${JSON.stringify(exactMatches)}`)
this.logger.info(`process-results::_compareImageHashResults: More Than One Exact Match Found ${JSON.stringify(exactMatches)}`)
return {
sets: _.map(exactMatches, (match) => match.setName)
}
}
if (exactMatches === 1) {
console.log(`process-results::_compareImageHashResults: Exact Match Found ${JSON.stringify(exactMatches)}`);
this.logger.info(`process-results::_compareImageHashResults: Exact Match Found ${JSON.stringify(exactMatches)}`);
return {
value: exactMatches[0]
}
}
console.log(`process-results::_compareImageHashResults: No Exact Match Found ${JSON.stringify(exactMatches)}`);
this.logger.info(`process-results::_compareImageHashResults: No Exact Match Found ${JSON.stringify(exactMatches)}`);
return {
sets: _.map(bestMatches, (match) => match.setName)
};
} else if (bestMatches.length === 1) {
console.log(`process-results::_compareImageHashResults: One Best Match Found ${JSON.stringify(bestMatches)}`);
this.logger.info(`process-results::_compareImageHashResults: One Best Match Found ${JSON.stringify(bestMatches)}`);
return {
value: bestMatches[0]
};
} else {
let sets = _.map(results, obj => obj.set_name);
console.log(`process-results::_compareImageHashResults: No Exact or Best Match Found ${JSON.stringify(sets)}`);
this.logger.info(`process-results::_compareImageHashResults: No Exact or Best Match Found ${JSON.stringify(sets)}`);
return {
sets
}
}
} catch (error) {
console.log(error);
this.logger.error(error);
}
};

Expand All @@ -156,7 +157,7 @@ ProcessResults.prototype._gatherResults = async function (object) {
//TODO Add Update when QTY > 1
if (isValid) {
if (this.queryingEnabled) {
console.log(`process-results::_gatherResults: Inserting Card in Card_Catalog ${model.data}`);
this.logger.info(`process-results::_gatherResults: Inserting Card in Card_Catalog`, model.data);
if (model.data.quantity > 1) {
//Update
} else {
Expand All @@ -165,8 +166,8 @@ ProcessResults.prototype._gatherResults = async function (object) {
}
}
} catch (e) {
console.log('Error');
console.log(e);
this.logger.error('Error');
this.logger.error(e);
}
};

Expand Down
1 change: 0 additions & 1 deletion src/file-io.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ async function CreateDirectory() {

function CleanUpFiles(directory, callback) {
rimraf(directory, (err) => {
console.log(`Removed Directory: ${directory}`)
if(err) {
return callback(err);
}
Expand Down
16 changes: 10 additions & 6 deletions src/image-analysis/extract-text.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
const {
cleanString
} = require('../util');

const log = require('../logger/log');
const dependencies = {
Tesseract: require('tesseract.js')
};

const logger = log.create({
isPretty: true
});

function ScanImage(imgBuffer, cb) {
console.log(`extract-text::ScanImage:: Scanning Card ${Buffer.isBuffer(imgBuffer)? 'Image Buffer' : imgBuffer}`);
logger.info(`extract-text::ScanImage:: Scanning Card ${Buffer.isBuffer(imgBuffer)? 'Image Buffer' : imgBuffer}`);
dependencies.Tesseract.recognize(imgBuffer)
.progress(message => {
console.log(JSON.stringify(message, null, 4))
logger.info(JSON.stringify(message, null, 4))
}).catch((err) => {
console.log(err);
logger.error(err);
return cb(err, null, dependencies.Tesseract);
})
.then(() => {

}).finally(resultOrError => {
let cleanedString = cleanString(resultOrError.text);
console.log(`Extracted text: ${resultOrError.text}`);
console.log(`Extracted cleaned text: ${cleanedString}`);
(`Extracted text: ${resultOrError.text}`);
logger.info(`Extracted cleaned text: ${cleanedString}`);
return cb(null, {
cleanText: cleanedString,
dirtyText: resultOrError.text
Expand Down
11 changes: 8 additions & 3 deletions src/image-hashing/base64-img.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const base64Img = require('image-to-base64');
const log = require('../logger/log');
const dependencies = {
base64Img
}
};

const logger = log.create({
isPretty: true
});

async function StringfyImagesNDAtn(imagePaths) {
try {
Expand All @@ -15,10 +20,10 @@ async function StringfyImagesNDAtn(imagePaths) {
typeImage,
nameImage
};
console.log(`base64-img::StringfyImagesNDAtn : ${Object.keys(base64Images)}`);
logger.info(`base64-img::StringfyImagesNDAtn : ${Object.keys(base64Images)}`);
return base64Images;
} catch (error) {
console.log(error);
logger.error(error);
return error;
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/image-hashing/hash-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ const {
const dependencies = {
imageHash
};
const log = require('../logger/log');

const logger = log.create({
isPretty: true
});

function HashImage(imgUrl, cb) {
console.log(`hash-image::HashImage:: Hashing Image ${imgUrl}`);
logger.info(`hash-image::HashImage:: Hashing Image ${imgUrl}`);
dependencies.imageHash(imgUrl, 16, true, (error, data) => {
if (error) {
return cb(error);
Expand All @@ -18,7 +23,7 @@ function HashImage(imgUrl, cb) {
}

function CompareHash(hashOne, hashTwo) {
console.log(`hash-image::CompareHash:: Comparing Hashes ${hashOne} ${hashTwo}`);
logger.info(`hash-image::CompareHash:: Comparing Hashes ${hashOne} ${hashTwo}`);
let HashLength = hashOne.length;
let twoBitMatches = 0;
let fourBitMatches = 0;
Expand All @@ -39,7 +44,7 @@ function CompareHash(hashOne, hashTwo) {
fourBitMatches: _.round(fourBitMatches / (HashLength / 4), 2),
stringCompare: _.round(stringSimilarity.compareTwoStrings(hashOne, hashTwo), 2)
};
console.log(`hash-image::CompareHash:: Hash Comparison Results ${comparisonResults}`);
logger.info(`hash-image::CompareHash:: Hash Comparison Results ${comparisonResults}`);
return comparisonResults;
}

Expand Down
59 changes: 59 additions & 0 deletions src/logger/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const _ = require('lodash');
const bunyan = require('bunyan');
function Logger(params) {
_.bindAll(this, Object.keys(Logger.prototype));
this.isPretty = params.isPretty || true;
if(this.isPretty) {
this.bunyan = bunyan.createLogger({
level: 4,
name: 'MTG-Processor'
});
}
}

Logger.prototype.info = function(message, object) {
if(this.bunyan) {
this.bunyan.info(`${message}`);
if(object) {
this.bunyan.info(object);
}
return;
}
console.log(message);
if(object) {
console.log(JSON.stringify(object,null, 4));
}
};
Logger.prototype.warn = function(message, object) {
if(this.bunyan) {
this.bunyan.warn(`${message}`);
if(object) {
this.bunyan.warn(object);
}
return;
}
console.warn(message);
if(object) {
console.warn(JSON.stringify(object,null, 4));
}
};

Logger.prototype.error = function(message, object) {
if(this.bunyan) {
this.bunyan.error(`${message}`);
if(object) {
this.bunyan.error(object);
}
return;
}
console.error(message);
if(object) {
console.error(JSON.stringify(object,null, 4));
}
};

module.exports = {
create: function (params) {
return new Logger(params);
}
}
Loading

0 comments on commit b9033a6

Please sign in to comment.