-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
240 lines (174 loc) · 8.01 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
//Add requires
const express = require('express');
const bodyParser = require('body-parser');
const chalk = require('chalk');
var moment = require('moment');
var request = require('request');
var schedule = require('node-schedule');
const MongoClient = require('mongodb').MongoClient;
const app = express();
//Set global variables
let port = process.env.PORT || 8080;
mongoURL = 'mongodb://system:[email protected]:24548/celebstock';
const mongoDBName = 'celebstock';
//Allow app to use Public directory for CSS
app.use(express.static(__dirname + '/public'));
//Allow app to use body-parser
app.use(bodyParser.urlencoded({extended: true}));
//Set view engine to EJS
app.set('view engine', 'ejs');
//Mongo connection
MongoClient.connect(mongoURL, { useNewUrlParser: true },function(err, database) {
if (err) return console.log(chalk.red(err));
db = database.db(mongoDBName);
console.log(chalk.green('MongoDB connected'));
})
//Homepage render
app.get('/', function(req, res) {
res.redirect('/all');
});
//Admin render
app.get('/admin', function(req, res) {
res.render('pages/admin.ejs');
});
//All render
app.get('/all', function(req, res) {
db.collection("celebs").find({}).sort( { celebrityPrice: -1 } ).toArray(function(err, result) {
if (err) throw err;
if (result.length == 0) {
console.log("No celebs in DB");
res.redirect('/admin');
} else {
res.render('pages/all.ejs', { celebList: result });
}
})
});
app.post('/freshstart', function(req, res) {
console.time('Fresh start completed');
db.collection('celebs').deleteMany({});
console.log(chalk.blue('Celebs DB emptied'));
request(celebrityBucksAPIOPtions, function (error, response, body) {
if (error) throw error;
//console.log('error:', error); // Print the error if one occurred
//console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log(chalk.blue('Celebs found:', body.CelebrityValues.length)); // Print the HTML for the Google homepage.
amountOfCelebs = body.CelebrityValues.length;
//add error checking here on api request
var i;
for (i = 0; i < amountOfCelebs; i++) {
var currentCeleb = {celebrityId: body.CelebrityValues[i].celebId, celebrityName: body.CelebrityValues[i].name, celebrityPrice: body.CelebrityValues[i].price/1000, lastUpdated: moment().format('H:mm:ss, Do MMMM YYYY')};
//console.log(body.CelebrityValues[i].name);
db.collection('celebs').insertOne(currentCeleb, function(err, result) {
if (err) return console.log(chalk.red(err));
});
console.log(chalk.green(currentCeleb.celebrityName + ' added'));
}
console.timeEnd('Fresh start completed');
res.redirect('/admin');
});
});
//app.post('/clearall', function(req, res) {
// db.collection('celebs').deleteMany({});
// console.log('Celebs DB emptied');
// res.redirect('/admin');
//});
app.post('/fetchandupdate', function(req, res) {
request(celebrityBucksAPIOPtions, function (error, response, body) {
if (error) throw error;
//console.log('error:', error); // Print the error if one occurred
//console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log(chalk.blue('Celebs found:', body.CelebrityValues.length)); // Print the HTML for the Google homepage.
amountOfCelebs = body.CelebrityValues.length;
//add error checking here on api request
var i;
for (i = 0; i < amountOfCelebs; i++) {
var currentCeleb = {celebrityId: body.CelebrityValues[i].celebId, celebrityName: body.CelebrityValues[i].name, celebrityPrice: body.CelebrityValues[i].price/1000, lastUpdated: moment().format('H:mm:ss, Do MMMM YYYY')};
//console.log(body.CelebrityValues[i].name);
db.collection('celebs').findOneAndUpdate({celebrityId: body.CelebrityValues[i].celebId}, {$set: currentCeleb}, {upsert: true}, function(err, result) {
if (err) return console.log(chalk.red(err));
if (result.ok === 1 && result.lastErrorObject.updatedExisting === true) {
console.log(result.value.celebrityName + ' updated')
} else if (result.ok === 1 && result.lastErrorObject.updatedExisting === false) {
console.log('New celeb added');
} else {
console.log('Error adding ' + result.value.celebrityName);
}
});
}
res.redirect('/admin');
});
});
//Listen server
app.listen(port, function() {
console.log(chalk.green('Server started and listening on port ' + port));
});
var celebrityBucksAPIOPtions = {
uri: 'https://celebritybucks.com/developers/export/JSON',
qs: {
//limit: '5' // -> uri + '?access_token=xxxxx%20xxxxx'
},
headers: {
// 'User-Agent': 'Request-Promise'
},
json: true // Automatically parses the JSON string in the response
};
var getCelebrityBucksAPIData = function() {
return new Promise(function(resolve, reject) {
request(celebrityBucksAPIOPtions)
.then(function (data) {
console.log(data.CelebrityValues.length + ' celebs gathered from API');
resolve(data.CelebrityValues);
})
.catch(function (err) {
console.log('API call failed ' + err);
});
});
};
let updateMongoDB = function(p) {
return new Promise(function(resolve, reject) {
let promises = []
var i;
for (i = 0; i < p.length; i++) {
promises.push(new Promise((resolve, reject) => {
var currentCeleb = {celebrityId: p[i].celebId, celebrityName: p[i].name, celebrityPrice: p[i].price};
console.log(p[i].name);
db.collection('celebs').find({celebrityId: p[i].celebId}).toArray()
.then(function(response) {
//resolve(response)
console.log(response)
})
.catch(function (err) {
console.log('Error: ' + err);
});
})
)
}
return Promise.all(promises)
})
}
var j = schedule.scheduleJob('30 * * * *', function() {
console.log('scheduled job starting')
request(celebrityBucksAPIOPtions, function (error, response, body) {
if (error) throw error;
//console.log('error:', error); // Print the error if one occurred
//console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log(chalk.blue('Celebs found:', body.CelebrityValues.length)); // Print the HTML for the Google homepage.
amountOfCelebs = body.CelebrityValues.length;
//add error checking here on api request
var i;
for (i = 0; i < amountOfCelebs; i++) {
var currentCeleb = {celebrityId: body.CelebrityValues[i].celebId, celebrityName: body.CelebrityValues[i].name, celebrityPrice: body.CelebrityValues[i].price/1000, lastUpdated: moment().format('H:mm:ss, Do MMMM YYYY')};
//console.log(body.CelebrityValues[i].name);
db.collection('celebs').findOneAndUpdate({celebrityId: body.CelebrityValues[i].celebId}, {$set: currentCeleb}, {upsert: true}, function(err, result) {
if (err) return console.log(chalk.red(err));
if (result.ok === 1 && result.lastErrorObject.updatedExisting === true) {
console.log(result.value.celebrityName + ' updated')
} else if (result.ok === 1 && result.lastErrorObject.updatedExisting === false) {
console.log('New celeb added');
} else {
console.log('Error adding ' + result.value.celebrityName);
}
});
}
});
})