-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhnService.js
61 lines (49 loc) · 1.44 KB
/
hnService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use strict';
var http = require('https');
var cheerio = require('cheerio');
var host = 'news.ycombinator.com';
var cache = {}; //Need to impliment.
var addToCache = function(data, uri){
cache[uri] = {};
cache[uri].time = new Date().getTime();
cache[uri].data = data;
};
var parsePage = function(data, uri){
var $ = cheerio.load(data);
var items = [];
$('td.title > a').each(function(k){
var $this = $(this);
if ($this.text().toLowerCase() === 'more') return;
var item = {
id: k<9 ? (' ' + (k+1) + '. ') : ((k+1) + '. '),
title: $this.text() || '',
href: $this.attr('href') || '',
points: $this.parent('td').parent('tr').next('tr').children('td:nth-child(2)').children('span').html() || '0 points'
};
items.push(item);
});
addToCache(items, uri);
return items;
};
exports.getHackerNews = function(uri, callback){
var options = {
host: host,
path: '/' + uri,
port: 443
};
var request = function(response) {
var str = '';
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
var items = parsePage(str, uri);
callback(items);
});
};
if (cache[uri] && cache[uri].data){
callback(cache[uri].data);
}else{
http.request(options, request).end();
}
};