Skip to content

HN script updates (using official API, CLI args support) #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"esnext": true,
"laxcomma": true,
"node": true
}
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,33 @@ Before installation you will need node/npm installed.


sudo npm install -g hacker-news-cli

##Usage

The global installation will symlink an executable script and place it in your PATH. To use with hacker news simply type:

hn


or use `--help` to show available options:

hn --help

A command line tool to print out the latest posts on Hacker News to your terminal.

Usage
$ hn <option> [parameters]

Examples of usage
$ hn --limit 10

Options
--limit n # Displays first n hottest posts. Default: 29
--version # Displays package version.

or for designer news:

dn

You will then be prompted to open a post. Type the number of the post to open a post or type 0 to quit and return to your terminal session.

##Version history
Expand Down
77 changes: 54 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,71 @@
#!/usr/bin/env node

var FeedParser = require('feedparser')
, request = require('request')
, async = require('async')
, posts = []
, hackerNews = require("node-hacker-news")()
, colors = require('colors')
, prompt = require('prompt')
, exec = require('child_process').exec
, platform = require('os').platform()
, i = 1;
, meow = require('meow')
, platform = require('os').platform();

const shellOpenCommand = {
'win32': 'start ',
'linux': 'xdg-open ',
'darwin': 'open '
}[platform];

request('https://news.ycombinator.com/rss')
.pipe(new FeedParser())
.on('error', function(error) {
console.log("An error occured");
})
.on('readable', function () {
var stream = this, item;
if(i < 29){
while (item = stream.read()) {
posts.push(item);
console.log(i.toString().red + ". " + item.title);
i++;
}
}
})
.on('finish', function(){
promptForPost();
const defaults = {
'limit': 29
};

const hnUrls = {
'item': 'https://news.ycombinator.com/item?id='
};

var cli = meow({
help: [
'Usage',
' $ hn <option> [parameters]',
'',
'Examples of usage',
' $ hn --limit 10',
'',
'Options',
' --limit n # Displays first n hottest posts. Default: 29',
' --version # Displays package version.',
]
});

hackerNews.getHottestItems(getLimit(), function(err, items) {
if (err) {
throw err;
}

posts = items.slice();
async.each(posts, printPostTitle, promptForPost);
});

function getLimit() {
return isNaN(cli.flags.limit)
|| !cli.flags.limit
? defaults.limit
: cli.flags.limit;
}

function printPostTitle(post, next) {
console.log((posts.indexOf(post) + 1).toString().red + ". " + post.title);
next();
}

function openPost(post) {
var url = post.url.length ? post.url : hnUrls.item + post.id;

exec(shellOpenCommand + url, function(error) {
if(error) throw error;
});
}

function promptForPost() {
prompt.start();
Expand All @@ -52,9 +85,7 @@ function promptForPost() {
if(isNaN(i) || i > posts.length || i < 1) {
console.log("Invalid post number");
} else {
exec(shellOpenCommand + posts[i - 1].link, function(error){
if(error) throw error;
});
openPost(posts[i - 1]);
}
promptForPost();
}
Expand Down
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"preferGlobal": true,
"bin": {
"hn": "./index.js",
"dn": "./designer.js"
Expand All @@ -20,9 +21,12 @@
"url": "https://github.com/mtharrison/hackernews/issues"
},
"dependencies": {
"request": "~2.27.0",
"feedparser": "~0.16.2",
"async": "^1.3.0",
"colors": "~0.6.2",
"prompt": "~0.2.11"
"feedparser": "~0.16.2",
"meow": "^3.3.0",
"node-hacker-news": "^0.1.1",
"prompt": "~0.2.11",
"request": "~2.27.0"
}
}