Skip to content

Commit

Permalink
adding login
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblyles committed Apr 13, 2012
1 parent ab20ca9 commit 7717018
Show file tree
Hide file tree
Showing 35 changed files with 2,267 additions and 35 deletions.
109 changes: 74 additions & 35 deletions app.js
Expand Up @@ -4,52 +4,91 @@
* Node-Reddit
*
* A simple CLI app that reads in Reddit's frontpage and colorizes it
*
* by Kamran Ayub
* with optional login info passed in at the command line
* username is passed with -u, --user, or first arg
* passwd is passed with -p, --passwd, or second arg
*
* by Kamran Ayub and Jacob Lyles
* License: WTFPL (http://sam.zoy.org/wtfpl/)
* Attribution is cool but not required
********************/

var cli = require('cli'),
http = require('http'),
request = require('request'),
argv = require('optimist').argv,
colors = require('colors');

var cookie, uh;
var USER = argv.u || argv.user || argv._[0];
var PASSWD = argv.p || argv.passwd || argv._[1];


cli.main(function (args, options) {
console.log("***********".rainbow);
console.log("Node Reddit".cyan);
console.log("***********\n\n".rainbow);

request('http://reddit.com/.json', function (err, res, body) {
if (!err && res.statusCode === 200) {
var reddit = JSON.parse(body),
stories = reddit.data.children.map(function (s) {
return s.data;
});

// Descending score
stories.sort(function (a, b) { return b.score - a.score; });

stories.forEach(function (story) {
var row = "",
title = story.title.length > 100
? story.title.substr(0, 100) + "..."
: story.title;

// Build row
// [score] [title] [comments] [subreddit]
// This sucks
row += story.score.toString().green + "\t";
row += title.bold
row += " (" + story.domain + ")";
row += (" /r/" + story.subreddit).cyan;
row += "\n\t";
row += story.author.grey;
row += " " + (story.num_comments + " comments").italic.yellow;
row += "\n";

console.log(row);
});
}
});
});
var apiOptions = {
uri: "http://www.reddit.com/api/login/" + USER,
user: USER,
headers: {
Host: "www.reddit.com",
Connection: "Keep-Alive"
},
form:{
user: USER,
passwd: PASSWD,
api_type: "json"
},
followAllRedirects: true
};


request.post(apiOptions, function(err, res, body){
var data = JSON.parse(body);
cookie = data.cookie;
uh = data.modhash;
apiOptions = {
uri: 'http://reddit.com/.json',
headers: {
Cookie: cookie
}
};

request(apiOptions, function (err, res, body) {
if (!err && res.statusCode === 200) {
var reddit = JSON.parse(body),
stories = reddit.data.children.map(function (s) {
return s.data;
});

// Descending score
stories.sort(function (a, b) { return b.score - a.score; });

stories.forEach(function (story) {
var row = "",
title = story.title.length > 100
? story.title.substr(0, 100) + "..."
: story.title;

// Build row
// [score] [title] [comments] [subreddit]
// This sucks
row += story.score.toString().green + "\t";
row += title.bold
row += " (" + story.domain + ")";
row += (" /r/" + story.subreddit).cyan;
row += "\n\t";
row += story.author.grey;
row += " " + (story.num_comments + " comments").italic.yellow;
row += "\n";

console.log(row);
});
}
});

});
});

4 changes: 4 additions & 0 deletions node_modules/optimist/.npmignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions node_modules/optimist/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7717018

Please sign in to comment.