Created
March 8, 2017 06:15
-
-
Save CyberFerret/e79bce5de4049582ef51b0a54ffecbc5 to your computer and use it in GitHub Desktop.
Example of Node.js app to replicate Hacker News Firebase feed into RethinkDB
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
var firebase = require('firebase'); | |
var r = require('rethinkdb'); | |
firebase.initializeApp({ | |
"appName" : "HN News Feed", | |
"databaseURL" : "https://hacker-news.firebaseio.com/" | |
}); | |
var rdbconn = null; | |
r.connect( {host: '<your rethinkdbhost.com>', port: 28015, user: '<your rethink user>', password: '<your rethink password>'}, function(err, conn) { | |
if (err) throw err; | |
rdbconn = conn; | |
}); | |
// Get a reference to the Top Stories feed | |
var newStoryRef = firebase.app().database().ref("v0/newstories"); | |
// You can get other story feeds too - see the HN API docs for more info. (Don't forget to set up a 'watch' function (see last block of code) for each story feed you want). | |
// var topStoryRef = firebase.app().database().ref("v0/topstories"); | |
// var bestStoryRef = firebase.app().database().ref("v0/beststories"); | |
// And the changes feed | |
var updatesRef = firebase.app().database().ref("v0/updates"); | |
function pullUser(currentUser) { | |
// Read full story from HN | |
firebase.app().database().ref("v0/user/" + currentUser).once("value").then(function(snapshot) { | |
// Add it to RethinkDB | |
if (snapshot.val() !== null) { | |
r.db("hn_data").table("hn_users").insert(snapshot.val(), {conflict: "update"}).run(rdbconn, function(err, result) { | |
if (err) throw err; | |
var username = "[" + snapshot.val().id + "]"; | |
if (result.inserted) { | |
console.log("Inserted user" + username); | |
} | |
if (result.replaced) { | |
console.log("Replaced user" + username); | |
} | |
}); | |
} | |
}); | |
} | |
function pullItem(currentItem) { | |
// Read full story from HN | |
firebase.app().database().ref("v0/item/" + currentItem.toString()).once("value").then(function(snapshot) { | |
// Add it to RethinkDB | |
if (snapshot.val() !== null) { | |
r.db("hn_data").table("hn_feed").insert(snapshot.val(), {conflict: "update"}).run(rdbconn, function(err, result) { | |
if (err) throw err; | |
if (snapshot.val().hasOwnProperty("title")) { | |
var itemTitle = snapshot.val().title; | |
} else { | |
var itemTitle = "A comment"; | |
} | |
if (result.inserted) { | |
console.log("Inserted: " + itemTitle); | |
} | |
if (result.replaced) { | |
console.log("Replaced: " + itemTitle); | |
} | |
}); | |
} | |
}); | |
} | |
// Set a watch on the changes | |
updatesRef.on("value", function(snap) { | |
var items = snap.val().items; | |
var users = snap.val().profiles; | |
// Loop through the items | |
for (var i = 0; i < items.length; i++) { | |
pullItem(items[i]); | |
} // for | |
// Loop through the users | |
for (var i = 0; i < users.length; i++) { | |
pullUser(users[i]); | |
} // for | |
}); | |
// Set a watch on the top stories, and act if Firebase pushes out a change | |
// NOTE: You need to set up a separate 'watcher' for every story feed that you want to 'listen' to. | |
newStoryRef.on("value", function(snap) { | |
console.log("(New Stories pushed from HN)"); | |
var pushData = {id: "newstories", list: snap.val()}; | |
r.db("hn_data").table("hn_lists").insert(pushData, {conflict: "update"}).run(rdbconn, function(err, result) { | |
if (err) throw err; | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment