unific-cli
Version:
Command Line Interface on http://unific.net
138 lines (114 loc) • 3.55 kB
JavaScript
// Generated by CoffeeScript 1.6.3
/*
# Module dependencies.
*/
var async, cnumber, color, colors, googl, moment, nourl, program, render, showfeed, stream, titles, updated, _,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
program = require("commander");
_ = require('underscore');
async = require('async');
moment = require('moment');
colors = require('colors');
googl = require('goo.gl');
/*
# Module Configration
*/
colors.setTheme({
time: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'blue'
});
color = ["white", 'yellow', 'cyan', 'magenta', 'red', 'green', 'blue'];
cnumber = 0;
/*
# Main Script
*/
program.version("0.2.0").option("-s, --stream [value]", "select Streamname(e.g.'nikezono')").option("-u, --nourl [Bool]", "Don't show articles url").option("-f, --feed [Bool]", "show feedname").parse(process.argv);
updated = (new Date()).getTime() - 1000 * 60 * 60;
titles = [];
stream = program.stream;
nourl = program.nourl != null;
showfeed = program.feed != null;
process.nextTick(function() {
var client, socket;
if (!stream) {
return console.log("Please input stream name.exit...");
}
/* socket.io Configration*/
client = require('socket.io-client');
socket = client.connect("http://unific.net");
return socket.on('connect', function() {
console.log("Add Stream '" + stream.blue + "'. Please Wait for Sync");
socket.emit('sync stream', {
stream: stream,
latest: updated
});
socket.on('sync completed', function(pages) {
var sorted, uniqued;
if (pages.length > 0) {
uniqued = _.filter(pages, function(article) {
var _ref;
if (_ref = article.page.title, __indexOf.call(titles, _ref) >= 0) {
return false;
} else {
return true;
}
});
if (_.isEmpty(uniqued)) {
return;
}
sorted = _.sortBy(uniqued, function(article) {
return Date.parse(article.page.pubDate);
});
updated = Date.parse(_.last(sorted).page.pubDate);
_.map(sorted, function(article) {
return titles.push(article.page.title);
});
return render(sorted);
}
});
return setInterval(function() {
return socket.emit('sync stream', {
stream: stream,
latest: updated
});
}, 1000 * 60);
});
});
render = function(sorted) {
return async.eachSeries(sorted, function(article, callback) {
var date, seed, site, title, url;
seed = parseInt(article.feed._id) + article.feed.title.length;
cnumber = seed % color.length;
date = "[" + (moment(article.page.pubDate).format("(ddd) HH:mm")) + "]";
title = article.page.title;
site = article.feed.title.split(' - ')[0];
url = article.page.url;
return async.series([
function(cb) {
return googl.shorten(url, function(shorter) {
if (shorter.id) {
url = shorter.id;
}
return cb();
});
}, function(cb) {
var text;
text = "" + date.time + " " + title[color[cnumber]];
if (showfeed) {
text = text + (" - " + site);
}
if (!nourl) {
text = text + (" " + url.underline);
}
return setTimeout(function() {
console.log(text);
return cb();
}, 100);
}
], function() {
return callback();
});
}, function() {});
};