UNPKG

wordreference-definition-api

Version:

Scrap word definitions from http://www.wordreference.com

91 lines (86 loc) 3.03 kB
const scrap = require('./Scrap.js') const path = require('path') const level = require('level') const axios = require('axios') const rimraf = require('rimraf') const cacheConfig = { dir: path.join(__dirname, '..', 'cache'), keyEncoding: 'utf8', valueEncoding: 'json', use: true } const wrURL = 'https://www.wordreference.com/definition/' class Definition { constructor({ cache = cacheConfig, logs = false, URL = wrURL }) { cache = { ...cacheConfig, ...cache } this.logs = logs this.log('wordreference-definition-api: logs enabled') this.log('initializing cache') if (cache && cache.use === true) { this.cache = level(cache.dir, { keyEncoding: cache.keyEncoding, valueEncoding: cache.valueEncoding }) } this.useCache = cache.use this.wrURL = URL this.log('cache [OK]') } defineWithCache(word) { return new Promise((resolve, reject) => { this.cache.get(word).then((data) => { this.log(JSON.stringify(data, null, 2)) this.log('Word received from cache') resolve(data) }).catch(err => { if (err.message && err.message.search('not found') > -1) this.log('Word not found in cache, getting data online...') this.defineWithoutCache(word) .then(resolve) .catch(reject) }) }) } defineWithoutCache(word) { return new Promise((resolve, reject) => { axios.get(this.wrURL + word).then(({ data: HTML }) => { let data = scrap.HTML(HTML) this.log(data) if (this.useCache) { this.cache.put(word, data) .then(() => this.log(`Cache saved for ${word}`)) .catch((err) => this.log(`Cache for ${word} not saved!`,err)) } resolve(data) }).catch(err => { this.log("Error when trying to get data online", err) reject(err) }) }) } define(word) { if (this.useCache) { return this.defineWithCache(word) } else { return this.defineWithoutCache(word) } } cleanCache(word) { return new Promise((resolve) => { if (this.useCache === false) return resolve(false); if (!word) { this.log('Removing all cache') return rimraf(cacheConfig.dir, () => resolve(`The cache is clear`)) } this.cache.del(word).then(() => { resolve(`Word '${word}' removed from cache`) }) }) } log(...message) { if (this.logs) console.log(...message) } } module.exports = Definition