el.xwx.moe/lib/api/archive.ts

73 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-03-28 02:31:50 -05:00
import { Page } from "puppeteer";
2023-03-25 09:17:34 -05:00
import { prisma } from "@/lib/api/db";
2023-03-28 02:31:50 -05:00
import puppeteer from "puppeteer-extra";
import AdblockerPlugin from "puppeteer-extra-plugin-adblocker";
import StealthPlugin from "puppeteer-extra-plugin-stealth";
export default async function archive(
url: string,
collectionId: number,
linkId: number
) {
const archivePath = `data/archives/${collectionId}/${linkId}`;
2023-03-28 02:31:50 -05:00
const browser = await puppeteer.launch();
2023-03-28 02:31:50 -05:00
try {
puppeteer.use(AdblockerPlugin()).use(StealthPlugin());
2023-03-28 02:31:50 -05:00
const page = await browser.newPage();
2023-03-28 02:31:50 -05:00
await page.goto(url, { waitUntil: "domcontentloaded", timeout: 300000 });
await page.setViewport({ width: 1080, height: 1024 });
await autoScroll(page);
const linkExists = await prisma.link.findFirst({
where: {
id: linkId,
},
});
2023-03-28 02:31:50 -05:00
if (linkExists) {
await Promise.all([
2023-06-11 17:28:37 -05:00
page.pdf({
path: archivePath + ".pdf",
2023-06-13 09:49:44 -05:00
width: "1366px",
height: "1931px",
2023-06-11 17:28:37 -05:00
printBackground: true,
margin: { top: "15px", bottom: "15px" },
}),
2023-03-28 02:31:50 -05:00
page.screenshot({ fullPage: true, path: archivePath + ".png" }),
]);
}
await browser.close();
} catch (err) {
console.log(err);
await browser.close();
2023-03-25 09:17:34 -05:00
}
}
2023-03-28 02:31:50 -05:00
const autoScroll = async (page: Page) => {
await page.evaluate(async () => {
await new Promise<void>((resolve, reject) => {
let totalHeight = 0;
let distance = 100;
let scrollDown = setInterval(() => {
let scrollHeight = document.body.scrollHeight;
window.scrollBy(0, distance);
totalHeight += distance;
if (totalHeight >= scrollHeight) {
clearInterval(scrollDown);
window.scroll(0, 0);
resolve();
}
}, 100);
});
await new Promise((r) => setTimeout(r, 2000));
});
};