Last active
December 27, 2015 11:49
-
-
Save Usse/7321643 to your computer and use it in GitHub Desktop.
Parse latest reddit article and insert them in a Mongo db collection.
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
var MongoClient = require('mongodb').MongoClient | |
, request = require('request'); | |
MongoClient.connect('mongodb://localhost:27017/reddit', function(err, db) { | |
if(err) throw err; | |
request('http://www.reddit.com/r/webdev/.json', function(error,response,body) { | |
if(!error && response.statusCode == 200) { | |
var obj = JSON.parse(body); | |
var stories = obj.data.children.map(function(story) {return story.data;}); | |
db.collection('webdev').insert(stories, function(err,data) { | |
if(err) throw err; | |
db.close(); | |
}) | |
} | |
}); | |
}); |
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
{ | |
"name": "rd", | |
"version": "0.0.0", | |
"description": "Import reddit", | |
"main": "app.js", | |
"dependencies": { | |
"mongodb": "~1.3.11", | |
"request" : "*" | |
}, | |
"author": "Me", | |
"license": "BSD", | |
"private": true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment