In this example the list of RSS channels from Yandex.news
To call the script you should use type: node --harmony cmd.js
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
/* | |
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(); |