Skip to content
Open
367 changes: 170 additions & 197 deletions README.md

Large diffs are not rendered by default.

70 changes: 70 additions & 0 deletions demo-using-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

// load the mysql library
var mysql = require('promise-mysql');

// create a connection to our Cloud9 server
var connection = mysql.createPool({
host : 'localhost',
user : 'root', // CHANGE THIS :)
password : '',
database: 'reddit',
connectionLimit: 10
});

// load our API and pass it the connection
var RedditAPI = require('./reddit');

var myReddit = new RedditAPI(connection);

// We call this function to create a new user to test our API
// The function will return the newly created user's ID in the callback
// myReddit.createUser({
// username: 'PM_ME_CUTES',
// password: 'abc123'
// })
// .then(newUserId => {
// // Now that we have a user ID, we can use it to create a new post
// // Each post should be associated with a user ID
// console.log('New user created! ID=' + newUserId);

// /*return*/ myReddit.createPost({
// title: 'A grate joke!',
// url: 'http://www.digg.com',
// userId: 1,
// subredditId: 2
// })

// .then(newPostId => {
// // If we reach that part of the code, then we have a new post. We can print the ID
// console.log('New post created! ID=' + newPostId);
// })
// .catch(error => {
// console.log(error.stack);
// });


myReddit.getAllPosts()
.then(response => {
// console.log(response)
}
)
.then(response => {
connection.end();
});

// myReddit.createSubreddit({name: 'funny', description: 'grate jokes'})
// .then(response => {
// console.log(response);
// })
// .then(response => {
// connection.end();
// });
// myReddit.getAllSubreddits()
// .then(response => {
// console.log(response);
// })
// .then (response => {
// connection.end();
// });


38 changes: 0 additions & 38 deletions index.js

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"license": "ISC",
"dependencies": {
"bcrypt": "^0.8.6",
"mysql": "^2.10.2"
"bcrypt-as-promised": "^1.1.0",
"promise-mysql": "^3.0.1"
}
}
100 changes: 100 additions & 0 deletions reddit-crawl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
var request = require('request-promise');
var mysql = require('promise-mysql');
var RedditAPI = require('./reddit');

function getSubreddits() {
return request(/* fill in the URL, it's always the same */)
.then(response => {
// Parse response as JSON and store in variable called result
var response; // continue this line

// Use .map to return a list of subreddit names (strings) only
return response.data.children.map(/* write a function */)
});
}

function getPostsForSubreddit(subredditName) {
return request(/* fill in the URL, it will be based on subredditName */)
.then(
response => {
// Parse the response as JSON and store in variable called result
var response; // continue this line


return response.data.children
.filter(/* write a function */) // Use .filter to remove self-posts
.map(/* write a function */) // Use .map to return title/url/user objects only

}
);
}

function crawl() {
// create a connection to the DB
var connection = mysql.createPool({
host : 'localhost',
user : 'root',
password : '',
database: 'reddit',
connectionLimit: 10
});

// create a RedditAPI object. we will use it to insert new data
var myReddit = new RedditAPI(connection);

// This object will be used as a dictionary from usernames to user IDs
var users = {};

/*
Crawling will go as follows:

1. Get a list of popular subreddits
2. Loop thru each subreddit and:
a. Use the `createSubreddit` function to create it in your database
b. When the creation succeeds, you will get the new subreddit's ID
c. Call getPostsForSubreddit with the subreddit's name
d. Loop thru each post and:
i. Create the user associated with the post if it doesn't exist
2. Create the post using the subreddit Id, userId, title and url
*/

// Get a list of subreddits
getSubreddits()
.then(subredditNames => {
subredditNames.forEach(subredditName => {
var subId;
myReddit.createSubreddit({name: subredditName})
.then(subredditId => {
subId = subredditId;
return getPostsForSubreddit(subredditName)
})
.then(posts => {
posts.forEach(post => {
var userIdPromise;
if (users[post.user]) {
userIdPromise = Promise.resolve(users[post.user]);
}
else {
userIdPromise = myReddit.createUser({
username: post.user,
password: 'abc123'
})
.catch(function(err) {
return users[post.user];
})
}

userIdPromise.then(userId => {
users[post.user] = userId;
return myReddit.createPost({
subredditId: subId,
userId: userId,
title: post.title,
url: post.url
});
});
});
});
});
});
}
Loading