-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
137 lines (98 loc) · 4.28 KB
/
index.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
const
fs = require("fs/promises"),
puppeteer = require("puppeteer-core")
/**
*
* @typedef {object} options
* @property {string} chromiumPath the path for a chromium browser to run in it - required
* @property {boolean} [headless] whether to run chromium in [headless mode](https://developers.google.com/web/updates/2017/04/headless-chrome) or not - default true
* @property {boolean} [silent] operate in silent mode, there will be no logs in the terminal - default true
* @property {number} [timeout] page loading timeout in ms - default 30000 (30s)
*/
/**
*
* @param {string} subreddit the subreddit name to scrape
* @param {string} outDir the output directory
* @param {options} opts scrape options
* @returns {Promise<void>}
*/
async function subredditWikiScrapper(subreddit, outDir, opts = {}) {
subreddit = subreddit.toLowerCase()
await fs.mkdir(outDir, { recursive: true })
const doneScraping = new Set()
const browser = await puppeteer.launch({
executablePath: opts.chromiumPath,
headless: opts.headless === false ? false : true
})
const page = await browser.newPage()
typeof opts.timeout === "number" && page.setDefaultTimeout(opts.timeout)
page.setRequestInterception(true)
page.on("request", req => {
req.resourceType() === "document" ? req.continue() : req.abort()
})
opts.silent === false && console.log("Downloading index")
await page.goto(`https://www.reddit.com/r/${subreddit}/wiki/index`)
const wikiError = await page.waitForSelector(".md.wiki").then(() => 0).catch(err => err)
if (wikiError !== 0) {
opts.silent === false && console.log("/r/%s doesn't have a wiki\n\n%O", subreddit, wikiError)
await browser.close()
await fs.writeFile(`${outDir}/.nowiki`, "")
return
}
let [wiki, links] = await page.evaluate(() => [
document.querySelector(".md.wiki").innerHTML,
Array.from(document.querySelector(".md.wiki").querySelectorAll("a")).map(a => a.pathname.toLowerCase())
])
links = links.filter(link => link.indexOf(`/r/${subreddit}/wiki/`) !== -1).map(link => link.substring(9 + subreddit.length))
await fs.writeFile(`${outDir}/index.html`, wiki).then(() => doneScraping.add("index"))
wiki = null
await page.close()
for (const link of links) {
await recursiveScrape(outDir, browser, subreddit, link, doneScraping, opts)
}
opts.silent === false && console.log("\nsuccessfuly done downloading: \n%O", [...doneScraping.values()])
await browser.close()
}
/**
*
* @param {string} outDir out directory
* @param {object} browser browser object
* @param {string} subreddit subreddit name
* @param {string} link current link to scrape (stripped version)
* @param {set} doneScraping already scraped links
* @param {options} opts scrape options
* @returns {Promise<void>}
* @private
*/
async function recursiveScrape(outDir, browser, subreddit, link, doneScraping, opts) {
if (doneScraping.has(link)) return
opts.silent === false && console.log("Downloading %s", link)
const page = await browser.newPage()
typeof opts.timeout === "number" && page.setDefaultTimeout(opts.timeout)
page.setRequestInterception(true)
page.on("request", req => {
req.resourceType() === "document" ? req.continue() : req.abort()
})
await page.goto(`https://www.reddit.com/r/${subreddit}/wiki/${link}`)
const wikiError = await page.waitForSelector(".md.wiki").then(() => 0).catch(err => err)
if (wikiError !== 0) {
opts.silent === false && console.log("Failed Downloading %s\n\n%O\n", link, wikiError)
return
}
let [wiki, links] = await page.evaluate(() => [
document.querySelector(".md.wiki").innerHTML,
Array.from(document.querySelector(".md.wiki").querySelectorAll("a")).map(a => a.pathname.toLowerCase())
])
links = links.filter(link => link.indexOf(`/r/${subreddit}/wiki/`) !== -1).map(link => link.substring(9 + subreddit.length))
if (link.indexOf("/") !== -1) {
await fs.mkdir(`${outDir}/${link.substring(0, link.lastIndexOf("/"))}`, { recursive: true })
}
await fs.writeFile(`${outDir}/${link}.html`, wiki).then(() => doneScraping.add(link))
wiki = null
await page.close()
for (const newLink of links) {
if (doneScraping.has(newLink)) continue
await recursiveScrape(outDir, browser, subreddit, newLink, doneScraping, opts)
}
}
module.exports = subredditWikiScrapper