choco-bot
Version:
Whatsapp-bot
74 lines (57 loc) • 2.62 kB
JavaScript
const puppeteer = require('puppeteer');
const path = require('path');
const archiver = require('archiver');
const { URL } = require('url');
const sanitizeFilename = require('sanitize-filename');
function dwp(remainingMessage, msg){
const dll_link = remainingMessage.trim();
const browser = puppeteer.launch();
const page = browser.newPage();
const chat = msg.getChat();
const url = dll_link;
page.goto(url, { waitUntil: 'networkidle2' });
const requests = page.evaluate(() => {
return Array.from(window.performance.getEntriesByType('resource'), (resource) => {
return resource.name;
});
});
const downloadFolder = '../downloaded_resources';
if (!fs.existsSync(downloadFolder)) {
fs.mkdirSync(downloadFolder);
}
const archive = archiver('zip', {
zlib: { level: 9 },
});
const parsedURL = new URL(url);
const hostname = "tempfile";
const zipFileName = `${hostname}_downloaded_resources.zip`;
const zipFilePath = path.join(downloadFolder, zipFileName);
const output = fs.createWriteStream(zipFilePath);
archive.pipe(output);
for (const request of requests) {
if (request.includes('https://fonts.googleapis.com/')) {
continue;
}
const sanitizedFileName = sanitizeFilename(request.replace(/[:/]/g, ''));
if (sanitizedFileName) {
const filePath = path.join(downloadFolder, sanitizedFileName);
page._client.send('Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath: downloadFolder,
});
page.goto(request);
page.waitForTimeout(2000);
fs.renameSync(path.join(downloadFolder, request), filePath);
archive.file(filePath, { name: sanitizedFileName });
} else {
archive.append(request, { name: 'links_without_extension.txt' });
}
}
archive.finalize();
browser.close();
const media = MessageMedia.fromFilePath("./downloaded_resources/temfile.zip");
chat.sendMessage(k, { caption: '*Resources Downloaded*' });
}
module.exports = {
dwp,
}