-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
366 lines (343 loc) · 13.4 KB
/
parser.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
const cheerio = require('cheerio');
const fetch = require('node-fetch');
const marked = require('marked');
function sanitizeUrl(url) {
return url.split('?')[0];
}
async function checkUrlArchive(url) {
url = sanitizeUrl(url);
const data = await fetch(`https://archive.org/wayback/available?url=${url}`);
if (!data) {
throw new Error('Unable to check URL');
}
const json = await data.json();
if (!json?.archived_snapshots?.closest?.url) {
throw new Error('Checked URL but no snapshot available');
}
return json.archived_snapshots.closest.url;
}
async function checkUrlGoogle(url, site) {
url = sanitizeUrl(url);
const searchTerm = url.split('/')[url.split('/').length - 1];
const res = await fetch(`https://content-customsearch.googleapis.com/customsearch/v1?cx=${process.env.GOOGLE_SEARCH_ID}&key=${process.env.GOOGLE_SEARCH_KEY}&q=${searchTerm}`)
const json = await res.json();
if (!json?.items?.length) {
throw new Error('Unable to get result from search engine');
}
const { cacheId } = json.items[0];
const webCacheUrl = `https://webcache.googleusercontent.com/search?q=cache:${cacheId}:${site}`;
return webCacheUrl;
}
function formatArticleText(text, protocol) {
try {
$ = cheerio.load(text);
// fix broken images due to mixed content error
$('img[src^="http://web.archive.org"], img[src^="https://web.archive.org"]').get().forEach(i => {
$(i).removeAttr('srcset');
const src = $(i).attr('src');
if (src && !src.startsWith(protocol)) {
$(i).attr('src', src.replace(/^https?:\/\/web.archive.org/, `${protocol}://web.archive.org`));
}
});
return $('*').html();
} catch (error) {
return text;
}
}
async function nyt(url) {
const rawHtml = await fetch(url);
const html = await rawHtml.text();
const $ = cheerio.load(html);
$('noscript').get().forEach(n => {
if ($(n).html().includes('optimistic-truncator')) {
$(n).parent().remove();
}
});
$('[data-testid="imageblock-wrapper"] :not(picture)').remove();
const articleHeadline = $('title').text().replace(' - The New York Times', '');
const articleText = $('section[name="articleBody"]').html();
return { articleText, articleHeadline };
}
async function nytCooking(url) {
const rawHtml = await fetch(url);
const html = await rawHtml.text();
const $ = cheerio.load(html);
$('[class^="ingredients_buttons"]').remove();
$("img").removeAttr("style");
$('[class^="popover_popover"]').parent().remove();
$('[class^="buttondialog_dialog"]').parent().remove();
$('[class^="topnote_unexpanded"] button').remove();
const articleHeadline = $('title').text().replace(' - NYT Cooking', '');
const articleIntro = $('[class^="recipeintro"]').toArray().map(a => $(a).html()).join('<br />');
const articleBody = $('[class^="recipebody"]').toArray().map(a => $(a).html()).join('<br />');
const articleText = articleIntro + articleBody;
return { articleText, articleHeadline };
}
async function newyorker(url) {
const rawHtml = await fetch(url);
const html = await rawHtml.text();
const $ = cheerio.load(html);
const scriptTag = $('script[type="application/ld+json"]').first().text().split(',\'keywords\':');
const json = JSON.parse(scriptTag[0]);;
const articleText = $('#articleBody').html();
const articleHeadline = json.headline;
return { articleText, articleHeadline };
}
async function economist(url) {
const rawHtml = await fetch(url);
const html = await rawHtml.text();
const $ = cheerio.load(html);
const articleHeadline = $('article h1').text();
let articleText = '';
$('[class^="article__body-"]').get().map(a => {
const html = $(a).html();
if (html) {
articleText += `${html}<br /><br />`;
}
});
return { articleText, articleHeadline };
}
async function washingtonPost(url) {
const rawHtml = await fetch(url);
const html = await rawHtml.text();
const $ = cheerio.load(html);
$('[data-qa="article-body-ad"]').remove();
$('[data-qa="subscribe-promo"]').remove();
$('.hide-for-print').remove();
const articleHeadline = $('span[data-qa="headline-text"]').text();
let articleText = '';
$('[data-qa="article-body"]').get().map(a => {
const html = $(a).html();
if (html) {
articleText += html;
}
});
return { articleText, articleHeadline };
}
async function latimes(url) {
const rawHtml = await fetch(url);
const html = await rawHtml.text();
const $ = cheerio.load(html);
const articleHeadline = $('title').first().text().replace('- Los Angeles Times', '');
$('.rich-text-article-body-content img').get().forEach(i => {
const image = $(i);
const rootUrl = image.attr('data-src').split('?url=')[1];
image.parent().html(`<img src="${decodeURIComponent(rootUrl)}" />`);
});
const articleHtml = $('.rich-text-article-body-content').html();
const articleText = articleHtml;
return { articleText, articleHeadline };
}
async function theAthletic(url) {
const rawHtml = await fetch(url);
const html = await rawHtml.text();
const $ = cheerio.load(html);
const articleHeadline = $('title').first().text().replace('- The Athletic', '');
const articleHtml = $('.article-content-container').html();
const articleText = articleHtml;
return { articleText, articleHeadline };
}
async function businessInsider(url) {
const rawHtml = await fetch(url);
const html = await rawHtml.text();
const $ = cheerio.load(html);
const articleHeadline = $('title').first().text();
$('.content-lock-content img.lazy-image').get().forEach(i => {
try {
const image = $(i);
image.parent('.lazy-holder').removeAttr('style');
const imageJson = JSON.parse(image.attr('data-srcs'));
const rootUrl = Object.keys(imageJson)[0];
image.parent().html(`<img src="${decodeURIComponent(rootUrl)}" />`);
} catch (error) {
// unable to parse image
}
});
$('.inline-newsletter-signup').remove();
const articleHtml = $('.content-lock-content').html();
const articleText = articleHtml;
return { articleText, articleHeadline };
}
async function bloomberg(url) {
const rawHtml = await fetch(url);
const html = await rawHtml.text();
const $ = cheerio.load(html);
const scriptTag = $('#__NEXT_DATA__').text();
const json = JSON.parse(scriptTag);
const content = json.props.pageProps.story.body.content;
let articleText = '';
content.forEach(item => {
if (item.type === 'paragraph') {
articleText += '<div>';
item.content.forEach(text => {
if (text.type === 'embed') {
articleText += `<div>${text.iframeData.html}</div>`;
} else if (text.content && text.content.length) {
text.content.forEach(t => {
articleText += `<span>${t.value}</span>`;
});
} else {
articleText += `<span>${text.value}</span>`;
}
});
articleText += '</div><br />';
} else if (item.type === 'media' && item.subType === 'photo') {
const image = item.data.remoteContent[0].link.destination.web;
articleText += `<div><img src="${image}" alt="Image from the article"></div><br />`;
}
});
const articleHeadline = json.props.pageProps.story.seoHeadline;
return { articleText, articleHeadline };
}
async function vogue(url) {
const rawHtml = await fetch(url);
const html = await rawHtml.text();
const $ = cheerio.load(html);
const articleHeadline = $('title').first().text().replace(' | Vogue', '');
let articleText = '';
$('.body__inner-container').get().map(a => {
const html = $(a).html();
if (html) {
articleText += `${html}<br />`;
}
});
return { articleText, articleHeadline };
}
async function atlantic(url) {
const rawHtml = await fetch(url);
const html = await rawHtml.text();
const $ = cheerio.load(html);
const articleHeadline = $('title').first().text().replace(' - The Atlantic', '');
let articleText = $("[class^='ArticleBody_root']").html();
return { articleText, articleHeadline };
}
async function forbes(url) {
const rawHtml = await fetch(url);
const html = await rawHtml.text();
const $ = cheerio.load(html);
const articleHeadline = $('title').first().text().replace(' - Forbes', '');
$('.article-body-image').removeAttr('style');
$('.article-sharing').remove();
$('progressive-image').get().forEach(i => {
const image = $(i);
const rootUrl = image.attr('src');
image.html(`<img src="${rootUrl}" />`);
});
let articleText = $('.article-body').html();
return { articleText, articleHeadline };
}
async function wired(url) {
const rawHtml = await fetch(url);
const html = await rawHtml.text();
const $ = cheerio.load(html);
const articleHeadline = $('title').first().text().replace(' | WIRED', '');
$('external-link').get().forEach(a => {
$(a).attr("href", $(a).attr('data-offer-url'));
});
let articleText = '';
$('.body__inner-container').get().forEach(b => {
const html = $(b).html();
if (html) {
articleText += `${html}<br />`;
}
});
return { articleText, articleHeadline };
}
async function scientificAmerican(url) {
const rawHtml = await fetch(url);
const html = await rawHtml.text();
const $ = cheerio.load(html);
const articleHeadline = $('title').first().text().replace(' | Scientific American', '');
const articleText = $("[class^='article__content']").html();
return { articleText, articleHeadline };
}
async function getContent(source, url, method) {
console.log(source, url, method);
let articleText = null;
let articleHeadline = null;
if (method === 'ARCHIVE') {
url = await checkUrlArchive(url);
} else if (method === 'GOOGLE') {
url = await checkUrlGoogle(url, source);
}
switch(source) {
case 'nytimes.com':
const nytRes = await nyt(url);
articleText = nytRes.articleText;
articleHeadline = nytRes.articleHeadline;
break;
case 'cooking.nytimes.com':
const nytCookingRes = await nytCooking(url);
articleText = nytCookingRes.articleText;
articleHeadline = nytCookingRes.articleHeadline;
break;
case 'newyorker.com':
const newyorkerRes = await newyorker(url);
articleText = newyorkerRes.articleText;
articleHeadline = newyorkerRes.articleHeadline;
break;
case 'economist.com':
const economistRes = await economist(url);
articleText = economistRes.articleText;
articleHeadline = economistRes.articleHeadline;
break;
case 'washingtonpost.com':
const washingtonPostRes = await washingtonPost(url);
articleText = washingtonPostRes.articleText;
articleHeadline = washingtonPostRes.articleHeadline;
break;
case 'latimes.com':
const laTimesRes = await latimes(url);
articleText = laTimesRes.articleText;
articleHeadline = laTimesRes.articleHeadline;
break;
case 'theathletic.com':
const theAthleticRes = await theAthletic(url);
articleText = theAthleticRes.articleText;
articleHeadline = theAthleticRes.articleHeadline;
break;
case 'businessinsider.com':
const businessInsiderRes = await businessInsider(url);
articleText = businessInsiderRes.articleText;
articleHeadline = businessInsiderRes.articleHeadline;
break;
case 'bloomberg.com':
const bloombergRes = await bloomberg(url);
articleText = bloombergRes.articleText;
articleHeadline = bloombergRes.articleHeadline;
break;
case 'vogue.com':
const vogueRes = await vogue(url);
articleText = vogueRes.articleText;
articleHeadline = vogueRes.articleHeadline;
break;
case 'theatlantic.com':
const atlanticRes = await atlantic(url);
articleText = atlanticRes.articleText;
articleHeadline = atlanticRes.articleHeadline;
break;
case 'forbes.com':
const forbesRes = await forbes(url);
articleText = forbesRes.articleText;
articleHeadline = forbesRes.articleHeadline;
break;
case 'wired.com':
const wiredRes = await wired(url);
articleText = wiredRes.articleText;
articleHeadline = wiredRes.articleHeadline;
break;
case 'scientificamerican.com':
const scientificAmericanRes = await scientificAmerican(url);
articleText = scientificAmericanRes.articleText;
articleHeadline = scientificAmericanRes.articleHeadline;
break;
default:
articleText = 'No article found';
articleHeadline = '404';
}
return {articleText, articleHeadline};
}
module.exports = {
formatArticleText,
getContent,
}