-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
35 lines (30 loc) · 990 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'use strict';
var flowy = require('flowy');
var request = require('request');
var cheerio = require('cheerio');
module.exports = function(term) {
return new Promise((ok, nok) => {
var url = urlForTerm(term);
request(url, (err, res, body) => {
if (err) return nok(err)
if (res.statusCode != 200) {
throw new Error(`Bad status (${res.statusCode}) for the url: ${url}`)
}
return ok(stripBody(body))
});
})
};
function urlForTerm(term) {
var langs = {
deen: 'http://pocket.dict.cc',
deru: 'http://deru.pocket.dict.cc'
};
var lang = term.match(/[а-яА-Я]/) ? 'deru' : 'deen';
return langs[lang] + '?s=' + encodeURIComponent(term);
}
function stripBody(html) {
var $ = cheerio.load(html);
var results = $.html('body > div > dl')
var absUrlsResults = results.toString().replace(/href=\"\/\?/g, 'href=\"http://pocket.dict.cc/?')
return absUrlsResults && `<div class="dictcc">${absUrlsResults}</div>`
}