Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lgvalle
Last active September 23, 2021 01:17
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lgvalle/df2a0a7ee10266ca8056fa15654307d8 to your computer and use it in GitHub Desktop.
Save lgvalle/df2a0a7ee10266ca8056fa15654307d8 to your computer and use it in GitHub Desktop.
Firebase cloud function to scrap html and send the content with push notifications
const rp = require('request-promise');
const cheerio = require('cheerio');
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
exports.allyPallyFarmersMarket = functions.https.onRequest((request, response) => {
const topic = "allyPallyFarmersMarket"
const url = `https://weareccfm.com/city-country-farmers-markets/market-profiles/alexandra-palace-market/`
const options = {
uri: url,
headers: { 'User-Agent': 'test' },
transform: (body) => cheerio.load(body)
}
rp(options)
.then(($) => {
const scrap = $('strong').text()
const [location, date, address] = scrap.split("–")
// Build message
var message = {
data: {
location: location,
date: date,
url: url
},
topic: topic
};
// Send a message to devices subscribed to the provided topic.
admin.messaging().send(message)
.then((res) => response
.status(200)
.type('application/json')
.send('-- Location: ' + location + ' -- Date: ' + date + ' -- Address: ' + address))
.catch((error) => response.status(400).send(error))
})
.catch((err) => response.status(400).send(err))
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment