-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateArticle.js
43 lines (34 loc) · 1.13 KB
/
generateArticle.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
const $ = require("cheerio");
const axios = require("axios").default;
const postToTelegraph = require("./utils/postToTelegraph");
const headers = require("./secrets/headers.json");
const nytimesHandler = require("./siteHandlers/nytimesHandler");
const stHandler = require("./siteHandlers/stHandler");
async function generateArticle(url, domain) {
if (domain === "nytimes.com") {
return nytimesHandler(url);
} else if (domain === "straitstimes.com") {
return stHandler(url);
} else {
const res =
domain === "bloomberg.com"
? await axios.get(url, { headers: headers.bloomberg })
: await axios.get(url, { headers: headers.generic });
let html = res.data;
const title = $("title", html).text();
let domNode;
switch (domain) {
case "wsj.com":
domNode = $("div[amp-access='access']", html)[0];
break;
case "wired.com":
domNode = $(".article__chunks", html)[0];
break;
case "bloomberg.com":
domNode = $(".main-column-v2", html)[0];
break;
}
return await postToTelegraph(title, domNode);
}
}
module.exports = generateArticle;