-
Notifications
You must be signed in to change notification settings - Fork 8
/
handler.js
72 lines (63 loc) · 1.62 KB
/
handler.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const got = require('got');
const cheerio = require('cheerio');
const dynamoose = require('dynamoose');
require('aws-sdk').config.region = "ap-northeast-2";
const PortalKeyword = dynamoose.model('PortalKeyword', {
portal: {
type: String,
hashKey: true
},
createdAt: {
type: String,
rangeKey: true
},
keywords: {
type: Array
}
}, {
create: false, // Create a table if not exist,
});
exports.crawler = async function (event, context, callback) {
try {
let naverKeywords = [];
let daumKeywords = [];
const result = await Promise.all([
got('https://naver.com'),
got('http://daum.net'),
]);
const createdAt = new Date().toISOString();
const naverContent = result[0].body;
const daumContent = result[1].body;
const $naver = cheerio.load(naverContent);
const $daum = cheerio.load(daumContent);
// Get doms containing latest keywords
$naver('.ah_l').filter((i, el) => {
return i===0;
}).find('.ah_item').each(((i, el) => {
if(i >= 20) return;
const keyword = $naver(el).find('.ah_k').text();
naverKeywords.push({rank: i+1, keyword});
}));
$daum('.rank_cont').find('.link_issue[tabindex=-1]').each((i, el) => {
const keyword = $daum(el).text();
daumKeywords.push({rank: i+1, keyword});
});
// console.log({
// naver: naverKeywords,
// daum: daumKeywords,
// });
await new PortalKeyword({
portal: 'naver',
createdAt,
keywords: naverKeywords
}).save();
await new PortalKeyword({
portal: 'daum',
createdAt,
keywords: daumKeywords
}).save();
return callback(null, "success");
} catch (err) {
callback(err);
}
}