-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_bk.js
128 lines (109 loc) · 4 KB
/
server_bk.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
var CONFIG = require('./public/js/config.js');
var http = require('http');
var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var assert = require('assert');
var MongoClient = require('mongodb').MongoClient;
var url = CONFIG.DB_URL + CONFIG.DB_NAME;
var app = express();
app.set('port', process.env.OPENSHIFT_NODEJS_PORT || 8080);
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.get('/about', function (req, res) {
res.sendFile(__dirname + '/about.html');
});
app.post('/channels', function (req, res) {
MongoClient.connect(url, function(err, db) {
//assert.equal(null, err);
if (!err) {
console.log("Connected correctly to server to find channels");
var collection = db.collection('channels');
collection.find({}).toArray(function(err, result) {
parseChannels(result, res, req.body);
db.close();
});
} else {
res.json( {success: false } );
}
});
});
http.createServer(app).listen(app.get('port'), process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1" , function(){
console.log('Express server listening on port ' + app.get('port'));
});
function parseChannels(channels, res, rbody) {
console.log(channels);
MongoClient.connect(url, function(err, db) {
//assert.equal(null, err);
if (!err) {
console.log("Connected correctly to server to find entries");
var collection = db.collection('entries');
// If search for single day make it search from 00:00 to 00:00
if ( rbody.date_from == rbody.date_to ) {
var pub_query = {
$gte: new Date(rbody.date_from),
$lte: new Date(new Date(rbody.date_from).setHours(24))
};
} else {
var pub_query = {
$gte: new Date(rbody.date_from),
$lte: new Date(rbody.date_to)
};
}
channels.forEach(function(c, i) {
collection.find({
pub_date: {
$gte: new Date(rbody.date_from),
$lte: new Date(new Date(rbody.date_to).setHours(24))
},
source: c._id,
$where: function() { return (obj.title.toLowerCase().indexOf(rbody.word1.toLowerCase()) != -1 || obj.title.toLowerCase().indexOf.indexOf(rbody.word2.toLowerCase()) != -1 ) }
} ).sort({pub_date: -1}).toArray(function(err, result) {
console.log(' FIND FOR ' + c._id);
if( !result || result.length <= 1) {
console.log('NO RESULT FOR ' + c._id);
//console.log(result);
var q = {
pub_date: {
$gte: new Date(rbody.date_from),
$lte: new Date(new Date(rbody.date_to).setHours(24))
},
source: c._id,
$where: function() { return (obj.title.toLowerCase().indexOf(rbody.word1.toLowerCase()) != -1 || obj.title.toLowerCase().indexOf.indexOf(rbody.word2.toLowerCase()) != -1 ) }
};
//console.log(q);
}
channels[i]['word1'] = [];
channels[i]['word2'] = [];
if ( result ) {
// Push word to respective channel
result.forEach( function(r, ind){
if ( r.title.toLowerCase().indexOf(rbody.word1.toLowerCase()) != -1 ) {
channels[i]['word1'].push(r);
}
if ( r.title.toLowerCase().indexOf(rbody.word2.toLowerCase()) != -1 ) {
channels[i]['word2'].push(r);
}
});
}
if ( i == channels.length - 1) {
// this timeout ensures callstack is finished
setTimeout(function(){
console.log('Close DB Connection');
res.json( {success: true, results: channels} );
db.close();
}, 1000);
}
});
});
} else {
res.json( {success: false } );
}
});
};