Skip to content

Instantly share code, notes, and snippets.

@yickson
Created March 8, 2021 15:42
Show Gist options
  • Save yickson/ac2ae1eab0ae33cc4fb530e4e1074619 to your computer and use it in GitHub Desktop.
Save yickson/ac2ae1eab0ae33cc4fb530e4e1074619 to your computer and use it in GitHub Desktop.
Download file with Puppeteer in Cloud Functions

Cloud functions of Google only download file in folder "/tmp"

const puppeteer = require('puppeteer');
const urlLogin = 'https://myweb.com';
exports.getzip = async (req, res) => {
const { user, pass, year, month } = req.body;
console.log('Starting puppeteer...');
const browser = await puppeteer.launch({ headless: true, slowMo: 100});
try {
const page = await browser.newPage();
console.log('go to url login...');
await page.goto(urlLogin);
console.log('Insertdata login...');
await page.type('#username', user);
await page.type('#password', pass);
await page.click('input[type="submit"]');
console.log('Enter my platform...');
await page.waitForSelector('#mContribuyentes_on');
console.log('Autenticación exitosa');
await page.click('#Contribuyente > table > tbody > tr:nth-child(6)');
await page.goto(`https://myweb.com?filterDte.stateSuccess=Doc_OK&periodEmit=${year}-${month}`);
await page.waitForTimeout(2000);
console.log('Enable download...');
await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: '/tmp/'});
await page.click('#datatable-responsive > thead > tr > th:nth-child(1) > input[type=checkbox]');
await page.click('#resumenFERForm > div.col-md-12 > div:nth-child(1) > div > button:nth-child(3)');
await page.waitForTimeout(7000);
res.download('/tmp/dtes.zip');
} catch (e) {
console.log(e);
res.status(200).send({ response: 'error'});
} finally {
await browser.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment