-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
83 lines (72 loc) · 2.16 KB
/
helpers.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
73
74
75
76
77
78
79
80
81
82
83
const fs = require('fs');
const { convertToMarkdown, formatDateAgo } = require('./utils');
function createMarkdownFile(data) {
const markdownContent = `---
title: '${data.title}'
date: '${formatDateAgo(data.createdAt)}'
category: ''
summary : '${data.summary}'
---
${convertToMarkdown(data.content)}`;
fs.writeFile(`posts/${data.fileName}.md`, markdownContent, (err) => {
if (err) {
console.error(err);
} else {
console.log(`${data.fileName} 파일 생성완료!`);
}
});
}
async function scrollToBottom(page) {
await page.evaluate(async () => {
await new Promise((resolve, reject) => {
let totalHeight = 0;
const distance = 1000;
const timer = setInterval(() => {
const scrollHeight = document.documentElement.scrollHeight;
window.scrollBy(0, distance);
totalHeight += distance;
if (totalHeight >= scrollHeight) {
clearInterval(timer);
resolve();
}
}, 100);
});
});
}
async function scrapPost(page, linkAndSummary) {
try {
await page.waitForNavigation();
await page.waitForSelector('h1:nth-child(1)');
await page.waitForSelector('div.information > span:nth-child(3)');
await page.waitForSelector('div.atom-one');
const postTitle = await page.evaluate(() => {
const selector = 'h1:nth-child(1)';
const title = document.querySelector(selector).innerText;
return title;
});
const postCreatedAt = await page.evaluate(() => {
const selector = 'div.information > span:nth-child(3)';
const createdAt = document.querySelector(selector).innerText;
return createdAt;
});
const postContent = await page.evaluate(() => {
const selector = 'div.atom-one';
const content = document.querySelector(selector).innerHTML;
return content;
});
return {
title: postTitle,
createdAt: postCreatedAt,
content: postContent,
summary: linkAndSummary.summary,
fileName: linkAndSummary.link.split('/')[2],
};
} catch (e) {
console.log('❌❌❌❌', page.url(), e);
}
}
module.exports = {
scrollToBottom,
scrapPost,
createMarkdownFile,
};