-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
47 lines (41 loc) · 1.54 KB
/
test.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
36
37
38
39
40
41
42
43
44
45
46
47
import * as cheerio from 'cheerio';
import nlp from 'compromise';
import speechPlugin from 'compromise-speech'
nlp.plugin(speechPlugin)
import axios from 'axios';
import UserAgent from 'user-agents';
import dummydata from './error.js';
const userAgent = new UserAgent();
var wordOfDay = [];
axios({
method: 'GET',
url: 'https://randomword.com/',
headers: {
'User-Agent': userAgent.toString(),
'Accept-Encoding': 'identity'
}
}).then(function(response) {
const $ = cheerio.load(response.data);
if (wordOfDay.length > 0) {
wordOfDay = [];
}
var post = $('.section #shared_section');
var word = post.find('#random_word').eq(0).text().replace('\r\n\t\t\t\t\t', '').replace('\r\n\t\t\t\t', '').replace('\n\t\t\t\t\t', '').replace('\n\t\t\t\t', '');
var definition = post.find('#random_word_definition').eq(0).text().replace('\n', '');
var pronounceword = word;
let doc = nlp(pronounceword);
var pronounced = doc.terms().soundsLike()
var pronounce = pronounced;
wordOfDay.push({
word: decodeURI(word.charAt(0).toUpperCase() + word.slice(1)),
definition: decodeURI(definition.charAt(0).toUpperCase() + definition.slice(1)),
pronunciation: decodeURI(pronounce)
})
console.log(wordOfDay);
}).catch(function(error) {
if (!error.response) {
console.log(dummydata());
} else {
console.log(dummydata());
}
});