forked from lewisdonovan/google-news-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetArticleContent.js
155 lines (135 loc) · 4.23 KB
/
getArticleContent.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const { Readability } = require('@mozilla/readability');
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const {
proxyRequest,
} = require('puppeteer-proxy');
const verifyMessages = [
"you are human",
"are you human",
"i'm not a robot",
"recaptcha"
];
function* cycle(arr) {
while (true) {
for (let i = 0; i < arr.length; i++) {
yield arr[i];
}
}
}
const cycleProxies = (iterableProxies = null) => {
if (!iterableProxies) return null;
return iterableProxies.next().value;
}
const getArticleContent = async (articles, browser, filterWords, logger, rotatingProxies) => {
try {
const iterableProxies = rotatingProxies?.length ? cycle(rotatingProxies) : null;
const processedArticlesPromises = articles.map(article =>
extractArticleContentAndFavicon(article, browser, filterWords, logger, cycleProxies(iterableProxies))
);
const processedArticles = await Promise.all(processedArticlesPromises);
return processedArticles;
} catch (err) {
logger.error("getArticleContent ERROR:", err);
return articles;
}
}
const extractArticleContentAndFavicon = async (article, browser, filterWords, logger, proxy = null) => {
try {
const page = await browser.newPage();
if (proxy) {
await page.setRequestInterception(true);
page.on('request', async (request) => {
await proxyRequest({
page,
proxyUrl: proxy,
request,
});
});
}
await page.goto(article.link, { waitUntil: 'networkidle2' });
const content = await page.evaluate(() => document.documentElement.innerHTML);
const favicon = await page.evaluate(() => {
const link = document.querySelector('link[rel="icon"], link[rel="shortcut icon"]');
return link ? link.getAttribute('href') : '';
});
const virtualConsole = new jsdom.VirtualConsole();
virtualConsole.on("error", logger.error);
const dom = new JSDOM(content, { url: article.link, virtualConsole });
let reader = new Readability(dom.window.document);
const articleContent = reader.parse();
if (!articleContent || !articleContent.textContent) {
logger.warn("Article content could not be parsed or is empty.", {article});
return { ...article, content: '', favicon};
}
const hasVerifyMessage = verifyMessages.find(w => articleContent.textContent.toLowerCase().includes(w));
if (hasVerifyMessage) {
logger.warn("Article requires human verification.", {article});
return { ...article, content: '', favicon};
}
const cleanedText = cleanText(articleContent.textContent, filterWords);
if (cleanedText.split(' ').length < 100) { // Example threshold: 100 words
logger.warn("Article content is too short and likely not valuable.", {article});
return { ...article, content: '', favicon };
}
logger.info("SUCCESSFULLY SCRAPED ARTICLE CONTENT:", cleanedText);
return { ...article, content: cleanedText, favicon};
} catch (error) {
logger.error(error);
return { ...article, content: '', favicon: '' };
}
}
const cleanText = (text, filterWords) => {
const unwantedKeywords = [
"subscribe now",
"sign up",
"newsletter",
"subscribe now",
"sign up for our newsletter",
"exclusive offer",
"limited time offer",
"free trial",
"download now",
"join now",
"register today",
"special promotion",
"promotional offer",
"discount code",
"early access",
"sneak peek",
"save now",
"don't miss out",
"act now",
"last chance",
"expires soon",
"giveaway",
"free access",
"premium access",
"unlock full access",
"buy now",
"learn more",
"click here",
"follow us on",
"share this article",
"connect with us",
"advertisement",
"sponsored content",
"partner content",
"affiliate links",
"click here",
"for more information",
"you may also like",
"we think you'll like",
"from our network",
...filterWords
];
return text
.split('\n')
.map(line => line.trim())
.filter(line => line.split(' ').length > 4)
.filter(line => !unwantedKeywords.some(keyword => line.toLowerCase().includes(keyword)))
.join('\n');
}
module.exports = {
default: getArticleContent
}