-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.js
More file actions
63 lines (57 loc) · 1.63 KB
/
scraper.js
File metadata and controls
63 lines (57 loc) · 1.63 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const rp = require('request-promise');
const $ = require('cheerio');
const TurndownService = require('turndown');
var turndownService = new TurndownService( {emDelimiter: '*'});
turndownService.addRule('noLinks', {
filter: function (node, options) {
return (
node.nodeName === 'A' &&
node.getAttribute('href')
)
},
replacement: function (content, node) {
return content;
}
});
// TODO: show extra search results..
// TODO: make a new harness for testing, discord is too heavy
// TODO: work with search
// TODO: check for accents
// TODO: make way to get 2nd or 3rd meaning or get all meanings
// TODO: print out how many other meanings there are so user knows even if we return only 1
// returns markdown text
const defParse = function(url) {
return rp(url)
.then(function(html){
let word = $('.dico_title_2', html).first().html();
var markdown = turndownService.turndown(word);
console.log("first sense: ", markdown);
let def = $('.dico_definition .dico_liste .grid_last', html).first().html();
markdown += '\n' + turndownService.turndown(def);
console.log('md=\n' + markdown);
return markdown;
})
.catch(function(err){
console.log('error code=', err.statusCode);
throw (err);
});
};
// returns list of URLs
const searchParse = function(url) {
return rp(url)
.then(function(html){
let results = [];
$('.results li h2 a', html).each(function(i, elem) {
results.push($(this)[0].attribs.href);
});
return results;
})
.catch(function(err){
console.log('error code=', err.statusCode);
throw (err);
});
};
module.exports = {
defParse,
searchParse
};