Skip to content

Commit

Permalink
fix: update api destination and responses
Browse files Browse the repository at this point in the history
  • Loading branch information
stagas committed Mar 24, 2022
1 parent 16129de commit dcc6da7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
34 changes: 28 additions & 6 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,50 @@
#!/usr/bin/env node

// POST https://wantwords.thunlp.org/GetEnDefis/
// POST https://wantwords.net/GetEnDefis/
// body FormData w=+purples+color+...&m=1
//
// GET https://wantwords.thunlp.org/EnglishRD/?description=colors&mode=EE
// GET https://wantwords.net/EnglishRD/?q=colors&m=EnEn

const fetch = import('node-fetch')
const columnize = require('columnize-array')
const { FormData } = require("formdata-node")
const argv = process.argv.slice(2)

const explain = argv.includes('--explain') && argv.splice(argv.indexOf('--explain'), 1)
const api = 'https://wantwords.net/'
const explain = (argv.includes('-e') && argv.splice(argv.indexOf('-e'), 1)) || (argv.includes('--explain') && argv.splice(argv.indexOf('--explain'), 1))

if (!argv.length || argv.includes('-h') || argv.includes('--help')) {
console.log(`
${process.argv[1]} [options] <word>
Options:
-e, --explain Read word dictionary definition
-h, --help Show this help
`)
process.exit(0)
}

const [name] = argv

if (explain) {
const body = new FormData()
body.set('w', name)
body.set('m', 1)
fetch.then(mod => mod.default(`https://wantwords.thunlp.org/GetEnDefis/`, {
fetch.then(mod => mod.default(`${api}GetEnDefis/`, {
method: 'POST',
body
})).then(res => res.json()).then(json => console.log(json[0].replaceAll('<b>', '\x1b[1m').replaceAll('</b>', '\x1b[0m').replaceAll('<br>', '\n')))
})).then(res => res.json()).then(json => {
if (!json.length) {
console.log('No definition found for:', name)
return
}
console.log(json[0].E[0].def.map(([kind, desc, subtypes]) =>
`\
\x1b[3m\x1b[4m${kind}\x1b[0m ${desc.replaceAll('<b>', '\x1b[1m').replaceAll('</b>', '\x1b[0m').replaceAll('<br>', '\n')}${subtypes.length ? ` \x1b[3m\x1b[2m(${subtypes.join(', ')})\x1b[0m` : ''}`
).join('\n'))
})
} else {
fetch.then(mod => mod.default(`https://wantwords.thunlp.org/EnglishRD/?description=${encodeURIComponent(name)}&mode=EE`)
fetch.then(mod => mod.default(`${api}EnglishRD/?q=${encodeURIComponent(name)}&m=EnEn`)
.then(res => res.json())
.then(json => {
console.log(columnize(json.map(x => x.w), { sort: true, maxRowLen: process.stdout.columns }).strs.join('\n'))
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ $ wantwords pink --explain

## Credits

This basically does a request to [WantWords](https://wantwords.thunlp.org/) (not affiliated).
This basically does a request to [WantWords](https://wantwords.net/) (not affiliated).

## License MIT

0 comments on commit dcc6da7

Please sign in to comment.