-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
327 lines (308 loc) · 11.9 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
var express = require('express');
var redis = require('redis');
var url = require('url');
var moment = require('moment');
var CronJob = require('cron').CronJob;
var Krawler = require('krawler');
var cors = require('cors');
var cheerio = require('cheerio');
var app = express();
var redisURL = url.parse(process.env.REDIS_URL);
var client = redis.createClient(redisURL.port, redisURL.hostname);
client.auth(redisURL.auth.split(":")[1]);
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.use(cors());
var job = new CronJob({
cronTime: '00 01 00 * * *',
onTick: function() {
// Runs every weekday at 00:01:00 AM.
crawl();
},
start: true,
timeZone: "Europe/Zurich"
});
job.start();
app.get('/', function(request, response) {
client.get('venues', function(err, value) {
if (err) {
response.status(404);
response.send('Error: Couldnt fetch venues off redis');
console.error('Error: Couldnt fetch venues off redis');
} else {
console.log('Read successfully: ' + moment().format('MMMM Do YYYY, h:mm:ss a'));
console.log(value);
response.json(value);
}
});
});
function crawl() {
var content = [];
var urls = [
'http://www.palace.sg/',
'http://grabenhalle.ch/',
'http://kugl.ch/',
'http://tankstell.ch/',
'http://oya-bar.ch/',
'http://treppenhaus.ch/',
'http://www.militaerkantine.ch/de/microsites/kultur/',
'http://talhof.sg/',
'http://www.flon-sg.ch/'
];
var krawler = new Krawler({
maxConnections: 10,
forceUTF8: true,
headers: {
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36'
}
});
krawler
.queue(urls)
.on('data', function($, url, response) {
var body = response.body;
content.push({"url": url, "body": body});
})
.on('error', function(err, url) {
console.log("Error crawl: " + url);
console.log(err);
content.push({"url": url, "body": ""});
})
.on('end', function() {
filterData(content);
});
}
function filterData(content) {
var venues = { "data" : [] };
content.forEach(function(entry) {
switch(entry.url) {
case 'http://www.palace.sg/':
venues.data[0] = { "name": "PALACE", "color": "stable", "title": "Keine Veranstaltung", "link": "http://www.palace.sg/", "description": ""};
break;
case 'http://grabenhalle.ch/':
venues.data[1] = grabenFilter(entry.body);
break;
case 'http://kugl.ch/':
venues.data[2] = kuglFilter(entry.body);
break;
case 'http://tankstell.ch/':
venues.data[3] = tankstellFilter(entry.body);
break;
case 'http://oya-bar.ch/':
venues.data[4] = oyaFilter(entry.body);
break;
case 'http://treppenhaus.ch/':
venues.data[5] = treppenhausFilter(entry.body);
break;
case 'http://www.militaerkantine.ch/de/microsites/kultur/':
venues.data[6] = militaerkantineFilter(entry.body);
break;
case 'http://talhof.sg/':
venues.data[7] = talhofFilter(entry.body);
break;
case 'http://www.flon-sg.ch/':
venues.data[8] = flonFilter(entry.body);
break;
}
});
writeVenues(venues);
}
function flonFilter(content) {
var $ = cheerio.load(content);
var event = { "name": "FLON", "color": "royal", "title": "Keine Veranstaltung", "link": "http://www.flon-sg.ch/", "description": ""};
if(content !== ""){
$('#events').filter(function(){
var data = $(this).children().first();
var infos = data.children().first().text();
var eventDate = infos.match(/\d{1,}\.\d{1,}\.\d{1,}/)[0];
var actualDate = moment().locale('de').format("L");
if(eventDate === actualDate) {
event.title = data.children().eq(1).children().first().text();
event.link = "http://www.flon-sg.ch/" + data.children().eq(1).children().first().attr('href');
//todo description anpassen
event.description = data.children().eq(2).text();
} else {
event.link = "http://www.flon-sg.ch/";
event.title = "Keine Veranstaltung";
}
});
}
return event;
}
function talhofFilter(content) {
var $ = cheerio.load(content);
var event = { "name": "TALHOF", "color": "balanced", "title": "Keine Veranstaltung", "link": "http://talhof.sg/", "description": ""};
if(content !== ""){
$('#events').filter(function(){
var data = $(this).children().first();
var infos = data.children().first().text();
var eventDate = infos.match(/\d{1,}\.\d{1,}\.\d{1,}/)[0];
var actualDate = moment().locale('de').format("L");
if(eventDate === actualDate) {
event.title = data.children().eq(1).children().first().text();
event.link = "http://talhof.sg" + data.children().eq(1).children().first().attr('href');
//todo description anpassen
event.description = data.children().eq(2).text();
} else {
event.link = "http://talhof.sg/";
event.title = "Keine Veranstaltung";
}
});
}
return event;
}
function militaerkantineFilter(content) {
var $ = cheerio.load(content);
var event = { "name": "MILITÄRKANTINE", "color": "stable", "title": "Keine Veranstaltung", "link": "http://www.militaerkantine.ch/de/microsites/kultur/", "description": ""};
if(content !== ""){
$('#infoseite').filter(function(){
var data = $(this).children().eq(1);
var infos = data.children().eq(1).children().first().text();
var eventDate = infos.match(/\d{2}\.\s\w{1,}\s\d{4}/i)[0];
var actualDate = moment().locale('de').format("LL");
event.link = "http://www.militaerkantine.ch/de/microsites/kultur/";
if(eventDate === actualDate) {
event.title = data.children().eq(2).children().first().text();
event.description = infos.replace(/\w{2},\s\d{2}\.\s\w{1,}\s\d{4}/, '').trim();
} else {
event.title = "Keine Veranstaltung";
}
});
}
return event;
}
function treppenhausFilter(content) {
var $ = cheerio.load(content);
var event = { "name": "TREPPENHAUS", "color": "assertive", "title": "Keine Veranstaltung", "link": "http://treppenhaus.ch/", "description": ""};
if(content !== ""){
$('main.content').filter(function(){
var data = $(this).children().eq(2).children().first();
var day = data.children().first().children().eq(1).children().first().text() + ". ";
var month = $(this).children().eq(1).text().replace(':', '') + " ";
var year = moment().format("YYYY");
var eventDate = day + month + year;
var actualDate = moment().locale('de').format("LL");
if(eventDate === actualDate) {
event.title = data.children().eq(1).children().eq(1).children().first().text().trim();
event.link = data.attr('href');
//todo description anpassen
event.description = "";
} else {
event.link = "http://treppenhaus.ch/";
event.title = "Keine Veranstaltung";
}
});
}
return event;
}
function kuglFilter(content) {
var $ = cheerio.load(content);
var event = { "name": "KUGL", "color": "calm", "title": "Keine Veranstaltung", "link": "http://kugl.ch/", "description": ""};
if(content !== ""){
$('.event-item-holder').filter(function(){
var data = $(this).children().first().children().first().children();
var day = data.first().children().eq(1).text();
var month = data.first().children().eq(3).text();
var year = moment().format("YYYY");
var eventDate = month + " " + day + ", " + year;
var actualDate = moment().format("ll");
if(eventDate === actualDate) {
event.title = data.eq(1).children().first().text();
event.link = data.eq(1).children().first().attr('href');
event.description = data.eq(2).text();
} else {
event.link = "http://kugl.ch/";
event.title = "Keine Veranstaltung";
}
});
}
return event;
}
function grabenFilter(content) {
var $ = cheerio.load(content);
var event = { "name": "GRABENHALLE", "color": "positive", "title": "Keine Veranstaltung", "link": "http://grabenhalle.ch/", "description": ""};
if(content !== ""){
$('#content').filter(function(){
var data = $(this).children().eq(2).children().first().children().first().children().first().children().first().children().eq(1).children();
var infos = data.first().text();
var eventDate = infos.match(/\d{1,}\.\s\w{1,}/i)[0] + " " + moment().format("YYYY");
var actualDate = moment().locale('de').format("LL");
if(eventDate === actualDate) {
event.title = data.eq(5).children().first().text();
event.link = "http://grabenhalle.ch" + data.eq(5).attr('href');
var descriptionTable = data.find('table').first().children();
var description = descriptionTable.first().children().first().text();
description += " " + descriptionTable.first().children().eq(1).text();
description += " " + descriptionTable.eq(1).children().first().text();
description += " " + descriptionTable.eq(1).children().eq(1).text();
description += " " + descriptionTable.eq(2).children().first().text();
description += " " + descriptionTable.eq(2).children().eq(1).text();
event.description = description;
} else {
event.link = "http://grabenhalle.ch/";
event.title = "Keine Veranstaltung";
}
});
}
return event;
}
function tankstellFilter(content) {
var $ = cheerio.load(content);
var event = { "name": "TANKSTELL", "color": "balanced", "title": "Keine Veranstaltung", "link": "http://treppenhaus.ch/", "description": ""};
if(content !== ""){
$('#content').filter(function(){
var data = $(this).children().first();
var infos = data.children().first().text();
var array = infos.match(/\|\s.{1,}\|/i);
var replace1 = array[0].replace('| ','');
var eventDate = replace1.replace(' |', '');
var actualDate = moment().locale('de').format("LL");
event.link = "http://tankstell.ch/";
if(eventDate === actualDate) {
event.title = data.children().eq(1).text();
var description = infos.match(/\|\s\d{4}.{1,}/i)[0].slice(2);
event.description = description;
} else {
event.link = "http://treppenhaus.ch/";
event.title = "Keine Veranstaltung";
}
});
}
return event;
}
function oyaFilter(content) {
var $ = cheerio.load(content);
var event = { "name": "ØYA", "color": "energized", "title": "Keine Veranstaltung", "link": "http://oya-bar.ch/", "description": ""};
if(content !== ""){
$('#events_container').filter(function(){
var data = $(this).children().first().children();
var infos = data.eq(1).text();
var eventDate = infos.match(/\d{2}\.\s\w{1,}/i)[0] + " " + moment().format("YYYY");
var actualDate = moment().locale('de').format("LL");
if(eventDate === actualDate) {
event.link = "http://oya-bar.ch/" + data.eq(2).children().first().attr('href');
event.title = data.eq(2).children().first().text();
event.description = infos.match(/\d{2}\:\d{2}/i) + " Uhr";
} else {
event.link = "http://oya-bar.ch/";
event.title = "Keine Veranstaltung";
}
});
}
return event;
}
function writeVenues(venues) {
client.del('venues');
var jsonString = JSON.stringify(venues);
console.log(jsonString);
client.set('venues', jsonString , function(err) {
if (err) {
// Something went wrong
console.error('Error: Couldnt write venues to redis');
} else {
console.log('Write successfully: '+ moment().format('MMMM Do YYYY, h:mm:ss a'));
}
});
}
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});