-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
194 lines (172 loc) · 4.84 KB
/
Copy pathapp.js
File metadata and controls
194 lines (172 loc) · 4.84 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
var express = require('express'),
app = express(),
jsdom = require("jsdom").jsdom,
async = require("async"),
port = Number(process.env.PORT || 3000),
config = {
host : 'https://news.ycombinator.com/',
};
// Async function, recieves object and loops asynchronously by callling "loopFunction"
var async = function(o){
var i = 0,
length = o.length,
url = "";
var loop = function(url){
i++;
if(i === o.length){
o.callback();
return;
}
o.loopFunction(loop, i, url);
}
loop("");
}
var helper = {
getNews : function(document){
var news = [],
titles = document.querySelectorAll('td.title:not([align="right"])'),
details = document.querySelectorAll('td.subtext'),
timeRegex = /\d+\s\w+\s+ago/,
commentsRegex = /\d+\s+comments/,
anchor,
comhead;
for(var i = 0; i < titles.length; i++){
if(i === 30){
break;
}
anchor = titles[i].querySelector("a") || "";
comhead = titles[i].querySelector("span") || "";
var obj = {
title : anchor === "" ? "" : anchor.textContent,
url : anchor.getAttribute("href") || "",
comhead : comhead === "" ? "" : comhead.textContent.slice(2, -2)
};
news.push(obj);
}
for(var i = 0; i < news.length; i++){
if(details[i].querySelector('span')){
news[i].points = details[i].querySelector('span').textContent.split(' ')[0];
}else{
news[i].points = 0;
}
// Get author
if(details[i].querySelector('a[href^="user"]')){
news[i].author = details[i].querySelector('a[href^="user"]').textContent;
}else{
news[i].author = "";
}
// Get post id
if(details[i].querySelector('a[href^="item"]')){
news[i].postUrl = config.host + details[i].querySelector('a[href^="item"]').getAttribute("href");
}else{
news[i].postUrl = "";
}
// Get time
news[i].time = details[i].innerHTML.match(timeRegex) || "";
if(news[i].time !== ""){
news[i].time = news[i].time[0];
}
// Get Comments
news[i].commentCount = details[i].innerHTML.match(commentsRegex) || 0;
if(news[i].commentCount !== 0){
news[i].commentCount = news[i].commentCount[0].split(' ')[0];
}
}
return news;
},
getNextUrl : function(document){
// Using document.querySelector('a[href^="/x"]').getAttribute("href"); doesn't work because first elementURL is news2
// Fallback: traverse each anchor and look for a node with "More"
var anchors = document.querySelectorAll('a');
for(var i = 0; i < anchors.length; i++){
if(anchors[i].textContent === "More"){
//console.log("attr: "+anchors[i].outerHTML);
return anchors[i].getAttribute("href").slice(1);
}
}
return "";
}
}
app.get('/', function(req, res){
res.type('application/json');
res.send(JSON.stringify({
name: "HackerNewsAPI",
description: "Hacker News API",
version: "0.0.1"
}, null, '\t'));
});
app.get('/news', function(req, res){
jsdom.env(config.host, function(err, window){
var news = helper.getNews(window.document);
res.type('application/json');
res.send(JSON.stringify(news, null, '\t'));
});
});
app.get(/^\/news\/(\d+)$/, function(req, res){
var pages = parseInt(req.params[0]),
url = '',
news = [];
if(pages === 1){
res.redirect('/news');
return;
}
// Traverse asynchronously *pages times calling loopFunction, when finished do callback
async({
length : pages+1,
// Get DOM from url and concatenates it to news array
loopFunction: function(loop, i, url){
console.log("llamo dom: "+config.host+url);
jsdom.env(config.host+url, function(err, window){
news = news.concat(helper.getNews(window.document));
url = (i === 1 ? 'news2' : helper.getNextUrl(window.document) );
console.log("Iteration " + i + " nexturl: " + url);
loop(url);
});
},
callback: function(){
// Done
res.type('application/json');
res.send(JSON.stringify(news, null, '\t'));
}
});
});
app.get(/^\/page\/(\d+)$/, function(req, res){
// Get x page
var pages = parseInt(req.params[0]),
url = '',
news = [];
if(pages === 1){
res.redirect('/news');
return;
}else if(pages > 20){
res.type('aplication/json');
res.send(JSON.stringify([{"error":"current limit of pages is 20"}], null, '\t'));
return;
}
/*
TODO same as pages (getting next urls) but wihtout concatenating arrays
*/
async({
length : pages+1,
// Get DOM from url and concatenates it to news array
loopFunction: function(loop, i, url){
console.log("llamo dom: "+config.host+url);
jsdom.env(config.host+url, function(err, window){
if(i === pages){
news = helper.getNews(window.document);
}
url = (i === 1 ? 'news2' : helper.getNextUrl(window.document) );
console.log("Iteration " + i + " nexturl: " + url);
loop(url);
});
},
callback: function(){
// Done
res.type('application/json');
res.send(JSON.stringify(news, null, '\t'));
}
});
});
app.listen(port, function(){
console.log("Listening on " + port);
});