Skip to content

Instantly share code, notes, and snippets.

@Usse
Last active December 27, 2015 11:49
Show Gist options
  • Save Usse/7321643 to your computer and use it in GitHub Desktop.
Save Usse/7321643 to your computer and use it in GitHub Desktop.
Parse latest reddit article and insert them in a Mongo db collection.
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();
})
}
});
});
{
"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