Created
May 8, 2015 09:04
-
-
Save alessioalex/c37a5d2481aa75826f01 to your computer and use it in GitHub Desktop.
reddit cli node app
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
"use strict"; | |
var growl = require('growl'); | |
var request = require('request'); | |
var argv = require('minimist')(process.argv.slice(2)); | |
var JSONStream = require('JSONStream'); | |
var assert = require('assert'); | |
var through = require('through'); | |
var ms = require('ms'); | |
var chalk = require('chalk'); | |
var ROOT = "https://www.reddit.com"; | |
var limit = (argv.limit !== '0' && argv.limit) ? parseInt(argv.limit) : Infinity; | |
var category = argv.cat || 'new'; | |
assert(argv.sub, "subreddit required"); | |
console.log(); | |
request(ROOT + "/r/" + argv.sub + "/" + category + ".json") | |
.pipe(JSONStream.parse("*.children.*.data")) | |
.pipe(through(function write(data) { | |
if (argv.filter) { | |
var terms = []; | |
if (argv.filter.indexOf('+') !== -1) { | |
Array.prototype.push.apply(terms, argv.filter.split('+')); | |
} else { | |
terms.push(argv.filter); | |
} | |
for (var i = 0; i < terms.length; i++) { | |
if (data.title.toLowerCase().indexOf(terms[i].toLowerCase()) === -1) { | |
return; | |
} | |
} | |
} | |
var ago = (Date.now() - data.created_utc * 1000); | |
if (argv.since) { | |
var since = ms(argv.since); | |
if (ago > since) { | |
return; | |
} | |
} | |
var obj = { | |
title: data.title, | |
permalink: data.permalink, | |
created: data.created_utc, | |
ago: ago | |
}; | |
if (limit > 0) { | |
console.log(('Title | ' + obj.title).split('').map(function() { return '-'; }).join('')); | |
console.log('Title | ' + obj.title); | |
console.log('Ago | ' + ms(obj.ago)); | |
limit--; | |
} | |
}, function end() { | |
//... | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
package.json