Skip to content

Instantly share code, notes, and snippets.

@shershen08
Created March 5, 2016 10:19
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 shershen08/11a740dfe63dad994ca4 to your computer and use it in GitHub Desktop.
Save shershen08/11a740dfe63dad994ca4 to your computer and use it in GitHub Desktop.
A simple RSS Reader with Readline in NodeJS
/*
Readline module is used to read and write data in console.
Full docs on Readline - https: //nodejs.org/api/readline.html
In this example the list of RSS channes from Yandex.news - https: //news.yandex.ru/export.html
To call the script you should use type: 'node --harmony cmd.js'
*/
// Adding needed modules:
// readline - reads console ine
// request - does http resquests
// parser - parses the responses
const readline = require('readline');
const r = require('request');
const parser = require('/usr/local/lib/node_modules/xml2json/');
// Script variables:
// currentState - flag showing what news type to show
// items - list of news in the RSS
// rsss - list of RSS litst
var currentState = 'general';
var items = [];
const rsss = [{
'title': 'Hardware',
'url': 'https://news.yandex.ru/hardware.rss'
}, {
'title': 'Science',
'url': 'https://news.yandex.ru/science.rss'
}, {
'title': 'Finances',
'url': 'https://news.yandex.ru/finances.rss'
}, {
'title': 'Society',
'url': 'https://news.yandex.ru/society.rss'
}, {
'title': 'Internet',
'url': 'https://news.yandex.ru/internet.rss'
}, {
'title': 'Netherlands',
'url': 'https://news.yandex.ru/Netherlands/index.rss'
}, {
'title': 'Football',
'url': 'https://news.yandex.ru/football.rss'
}, {
'title': 'Law',
'url': 'https://news.yandex.ru/law.rss'
}];
//init Readline interface
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
//Below there are several handlers
// - loadChannel, channelAnswers, generalAnswers, loadGeneral
// their names should be pretty self-explanatory
var loadChannel = function(e) {
currentState = 'channel';
var ch = JSON.parse(e).rss;
items = ch.channel.item;
console.log('|| ', ch.channel.title);
items.forEach((x, i) => {
console.log((i + 1) + ' - ' + x.title);
});
console.log('>> Select item to read (0 - for level up)');
}
var channelAnswers = function(answer) {
if (!items[answer]) {
console.log('Wrong item number');
} else {
console.log('|| ', items[(answer - 1)].title);
console.log(items[(answer - 1)].description);
console.log('>> Enter next command...');
}
}
var generalAnswers = function(answer) {
if (!rsss[answer]) {
console.log('Wrong channel number');
}
r(rsss[(answer - 1)].url, function(error, response, body) {
if (!error && response.statusCode == 200) {
loadChannel(parser.toJson(body));
}
})
}
var loadGeneral = function() {
console.log('Please select channel to read out of ' + rsss.length + '\n');
rsss.forEach((x, i) => {
console.log((i + 1) + ' - ' + x.title);
});
currentState = 'general';
console.log('>> Enter id from 1 to ' + rsss.length + ':');
}
//Main Readline event handler
rl.on('line', (answer) => {
if (answer == 0) {
loadGeneral();
return;
}
//quit it user presses 'x' or 'q'
if (answer == 'x' || answer == 'q') {
console.log('Bye!');
process.exit(0);
}
(currentState == 'general') ? generalAnswers(answer): channelAnswers(answer);
});
/*
This is relatively simple example.You can extend this one or use one of the advanced modules working with cli in NodeJS.
For example this: https: //github.com/chriso/cli
*/
//JS module syntax - default function to call when module file in called
module.exports.default = loadGeneral();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment