2023-04-23 08:26:39 -05:00
// Copyright (C) 2022-present Daniel31x13 <daniel31x13@gmail.com>
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3.
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
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" ;
2023-03-08 15:31:24 -06:00
export default async ( 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-08 15:31:24 -06:00
2023-03-28 02:31:50 -05:00
try {
puppeteer . use ( AdblockerPlugin ( ) ) . use ( StealthPlugin ( ) ) ;
2023-03-08 15:31:24 -06:00
2023-03-28 02:31:50 -05:00
const page = await browser . newPage ( ) ;
2023-03-08 15:31:24 -06:00
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-08 15:31:24 -06:00
2023-03-28 02:31:50 -05:00
if ( linkExists ) {
await Promise . all ( [
page . pdf ( { path : archivePath + ".pdf" , format : "a4" } ) ,
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
} ;
2023-03-08 15:31:24 -06: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 ) ) ;
} ) ;
2023-03-08 15:31:24 -06:00
} ;