Skip to content

Instantly share code, notes, and snippets.

@mirsahib
Last active December 26, 2020 08:34
Show Gist options
  • Save mirsahib/ae0e75cb45ebb0a5c7d7e530a987b21e to your computer and use it in GitHub Desktop.
Save mirsahib/ae0e75cb45ebb0a5c7d7e530a987b21e to your computer and use it in GitHub Desktop.
import axios from "axios";
const cheerio = require("cheerio");
async function sleep(millis) {
return new Promise((resolve) => setTimeout(resolve, millis));
}
const getData = async (customer_no, year, month) => {
const container = [];
const PromiseArr = [];
try {
//loop through the customer_no and scrape
for (let i = 0; i < customer_no.length; i++) {
let url = `https://dpdc.org.bd/service/ebill?btyp=current&year=${year}&month=${month}&cno=${customer_no[i]}&email=`;
PromiseArr.push(axios.get(url));
} //end of for loop
Promise.all(PromiseArr).then(function (response) {
const $ = cheerio.load(response.data);
/*
scraping logic here
*/
container.push(data);//push the scrape data
await sleep(5000);//wait for 5s
}); //end of promise all
} catch (error) {
console.log(error); // output to netlify function log
return {
statusCode: 400,
body: JSON.stringify({ msg: err.message }), // Could be a custom message or object i.e. JSON.stringify(err)
};
}
console.log(container);
return container;
};
export async function handler(event, context) {
/*
event.queryParameter
*/
try {
const data = await getData(customer, year, month);
return {
statusCode: 200,
body: JSON.stringify({ data: data }),
};
} catch (err) {
console.log(err); // output to netlify function log
return {
statusCode: 500,
body: JSON.stringify({ msg: err.message }), // Could be a custom message or object i.e. JSON.stringify(err)
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment