Skip to content

Commit

Permalink
fix(rhymebrain): rate limiting output
Browse files Browse the repository at this point in the history
  • Loading branch information
drawnepicenter committed Jul 20, 2016
1 parent 5b98c47 commit 2a13efe
Show file tree
Hide file tree
Showing 22 changed files with 311 additions and 167 deletions.
15 changes: 10 additions & 5 deletions build/cmds/dmuse_cmds/get.js

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions build/cmds/onelook.js

Large diffs are not rendered by default.

23 changes: 15 additions & 8 deletions build/cmds/rhymebrain_cmds/combine.js

Large diffs are not rendered by default.

23 changes: 15 additions & 8 deletions build/cmds/rhymebrain_cmds/info.js

Large diffs are not rendered by default.

27 changes: 17 additions & 10 deletions build/cmds/wordnik_cmds/define.js

Large diffs are not rendered by default.

25 changes: 16 additions & 9 deletions build/cmds/wordnik_cmds/example.js

Large diffs are not rendered by default.

25 changes: 16 additions & 9 deletions build/cmds/wordnik_cmds/hyphen.js

Large diffs are not rendered by default.

21 changes: 14 additions & 7 deletions build/cmds/wordnik_cmds/origin.js

Large diffs are not rendered by default.

25 changes: 16 additions & 9 deletions build/cmds/wordnik_cmds/phrase.js

Large diffs are not rendered by default.

27 changes: 17 additions & 10 deletions build/cmds/wordnik_cmds/pronounce.js

Large diffs are not rendered by default.

24 changes: 15 additions & 9 deletions build/cmds/wordnik_cmds/relate.js

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions src/cmds/dmuse_cmds/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ exports.handler = (argv) => {
let config = noon.load(CFILE)
let proceed = false
const stamp = new Date(config.dmuse.date.stamp)
const now = new Date
const diff = moment(now).diff(stamp, 'hours')
const reset = 24 - diff
const now = moment(new Date).diff(stamp, 'hours')
const diff = 24 - now
let reset = false
if (diff < 24) {
config.dmuse.date.remain = config.dmuse.date.remain - 1
noon.save(CFILE, config)
} else if (diff >= 24) {
reset = true
config.dmuse.date.stamp = moment().format()
config.dmuse.date.remain = config.dmuse.date.limit
console.log(chalk.white(`Reset API limit to ${config.dmuse.date.limit}/${config.dmuse.date.interval}.`))
Expand Down Expand Up @@ -123,7 +124,11 @@ exports.handler = (argv) => {
if (argv.o) tools.outFile(argv.o, argv.f, tofile)
if (argv.s && config.merge) noon.save(CFILE, config)
if (argv.s && !config.merge) console.err(chalk.red('Set option merge to true!'))
console.log(`${config.dmuse.date.remain}/${config.dmuse.date.limit} requests remaining today, will reset in ${reset} hours.`)
if (reset) {
console.log(`${config.dmuse.date.remain}/${config.dmuse.date.limit} requests remaining today.`)
} else {
console.log(`${config.dmuse.date.remain}/${config.dmuse.date.limit} requests remaining today, will reset in ${diff} hours.`)
}
} else {
console.error(`${chalk.red.bold(`HTTP ${response.statusCode}:`)} ${chalk.red(error)}`)
}
Expand Down
13 changes: 9 additions & 4 deletions src/cmds/onelook.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ exports.handler = (argv) => {
let config = noon.load(CFILE)
let proceed = false
const stamp = new Date(config.onelook.date.stamp)
const now = new Date
const diff = moment(now).diff(stamp, 'hours')
const reset = 24 - diff
const now = moment(new Date).diff(stamp, 'hours')
const diff = 24 - now
let reset = false
if (diff < 24) {
config.onelook.date.remain = config.onelook.date.remain - 1
noon.save(CFILE, config)
} else if (diff >= 24) {
reset = true
config.onelook.date.stamp = moment().format()
config.onelook.date.remain = config.onelook.date.limit
console.log(chalk.white(`Reset API limit to ${config.onelook.date.limit}/${config.onelook.date.interval}.`))
Expand Down Expand Up @@ -128,7 +129,11 @@ exports.handler = (argv) => {
if (argv.o) tools.outFile(argv.o, argv.f, tofile)
if (argv.s && config.merge) noon.save(CFILE, config)
if (argv.s && !config.merge) console.err(chalk.red('Set option merge to true!'))
console.log(`${config.onelook.date.remain}/${config.onelook.date.limit} requests remaining today, will reset in ${reset} hours.`)
if (reset) {
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 ${diff} hours.`)
}
} else {
console.error(`${chalk.red.bold(`HTTP ${response.statusCode}:`)} ${chalk.red(error)}`)
}
Expand Down
21 changes: 14 additions & 7 deletions src/cmds/rhymebrain_cmds/combine.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ exports.handler = (argv) => {
let config = noon.load(CFILE)
let proceed = false
const stamp = new Date(config.rbrain.date.stamp)
const now = new Date
const diff = moment(now).diff(stamp, 'minutes')
const reset = 60 - diff
const now = moment(new Date).diff(stamp, 'minutes')
const diff = 60 - now
let reset = false
if (diff < 60) {
config.rbrain.date.remain = config.rbrain.date.remain - 1
noon.save(CFILE, config)
} else if (diff >= 60) {
reset = true
config.rbrain.date.stamp = moment().format()
config.rbrain.date.remain = config.rbrain.date.limit
console.log(chalk.white(`Reset API limit to ${config.rbrain.date.limit}/${config.rbrain.date.interval}.`))
Expand All @@ -73,9 +74,11 @@ exports.handler = (argv) => {
}
if (proceed) {
const userConfig = {
combine: {
lang: argv.l,
max: argv.m,
rbrain: {
combine: {
lang: argv.l,
max: argv.m,
},
},
}
if (config.merge) config = _.merge({}, config, userConfig)
Expand Down Expand Up @@ -109,7 +112,11 @@ exports.handler = (argv) => {
if (argv.o) tools.outFile(argv.o, argv.f, tofile)
if (argv.s && config.merge) noon.save(CFILE, config)
if (argv.s && !config.merge) console.err(chalk.red('Set option merge to true!'))
console.log(`${config.rbrain.date.remain}/${config.rbrain.date.limit} requests remaining this hour, will reset in ${reset} minutes.`)
if (reset) {
console.log(`${config.rbrain.date.remain}/${config.rbrain.date.limit} requests remaining this hour.`)
} else {
console.log(`${config.rbrain.date.remain}/${config.rbrain.date.limit} requests remaining this hour, will reset in ${diff} minutes.`)
}
} else {
console.error(`${chalk.red.bold(`HTTP ${response.statusCode}:`)} ${chalk.red(error)}`)
}
Expand Down
21 changes: 14 additions & 7 deletions src/cmds/rhymebrain_cmds/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ exports.handler = (argv) => {
let config = noon.load(CFILE)
let proceed = false
const stamp = new Date(config.rbrain.date.stamp)
const now = new Date
const diff = moment(now).diff(stamp, 'minutes')
const reset = 60 - diff
const now = moment(new Date).diff(stamp, 'minutes')
const diff = 60 - now
let reset = false
if (diff < 60) {
config.rbrain.date.remain = config.rbrain.date.remain - 1
noon.save(CFILE, config)
} else if (diff >= 60) {
reset = true
config.rbrain.date.stamp = moment().format()
config.rbrain.date.remain = config.rbrain.date.limit
console.log(chalk.white(`Reset API limit to ${config.rbrain.date.limit}/${config.rbrain.date.interval}.`))
Expand All @@ -67,9 +68,11 @@ exports.handler = (argv) => {
}
if (proceed) {
const userConfig = {
info: {
lang: argv.l,
max: argv.m,
rbrain: {
info: {
lang: argv.l,
max: argv.m,
},
},
}
if (config.merge) config = _.merge({}, config, userConfig)
Expand Down Expand Up @@ -118,7 +121,11 @@ exports.handler = (argv) => {
if (argv.o) tools.outFile(argv.o, argv.f, tofile)
if (argv.s && config.merge) noon.save(CFILE, config)
if (argv.s && !config.merge) console.err(chalk.red('Set option merge to true!'))
console.log(`${config.rbrain.date.remain}/${config.rbrain.date.limit} requests remaining this hour, will reset in ${reset} minutes.`)
if (reset) {
console.log(`${config.rbrain.date.remain}/${config.rbrain.date.limit} requests remaining this hour.`)
} else {
console.log(`${config.rbrain.date.remain}/${config.rbrain.date.limit} requests remaining this hour, will reset in ${diff} minutes.`)
}
} else {
console.error(`${chalk.red.bold(`HTTP ${response.statusCode}:`)} ${chalk.red(error)}`)
}
Expand Down
25 changes: 16 additions & 9 deletions src/cmds/wordnik_cmds/define.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ exports.handler = (argv) => {
let config = noon.load(CFILE)
let proceed = false
const stamp = new Date(config.wordnik.date.stamp)
const now = new Date
const diff = moment(now).diff(stamp, 'minutes')
const reset = 60 - diff
const now = moment(new Date).diff(stamp, 'minutes')
const diff = 60 - now
let reset = false
if (diff < 60) {
config.wordnik.date.remain = config.wordnik.date.remain - 1
noon.save(CFILE, config)
} else if (diff >= 60) {
reset = true
config.wordnik.date.stamp = moment().format()
config.wordnik.date.remain = config.wordnik.date.limit
console.log(chalk.white(`Reset API limit to ${config.wordnik.date.limit}/${config.wordnik.date.interval}.`))
Expand All @@ -85,11 +86,13 @@ exports.handler = (argv) => {
}
if (proceed) {
const userConfig = {
define: {
canon: argv.c,
limit: argv.l,
defdict: argv.d,
part: argv.p,
wordnik: {
define: {
canon: argv.c,
limit: argv.l,
defdict: argv.d,
part: argv.p,
},
},
}
if (config.merge) config = _.merge({}, config, userConfig)
Expand Down Expand Up @@ -138,7 +141,11 @@ exports.handler = (argv) => {
if (argv.o) tools.outFile(argv.o, argv.f, tofile)
if (argv.s && config.merge) noon.save(CFILE, config)
if (argv.s && !config.merge) console.err(chalk.red('Set option merge to true!'))
console.log(`${config.wordnik.date.remain}/${config.wordnik.date.limit} requests remaining this hour, will reset in ${reset} minutes.`)
if (reset) {
console.log(`${config.wordnik.date.remain}/${config.wordnik.date.limit} requests remaining this hour.`)
} else {
console.log(`${config.wordnik.date.remain}/${config.wordnik.date.limit} requests remaining this hour, will reset in ${diff} minutes.`)
}
} else {
console.error(`${chalk.red.bold(`HTTP ${response.statusCode}:`)} ${chalk.red(error)}`)
}
Expand Down
23 changes: 15 additions & 8 deletions src/cmds/wordnik_cmds/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ exports.handler = (argv) => {
let config = noon.load(CFILE)
let proceed = false
const stamp = new Date(config.wordnik.date.stamp)
const now = new Date
const diff = moment(now).diff(stamp, 'minutes')
const reset = 60 - diff
const now = moment(new Date).diff(stamp, 'minutes')
const diff = 60 - now
let reset = false
if (diff < 60) {
config.wordnik.date.remain = config.wordnik.date.remain - 1
noon.save(CFILE, config)
} else if (diff >= 60) {
reset = true
config.wordnik.date.stamp = moment().format()
config.wordnik.date.remain = config.wordnik.date.limit
console.log(chalk.white(`Reset API limit to ${config.wordnik.date.limit}/${config.wordnik.date.interval}.`))
Expand All @@ -79,10 +80,12 @@ exports.handler = (argv) => {
}
if (proceed) {
const userConfig = {
example: {
canon: argv.c,
limit: argv.l,
skip: argv.k,
wordnik: {
example: {
canon: argv.c,
limit: argv.l,
skip: argv.k,
},
},
}
if (config.merge) config = _.merge({}, config, userConfig)
Expand Down Expand Up @@ -122,7 +125,11 @@ exports.handler = (argv) => {
if (argv.o) tools.outFile(argv.o, argv.f, tofile)
if (argv.s && config.merge) noon.save(CFILE, config)
if (argv.s && !config.merge) console.err(chalk.red('Set option merge to true!'))
console.log(`${config.wordnik.date.remain}/${config.wordnik.date.limit} requests remaining this hour, will reset in ${reset} minutes.`)
if (reset) {
console.log(`${config.wordnik.date.remain}/${config.wordnik.date.limit} requests remaining this hour.`)
} else {
console.log(`${config.wordnik.date.remain}/${config.wordnik.date.limit} requests remaining this hour, will reset in ${diff} minutes.`)
}
} else {
console.error(`${chalk.red.bold(`HTTP ${response.statusCode}:`)} ${chalk.red(error)}`)
}
Expand Down
23 changes: 15 additions & 8 deletions src/cmds/wordnik_cmds/hyphen.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ exports.handler = (argv) => {
let config = noon.load(CFILE)
let proceed = false
const stamp = new Date(config.wordnik.date.stamp)
const now = new Date
const diff = moment(now).diff(stamp, 'minutes')
const reset = 60 - diff
const now = moment(new Date).diff(stamp, 'minutes')
const diff = 60 - now
let reset = false
if (diff < 60) {
config.wordnik.date.remain = config.wordnik.date.remain - 1
noon.save(CFILE, config)
} else if (diff >= 60) {
reset = true
config.wordnik.date.stamp = moment().format()
config.wordnik.date.remain = config.wordnik.date.limit
console.log(chalk.white(`Reset API limit to ${config.wordnik.date.limit}/${config.wordnik.date.interval}.`))
Expand All @@ -79,10 +80,12 @@ exports.handler = (argv) => {
}
if (proceed) {
const userConfig = {
hyphen: {
canon: argv.c,
dict: argv.d,
limit: argv.l,
wordnik: {
hyphen: {
canon: argv.c,
dict: argv.d,
limit: argv.l,
},
},
}
if (config.merge) config = _.merge({}, config, userConfig)
Expand Down Expand Up @@ -131,7 +134,11 @@ exports.handler = (argv) => {
if (argv.o) tools.outFile(argv.o, argv.f, tofile)
if (argv.s && config.merge) noon.save(CFILE, config)
if (argv.s && !config.merge) console.err(chalk.red('Set option merge to true!'))
console.log(`${config.wordnik.date.remain}/${config.wordnik.date.limit} requests remaining this hour, will reset in ${reset} minutes.`)
if (reset) {
console.log(`${config.wordnik.date.remain}/${config.wordnik.date.limit} requests remaining this hour.`)
} else {
console.log(`${config.wordnik.date.remain}/${config.wordnik.date.limit} requests remaining this hour, will reset in ${diff} minutes.`)
}
} else {
console.error(`${chalk.red.bold(`HTTP ${response.statusCode}:`)} ${chalk.red(error)}`)
}
Expand Down
19 changes: 13 additions & 6 deletions src/cmds/wordnik_cmds/origin.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ exports.handler = (argv) => {
let config = noon.load(CFILE)
let proceed = false
const stamp = new Date(config.wordnik.date.stamp)
const now = new Date
const diff = moment(now).diff(stamp, 'minutes')
const reset = 60 - diff
const now = moment(new Date).diff(stamp, 'minutes')
const diff = 60 - now
let reset = false
if (diff < 60) {
config.wordnik.date.remain = config.wordnik.date.remain - 1
noon.save(CFILE, config)
} else if (diff >= 60) {
reset = true
config.wordnik.date.stamp = moment().format()
config.wordnik.date.remain = config.wordnik.date.limit
console.log(chalk.white(`Reset API limit to ${config.wordnik.date.limit}/${config.wordnik.date.interval}.`))
Expand All @@ -68,8 +69,10 @@ exports.handler = (argv) => {
}
if (proceed) {
const userConfig = {
origin: {
canon: argv.c,
wordnik: {
origin: {
canon: argv.c,
},
},
}
if (config.merge) config = _.merge({}, config, userConfig)
Expand Down Expand Up @@ -109,7 +112,11 @@ exports.handler = (argv) => {
if (argv.o) tools.outFile(argv.o, argv.f, tofile)
if (argv.s && config.merge) noon.save(CFILE, config)
if (argv.s && !config.merge) console.err(chalk.red('Set option merge to true!'))
console.log(`${config.wordnik.date.remain}/${config.wordnik.date.limit} requests remaining this hour, will reset in ${reset} minutes.`)
if (reset) {
console.log(`${config.wordnik.date.remain}/${config.wordnik.date.limit} requests remaining this hour.`)
} else {
console.log(`${config.wordnik.date.remain}/${config.wordnik.date.limit} requests remaining this hour, will reset in ${diff} minutes.`)
}
} else {
console.error(`${chalk.red.bold(`HTTP ${response.statusCode}:`)} ${chalk.red(error)}`)
}
Expand Down
23 changes: 15 additions & 8 deletions src/cmds/wordnik_cmds/phrase.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ exports.handler = (argv) => {
let config = noon.load(CFILE)
let proceed = false
const stamp = new Date(config.wordnik.date.stamp)
const now = new Date
const diff = moment(now).diff(stamp, 'minutes')
const reset = 60 - diff
const now = moment(new Date).diff(stamp, 'minutes')
const diff = 60 - now
let reset = false
if (diff < 60) {
config.wordnik.date.remain = config.wordnik.date.remain - 1
noon.save(CFILE, config)
} else if (diff >= 60) {
reset = true
config.wordnik.date.stamp = moment().format()
config.wordnik.date.remain = config.wordnik.date.limit
console.log(chalk.white(`Reset API limit to ${config.wordnik.date.limit}/${config.wordnik.date.interval}.`))
Expand All @@ -79,10 +80,12 @@ exports.handler = (argv) => {
}
if (proceed) {
const userConfig = {
phrase: {
canon: argv.c,
limit: argv.l,
weight: argv.w,
wordnik: {
phrase: {
canon: argv.c,
limit: argv.l,
weight: argv.w,
},
},
}
if (config.merge) config = _.merge({}, config, userConfig)
Expand Down Expand Up @@ -119,7 +122,11 @@ exports.handler = (argv) => {
if (argv.o) tools.outFile(argv.o, argv.f, tofile)
if (argv.s && config.merge) noon.save(CFILE, config)
if (argv.s && !config.merge) console.err(chalk.red('Set option merge to true!'))
console.log(`${config.wordnik.date.remain}/${config.wordnik.date.limit} requests remaining this hour, will reset in ${reset} minutes.`)
if (reset) {
console.log(`${config.wordnik.date.remain}/${config.wordnik.date.limit} requests remaining this hour.`)
} else {
console.log(`${config.wordnik.date.remain}/${config.wordnik.date.limit} requests remaining this hour, will reset in ${diff} minutes.`)
}
} else {
console.error(`${chalk.red.bold(`HTTP ${response.statusCode}:`)} ${chalk.red(error)}`)
}
Expand Down
Loading

0 comments on commit 2a13efe

Please sign in to comment.