Last active
September 22, 2019 14:42
-
-
Save adityajoshi12/2c6fb3d6a13fe44dd3c10515f1d3e511 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require('puppeteer') | |
const admin = require('firebase-admin'); | |
const functions = require('firebase-functions') | |
admin.initializeApp(functions.config().firebase); | |
const db = admin.firestore(); | |
var options={ memory: '2GB', timeoutSeconds: 300, } | |
const findPrice = async () => { | |
let url = 'https://www.amazon.in/dp/B07C2VJXP4/' | |
let browser = await puppeteer.launch({ | |
headless: true, | |
args: ['--no-sandbox', '--disable-setuid-sandbox'] | |
}) | |
let page = await browser.newPage() | |
await page.setViewport({ width: 1920, height: 1080 }); | |
await page.setRequestInterception(true); | |
page.on('request', (req) => { | |
if (req.resourceType() == 'stylesheet' || req.resourceType() == 'font' || req.resourceType() == 'image') { | |
req.abort(); | |
} | |
else { | |
req.continue(); | |
} | |
}); | |
await page.goto(url, { waitUntil: 'networkidle2' }) | |
const data = await page.evaluate(() => { | |
let currentPrice = document.querySelector('#priceblock_ourprice').innerText | |
return currentPrice | |
}) | |
await browser.close() | |
return data | |
} | |
const formatPrice = (Price) => { | |
var price = Price.replace(",", "") | |
return price.substring(2, Price.length) | |
} | |
exports.priceTrackerCron = functions.runWith(options).pubsub.schedule('every 120 minutes').onRun((context) => { | |
return findPrice().then(res => { | |
var currentPrice = formatPrice(res) | |
console.log(Number(currentPrice)) | |
console.log(new Date().toLocaleTimeString()) | |
db.collection("test").add({ price: Number(currentPrice), time: Date.now() }); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment