-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
136 lines (122 loc) · 4.19 KB
/
index.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
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
var express = require('express');
var RSS = require('rss');
var FeedParser = require('feedparser');
var cheerio = require('cheerio');
var Q = require('q');
var http = require('http');
var dblite = require('dblite');
var db = dblite('urlstorage.db');
var app = express();
var rivvaFeedUrl = "http://feeds.feedburner.com/rivva";
var logStatus = false;
db.query('CREATE TABLE IF NOT EXISTS rivva (guid TEXT UNIQUE, url TEXT)');
app.set('port', (process.env.PORT || 5000))
app.use(express.static(__dirname + '/public'))
app.get('/', function(request, response) {
var feedItems = [];
http.get(rivvaFeedUrl, function(res) {
res.pipe(new FeedParser({}))
.on('error', function(error){
response.status(500).send("Oh snap, an error occured.");
})
.on('meta', function(meta){
feedMeta = meta;
if(logStatus) console.log("got meta...");
})
.on('readable', function(){
var stream = this, item;
while (item = stream.read()){
if(logStatus) console.log("adding feed item..." + feedItems.length);
feedItems.push(item);
}
})
.on('end', function(){
if(logStatus) console.log("got all feed items...");
var promises = [];
for(var i = 0; i < feedItems.length; i++) {
var item = feedItems[i];
promises.push(getPostTitle(i, item.guid, item.link));
}
if(logStatus) console.log("waiting for all direct links... (" + promises.length + ")");
Q.all(promises).then(function(result) {
if(logStatus) console.log("building RSS now...");
var feed = new RSS(feedMeta);
if(logStatus) console.log("going through all items...");
for(var i = 0; i < result.length; i++) {
var index = result[i].index;
var link = result[i].link;
feedItems[index].url = link;
}
for(var i = 0; i < feedItems.length; i++) {
feed.item(feedItems[i]);
}
response.setHeader('Content-Type', 'text/xml');
response.status(200).send(feed.xml());
});
});
});
});
app.listen(app.get('port'), function() {
if(logStatus) console.log("Node app is running at localhost:" + app.get('port'))
})
var getPostTitle = function(index, guid, url) {
var data = "", defer = Q.defer();
getUrlFromDatabase(guid).then(function(existingUrl) {
if(logStatus) console.log("result for db query of " + guid + ": " + existingUrl);
if(existingUrl === false) {
http.get(url, function (res) {
var content_length = parseInt(res.headers['content-length'], 10);
var total_downloaded = 0;
if (res.statusCode !== 200) {
defer.reject("HTTP Error " + res.statusCode + " for " + url);
return;
}
res.on("error", defer.reject);
res.on("data", function (chunk) {
data += chunk;
total_downloaded += chunk.length;
var percentage = Math.floor((total_downloaded / content_length) * 100);
defer.notify(percentage);
});
res.on("end", function () {
if(logStatus) console.log("getting direct link " + index + " of page " + url);
$ = cheerio.load(data);
var directLink = $("div>article>header>h1>a").attr("href");
if(logStatus) console.log("got direct link " + index + " of page " + url);
saveUrlInDatabase(guid, directLink);
defer.resolve({
index: index,
link: directLink
});
});
});
}else{
defer.resolve({
index: index,
link: existingUrl
});
}
});
return defer.promise;
}
var getUrlFromDatabase = function(guid) {
var defer = Q.defer();
db.query('SELECT url FROM rivva WHERE guid = :guid', {guid: guid}, function(err, rows) {
if(err) {
defer.reject(err);
}else{
if(rows.length == 1) {
defer.resolve(rows[0][0]);
}
defer.resolve(false);
}
});
return defer.promise;
}
var saveUrlInDatabase = function(guid, url) {
db.query('INSERT INTO rivva VALUES(:guid, :url)', {guid: guid, url: url}, function(err) {
if(err) {
if(logStatus) console.log("error when saving url for " + guid);
}
});
}