Last active
September 23, 2021 01:17
-
-
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
This file contains 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 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