Created
August 8, 2017 07:07
-
-
Save lgvalle/9e2fefe95d34615db3a227d8b1336359 to your computer and use it in GitHub Desktop.
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 functions = require('firebase-functions') | |
const URL_THE_GUARDIAN = "https://www.theguardian.com/uk/london/rss" | |
const Client = require('node-rest-client').Client | |
const client = new Client() | |
exports.fetch = functions.https.onRequest((req, res) => { | |
client.get(URL_THE_GUARDIAN, function (data, response) { | |
const items = cleanUp(data) | |
res.status(201) | |
.type('application/json') | |
.send(items) | |
}) | |
}) | |
function cleanUp(data) { | |
// Empty array to add cleaned up elements to | |
const items = [] | |
// We are only interested in children of the 'channel' element | |
const channel = data.rss.channel | |
channel.item.forEach(element => { | |
item = { | |
title: element.title, | |
description: element.description, | |
date: element.pubDate, | |
creator: element['dc:creator'], | |
media: [] | |
} | |
// Iterates through all the elements named '<media:content>' extracting the info we care about | |
element['media:content'].forEach(mediaContent => { | |
item.media.push({ | |
url: mediaContent.$.url, // Parses media:content url attribute | |
credit: mediaContent['media:credit']._ // Parses media:credit tag content | |
}) | |
}) | |
items.push(item) | |
}) | |
return items | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment