-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (52 loc) · 1.82 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
import { RECIPE_LINKS } from "./recipeLinks.js";
import fs from "fs";
import puppeteer from "puppeteer";
// Launch the browser and open a new blank page
const browser = await puppeteer.launch({
// headless: false,
// slowMo: 250, // slow down by 250ms
});
const originalRecipeLinks = [];
const noLink = []; // Recipes without original links (probably manually created)
const getLink = async (pageLink) => {
const page = await browser.newPage();
// Navigate the page to a URL.
await page.goto(pageLink);
// Set screen size.
await page.setViewport({ width: 1080, height: 1024 });
// Wait for recipe to load.
const textSelector = "h1[data-testid=desktop-saved-recipe-name]";
const heading = await page.waitForSelector(textSelector);
const fullTitle = await heading?.evaluate((el) => el.textContent);
console.log(`Fetching original recipe link for ${fullTitle}`);
// Get original link
try {
const recipeLinkSelector = ".s40084 a";
await page.waitForSelector(recipeLinkSelector);
const recipeLink = await page.$eval(recipeLinkSelector, (e) => e.href);
if (recipeLink.includes("profile")) {
console.log("recipe has no original link");
noLink.push(fullTitle);
return;
}
console.log(`Adding link for ${fullTitle} to list!`);
originalRecipeLinks.push(recipeLink);
} catch {
console.log(`Could not find original link for ${fullTitle}`);
} finally {
page.close();
console.log("");
}
};
for (const pageLink of RECIPE_LINKS) {
await getLink(pageLink);
}
await browser.close();
fs.writeFile("recipeLinks.json", JSON.stringify(originalRecipeLinks), (err) => {
if (err) throw err;
console.log("File written successfully");
});
fs.writeFile("userCreatedRecipes.json", JSON.stringify(noLink), (err) => {
if (err) throw err;
console.log("File written successfully");
});