diff --git a/build/cmds/onelook.js b/build/cmds/onelook.js index 7dd7499..cd7e49b 100644 --- a/build/cmds/onelook.js +++ b/build/cmds/onelook.js @@ -46,13 +46,13 @@ exports.handler = function (argv) { var config = noon.load(CFILE); var proceed = false; var reset = false; - var stamp = new Date(config.onelook.date.stamp); - var hours = moment(new Date()).diff(stamp, 'hours'); - var minutes = moment(new Date()).diff(stamp, 'minutes'); var checkStamp = tools.limitOnelook(config); config = checkStamp[0]; proceed = checkStamp[1]; reset = checkStamp[2]; + var stamp = new Date(config.onelook.date.stamp); + var hours = moment(new Date()).diff(stamp, 'hours'); + var minutes = moment(new Date()).diff(stamp, 'minutes'); if (proceed) { (function () { var userConfig = { @@ -75,6 +75,11 @@ exports.handler = function (argv) { var ctstyle = _.get(chalk, theme.content.style); http({ url: url }, function (error, response) { if (!error && response.statusCode === 200) { + if (response.headers['x-gg-state'] === 'cached') { + config.onelook.date.remain++; + noon.save(CFILE, config); + if (config.usage) console.log('Cached response, not decrementing usage.'); + } var body = response.body; var parser = new xml2js.Parser(); parser.parseString(body, function (err, result) { @@ -121,18 +126,15 @@ exports.handler = function (argv) { } } if (argv.o) tools.outFile(argv.o, argv.f, tofile); - if (reset) { - console.log(config.onelook.date.remain + '/' + config.onelook.date.limit + ' requests remaining today.'); - } else { - if (config.usage) console.log(config.onelook.date.remain + '/' + config.onelook.date.limit + ' requests remaining today, will reset in ' + (23 - hours) + ' hours, ' + (59 - minutes) + ' minutes.'); + if (config.usage) { + if (reset) { + console.log('Timestamp expired, reset usage limits.'); + console.log(config.onelook.date.remain + '/' + config.onelook.date.limit + ' requests remaining today.'); + } else console.log(config.onelook.date.remain + '/' + config.onelook.date.limit + ' requests remaining today, will reset in ' + (23 - hours) + ' hours, ' + (59 - minutes) + ' minutes.'); } }); - } else { - throw new Error('HTTP ' + response.statusCode + ' ' + error); - } + } else throw new Error('HTTP ' + response.statusCode + ': ' + error); }); })(); - } else { - throw new Error('Reached today\'s usage limit of ' + config.onelook.date.limit + '.'); - } + } else throw new Error('Reached today\'s usage limit of ' + config.onelook.date.limit + '.'); }; \ No newline at end of file diff --git a/src/cmds/onelook.js b/src/cmds/onelook.js index 2bcab26..0fb7142 100644 --- a/src/cmds/onelook.js +++ b/src/cmds/onelook.js @@ -44,13 +44,13 @@ exports.handler = (argv) => { let config = noon.load(CFILE) let proceed = false let reset = false - const stamp = new Date(config.onelook.date.stamp) - const hours = moment(new Date).diff(stamp, 'hours') - const minutes = moment(new Date).diff(stamp, 'minutes') const checkStamp = tools.limitOnelook(config) config = checkStamp[0] proceed = checkStamp[1] reset = checkStamp[2] + const stamp = new Date(config.onelook.date.stamp) + const hours = moment(new Date).diff(stamp, 'hours') + const minutes = moment(new Date).diff(stamp, 'minutes') if (proceed) { const userConfig = { onelook: { @@ -72,6 +72,11 @@ exports.handler = (argv) => { const ctstyle = _.get(chalk, theme.content.style) http({ url }, (error, response) => { if (!error && response.statusCode === 200) { + if (response.headers['x-gg-state'] === 'cached') { + config.onelook.date.remain++ + noon.save(CFILE, config) + if (config.usage) console.log('Cached response, not decrementing usage.') + } const body = response.body const parser = new xml2js.Parser() parser.parseString(body, (err, result) => { @@ -118,17 +123,14 @@ exports.handler = (argv) => { } } if (argv.o) tools.outFile(argv.o, argv.f, tofile) - if (reset) { - console.log(`${config.onelook.date.remain}/${config.onelook.date.limit} requests remaining today.`) - } else { - if (config.usage) console.log(`${config.onelook.date.remain}/${config.onelook.date.limit} requests remaining today, will reset in ${23 - hours} hours, ${59 - minutes} minutes.`) + if (config.usage) { + if (reset) { + console.log('Timestamp expired, reset usage limits.') + console.log(`${config.onelook.date.remain}/${config.onelook.date.limit} requests remaining today.`) + } else console.log(`${config.onelook.date.remain}/${config.onelook.date.limit} requests remaining today, will reset in ${23 - hours} hours, ${59 - minutes} minutes.`) } }) - } else { - throw new Error(`HTTP ${response.statusCode} ${error}`) - } + } else throw new Error(`HTTP ${response.statusCode}: ${error}`) }) - } else { - throw new Error(`Reached today's usage limit of ${config.onelook.date.limit}.`) - } + } else throw new Error(`Reached today's usage limit of ${config.onelook.date.limit}.`) }