Last active
October 10, 2016 23:40
-
-
Save melvincarvalho/89249baada389580a1a91d2f6c8d8f85 to your computer and use it in GitHub Desktop.
hn.js
This file contains hidden or 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
#!/usr/bin/env node | |
var debug = require('debug')('hn') | |
var request = require('request') | |
debug('starting hn') | |
var config = {} | |
config.defaultStories = 5 | |
var topURI = 'https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty' | |
var maxStories = process.argv[2] || config.defaultStories | |
/** | |
* Get item URI from item number | |
* @param {number} item The item number | |
* @return {string} The item URI | |
*/ | |
function getItemURI(item) { | |
var ret = 'https://hacker-news.firebaseio.com/v0/item/' + item + '.json?print=pretty' | |
return ret | |
} | |
/** | |
* Get a story from a story URI | |
* @param {String} storyURI The story URI | |
*/ | |
function printStory(storyURI) { | |
request(storyURI, function(error, response, body) { | |
if (!error && response.statusCode === 200) { | |
var itemJSON = JSON.parse(body) | |
console.log(itemJSON.url) | |
console.log(itemJSON.score) | |
console.log(itemJSON.title) | |
console.log() | |
debug(body) | |
} | |
}) | |
} | |
/** | |
* main function | |
*/ | |
function main() { | |
request(topURI, function(error, response, body) { | |
if (!error && response.statusCode === 200) { | |
var topJSON = JSON.parse(body) | |
for (var i = 0; i < maxStories; i++) { | |
var story = topJSON[i] | |
var storyURI = getItemURI(story) | |
printStory(storyURI) | |
} | |
} | |
}) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment