-
Notifications
You must be signed in to change notification settings - Fork 0
/
googleapis.js
54 lines (52 loc) · 1.9 KB
/
googleapis.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
48
49
50
51
52
53
54
const axios = require('axios').default;
const userAgent = require('user-agents');
googleapis = {
request : async (keyword)=>{
try{
const ua = new userAgent({deviceCategory:'desktop'}).toString();
console.log(googleapis.formatKeyword(keyword));
const {data} = await axios.get(`https://www.googleapis.com/customsearch/v1?key=AIzaSyDLyvAmzjPQhYExny85ZGawRPMps5Dvs7c&cx=245e4667658debe04&q=${googleapis.formatKeyword(keyword)}`,{
headers : {
'User-Agent' : ua
},
// proxy: {
// host: 'localhost',
// port: 3000,
// auth: {username:'', password : ''},
// protocol : 'https'
// }
});
const searchInformation = data?.searchInformation;
return {
keyword,
result : parseFloat(searchInformation?.totalResults),
time : parseFloat(searchInformation?.searchTime),
};
}catch(err){
console.log(err);
return {keyword, result : 0, time : 0};
}
},
formatKeyword : (value)=>{
const regex = /\s/g;
return value?.replace(regex,'+');
},
format : (value)=>{
const regexs = [
/Sekitar (.*) hasil \((.*?) detik\)/gm,
/About (.*) results \((.*?) seconds\)/gm
];
let response;
regexs.map((regex,index)=>{
const result = regex.exec(value);
if(result?.[1] != undefined && result?.[2] != undefined){
response = {
'result' : parseFloat(result[1]?.replace(/\./g,'').replace(',', '.')),
'time' : parseFloat(result[2]?.replace(/\./g,'').replace(',', '.')),
}
}
})
return response;
},
};
module.exports = googleapis;