Skip to content

Instantly share code, notes, and snippets.

@alessioalex
Created May 8, 2015 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alessioalex/c37a5d2481aa75826f01 to your computer and use it in GitHub Desktop.
Save alessioalex/c37a5d2481aa75826f01 to your computer and use it in GitHub Desktop.
reddit cli node app
"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() {
//...
}));
@alessioalex
Copy link
Author

package.json

{
  "name": "rdt-ftms",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "JSONStream": "^0.10.0",
    "chalk": "^1.0.0",
    "minimist": "^1.1.1",
    "ms": "^0.7.1",
    "request": "^2.55.0",
    "through": "^2.3.7"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment