Skip to content

Commit

Permalink
Apply latest features from official HN API
Browse files Browse the repository at this point in the history
Best feature of all is `descendants` for total comments count
  • Loading branch information
cheeaun committed Mar 8, 2015
1 parent 3282fdf commit 50ab03d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 37 deletions.
54 changes: 24 additions & 30 deletions lib/hnapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var extend = require('extend');
var url = require('url');

var hn = new Firebase('https://hacker-news.firebaseio.com/v0');
var hnTopStories = hn.child('topstories');
var hnRecentItems = hn.child('updates/items');

var typeMapping = {
Expand All @@ -22,7 +21,7 @@ var cleanText = function(html){

var api = {

news: function(options, fn){
stories: function(base, options, fn){
var opts = extend({
page: 1
}, options);
Expand All @@ -31,8 +30,8 @@ var api = {
var startIndex = (page-1) * limit;
var endIndex = startIndex + limit;

var top = hnTopStories.limitToFirst(limit * page);
top.once('value', function(snapshot){
var stories = hn.child(base).limitToFirst(limit * page);
stories.once('value', function(snapshot){
// Grab all items from the IDs
var items = snapshot.val().slice(startIndex, endIndex);
var itemFetches = items.map(function(itemID){
Expand All @@ -49,9 +48,7 @@ var api = {
// Throw them all into an array
Promise.all(itemFetches).then(function(res){
var apiRes = res.map(function(item){

// TODO: show the REAL comments count
var commentsCount = item.kids ? item.kids.length : 0;
var commentsCount = item.descendants || 0;

var output = {
id: item.id,
Expand All @@ -76,6 +73,11 @@ var api = {
output.user = output.points = null;
}

// Identify type=ask
if (item.type == 'story' && output.url.match(/^item/i) && item.title.match(/^ask/i)){
output.type = 'ask';
}

return output;
});

Expand All @@ -86,32 +88,24 @@ var api = {
});
},

news2: function(fn){
api.news({ page: 2 }, fn);
news: function(options, fn){
api.stories('topstories', options, fn);
},

newest: function(fn){ // Not-so-complete 'newest'
var recent = hnRecentItems.limitToFirst(30);
recent.once('value', function(snapshot){
var items = snapshot.val();
var itemFetches = items.map(function(itemID){
return new Promise(function(resolve, reject){
var item = hn.child('item/' + itemID);
item.once('value', function(snap){
resolve(snap.val());
}, function(err){
reject(err);
});
});
});
newest: function(options, fn){
api.stories('newstories', options, fn);
},

Promise.all(itemFetches).then(function(res){
var stories = res.filter(function(r){
return r.type == 'story';
});
fn(null, stories);
});
});
ask: function(options, fn){
api.stories('askstories', options, fn);
},

show: function(options, fn){
api.stories('showstories', options, fn);
},

jobs: function(options, fn){
api.stories('jobstories', options, fn);
},

newComments: function(fn){ // Not-so-complete 'newComments' too
Expand Down
17 changes: 10 additions & 7 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,19 @@ request.on('error', function(e){
if (e) winston.error(e);
});

app.get(/^\/(news|news2)$/, function(req, res){
var cacheKey = req.params[0];
app.get(/^\/(news|news2|newest|ask|show|jobs)$/, function(req, res){
var base = req.params[0];
var page = req.query.page;
if (cacheKey == 'news') cacheKey += page;
if (base == 'news2'){ // Totally ignore `page` if `news2`
base = 'news';
page = 2;
}
var cacheKey = base + page;
cache.get(cacheKey, function(err, result){
if (result){
res.jsonp(result);
} else {
if (cacheKey == 'news2') page = 2;
hnapi.news({
hnapi[base]({
page: page
}, function(err, data){
if (err){
Expand All @@ -306,12 +309,12 @@ app.get(/^\/(news|news2)$/, function(req, res){
});

// If 'news' expired, 'news2' should expire too
if (cacheKey == 'news') cache.del('news2');
if (cacheKey == 'news' || cacheKey == 'news1') cache.del('news2');
}
});
});

app.get(/^\/(newest|ask|show|shownew|best|active|noobstories)$/, function(req, res){
app.get(/^\/(shownew|best|active|noobstories)$/, function(req, res){
var cacheKey = req.params[0];
var page = req.query.page;
cache.get(cacheKey, function(err, result){
Expand Down

0 comments on commit 50ab03d

Please sign in to comment.