forked from ericmurphyxyz/nftgrabber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (65 loc) · 2.08 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
const puppeteer = require("puppeteer");
const fs = require("fs");
const path = require("path");
const url = process.argv[2];
if (!url) {
console.log(`Please enter a URL (e.g. "npm start https://rarible.com/boredapeyachtclub").`);
return;
}
(async () => {
console.log("Loading...");
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.goto(url);
await page.setViewport({
width: 1200,
height: 800,
});
await page.waitForSelector(".ReactVirtualized__Grid");
const pageTitle = await page.title();
const collection = await pageTitle.split("-").shift().trim();
if (!fs.existsSync(collection)) {
fs.mkdirSync(collection);
}
let currentImage = 1;
page.on("response", async (response) => {
const imageUrl = response.url();
if (response.request().resourceType() === "image") {
response.buffer().then((file) => {
if (imageUrl.includes("t_preview")) {
const fileName = imageUrl.split("/").pop() + ".avif";
const filePath = path.resolve(__dirname, collection, fileName);
const writeStream = fs.createWriteStream(filePath);
writeStream.write(file);
console.log(`${collection} #${currentImage} saved to ${collection}/${fileName}`);
currentImage++;
}
});
}
});
await autoScroll(page);
await page.evaluate(() => {
const elements = [...document.querySelectorAll("button")];
const targetElement = elements.find((e) => e.innerText.includes("Load more"));
targetElement && targetElement.click();
});
await autoScroll(page);
await browser.close();
})();
async function autoScroll(page) {
await page.evaluate(async () => {
await new Promise((resolve) => {
let totalHeight = 0;
let distance = 500;
let timer = setInterval(() => {
let scrollHeight = document.body.scrollHeight;
window.scrollBy(0, distance);
totalHeight += distance;
if (totalHeight >= scrollHeight) {
clearInterval(timer);
resolve();
}
}, 1000);
});
});
}