const puppeteer = require("puppeteer");
const fs = require("fs");
(async () => {
const amount = 25;
const url = "https://www.reddit.com/r/ProgrammerHumor/";
let jokesObject = {};
console.log("Getting posts from Reddit");
const browser = await puppeteer.launch({
headless: false
});
const page = await browser.newPage();
await page.goto(url, { waitUntil: "load" });
const root = (await page.$$(`.rpBJOHq2PR60pnwJlUyP0`))[0];
const posts = [];
for (let i = 0; i < amount; i++) {
const chunk = await (await root.$$("._1poyrkZ7g36PawDueRza-J"));
posts.push(...chunk);
await sleep(1000);
await page.evaluate(() => {
window.scrollBy(0, (632 * 12));
});
}
console.log("Extracting jokes from posts");
for (const post of posts) {
try {
const title = await getProperty(post, "textContent", "_eYtD2XCVieq6emjKBH3m");
const image = await getProperty(post, "src", "ImageBox-image");
jokesObject[title] = { image: image };
} catch (error) {
}
}
console.log("Converting jokes into an array");
const jokes = [];
for (const joke in jokesObject) {
jokes.push({
title: joke,
image: jokesObject[joke].image
})
}
console.log("Saving jokes");
fs.writeFileSync("jokes.json", JSON.stringify(jokes));
await browser.close();
})();
async function getProperty(rootElement, property, className) {
const element = (await rootElement.$$(`.${className}`))[0];
return await (await element.getProperty(property)).jsonValue();
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}