-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
121 lines (96 loc) · 3.39 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
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
var express = require('express');
const entry = require('./crawler/stockholm/crawler');
const schedule = require('node-schedule');
const fs = require('fs');
var app = express();
app.use(function (req, res, next) {
const allowedOrigins = [
'http://localhost:4200',
'http://localhost:4201',
'https://crawler.globati.com',
'https://admin.globati.com',
'https://crawler.globati.com/crawled-event',
'https://crawler.globati.com'
];
const origin = req.headers.origin;
if (allowedOrigins.includes(origin)) {
res.header("Access-Control-Allow-Origin", origin); // restrict it to the required domain
}
console.log('Origin: '+origin);
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', origin);
res.header('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.header('Access-Control-Allow-Credentials', true);
return next();
});
const eventsUrl = 'parsed-data/stockholm-events.json';
var j = schedule.scheduleJob('16 21 * * *', function(){
console.log('Starting web crawler...');
entry.startCrawler();
});
app.listen(8081);
app.get('/crawled-event/:city', (req, res) => {
let city = req.params.city.toLowerCase();
if(city === 'stockholm') {
let events = fs.readFile(eventsUrl, (err, data) => {
if (err) throw err;
const response = JSON.parse(data).splice(0, 80);
res.send(response);
});
}
});
app.get('/crawled-event/noeventsmessage/:city', (req, res) => {
let city = req.params.city.toLowerCase();
let message = `Sorry! The automatic events listing service is not running in ${city} yet, but you can check the site below for events and add them manually to your globati page.`;
let noevents = null;
if( city=== 'stockholm'){
noevents = {
"message": message,
"link": "https://www.visitstockholm.com/events/"
}
}
if( city === 'amsterdam' ){
noevents = {
"message": message,
"link":"https://www.iamsterdam.com/en/see-and-do/whats-on/monthly-event-calendar"
};
}
else if(city === 'oslo'){
noevents = {
"message":message,
"link":'https://www.visitoslo.com/en/whats-on/events/'
};
}
else if(city === 'copenhagen' || city ==='københavn'){
noevents = {
"message":message,
"link":'https://www.visitcopenhagen.com/search/whatson'
};
}
else if(city === 'malmo' || city === 'malmö'){
noevents = {
"message":message,
"link":'http://www.malmotown.com/en/events-calendar/'
};
}
else if(city === 'gothenburg' || city === 'göteborg'){
noevents = {
"message":message,
"link":'http://www.goteborg.com/en/events/'
};
}
else if(city === 'london'){
noevents = {
"message":message,
"link":'https://www.visitlondon.com/things-to-do/whats-on/special-events/london-events-calendar'
};
}
else{
noevents = {
"message": `Sorry! The automatic events listing service is not running in ${city} yet. If you want it running here then let us know at.`,
"link": "https://globati.com"
}
}
res.send(noevents)
})