forked from OpenNewsLabs/nomo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
85 lines (74 loc) · 1.95 KB
/
app.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
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
var http = require('http');
var request = require('request');
var simplexml = require('xml-simple');
var publicApi = [];
function scrape(title, url){
var amoUrl = 'http://localhost:1337/?q=' + url
request(amoUrl, function (err, res, body){
if(body !== undefined){
var obj = JSON.parse(body);
obj.title = title;
obj.url = url;
//console.log(obj);
publicApi.push(obj);
}
});
}
function delquote(str){
return (str=str.replace(/["']{1}/gi,""));
}
function getStory(url){
var options = {
host: 'xml.zeit.de',
port: 80,
path: url.split('http://xml.zeit.de')[1]
}, body = "";
var req = http.request(options, function(res) {
res.setEncoding('binary');
if(res.statusCode === 200){
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
simplexml.parse(body, function(e, p) {
var newstr = p.body.title.replace("’", "");
var noHyphenString = newstr.replace("–", "-");
var noSlash = noHyphenString.replace("“", '"');
console.log(noSlash)
scrape(noSlash, url.replace("xml.", "www."));
});
});
}
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.end();
}
function init(){
publicApi = [];
request('http://xml.zeit.de/index', function (err, res, body){
if(!err && res.statusCode === 200){
simplexml.parse(body, function(e, p) {
var parsed = p.feed.reference;
for( i = 0; i < parsed.length; i++ ){
getStory(parsed[i]['@'].href);
}
});
}
})
}
function main(){
setInterval(function(){
init();
}, 600000);
init();
}
main();
http.createServer(function (req, res) {
res.writeHead(200, {
'Content-Type': 'application/json;charset=ISO-8859-1',
'Access-Control-Allow-Origin' : '*'
});
res.end(JSON.stringify(publicApi), 'binary');
}).listen(1338, '0.0.0.0');