forked from nunuchi/cisc375hw3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
794 lines (691 loc) · 27 KB
/
server.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
//Sets up the requirement to use
var fs = require('fs');
var path = require('path');
var http = require('http');
var url = require('url');
var https = require('https');
//Sets up Modules for use
var express = require('express');
var bodyParser = require('body-parser');
var sqlite3 = require('sqlite3').verbose();
var validator = require('express-validator');
var querystring = require('querystring');
var app = express();
var port = 3000;
//ArraySets
var sqlHolder = [];
var sqlTitleArray = [];
var sqlTitlePrincipalsArray = [];
var sqlPeopleArray = [];
var posterImage;
//middleware, required to use the modules
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
//app.use(validator());
app.use(validator({
errorFormatter: function(param, msg, value, location) {
var namespace = param.split('.'),
root = namespace.shift(),
formParam = root;
while(namespace.length) {
formParam += '['+namespace.shift()+']';
}
return {
param: formParam,
msg: msg,
value: value
};
}
}));//validator
//view engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
//set static path
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
res.render('index', {
errors: undefined
});//res.render end
});
//POST request from search form in index.html
app.post('/search', (req, res) => {
req.checkBody('search_text', 'Please provide something to search').notEmpty();
console.log(req.headers);
console.log(req.body);
//validate form input
//req.checkBody('search-text', 'Enter text to search').trim().isLength({min: 1});
req.sanitizeBody('search_text').trim();
req.sanitizeBody('search_text').escape();
req.sanitizeBody('search_text').blacklist('\\(\\)\\;');
var errors = req.validationErrors();
if (errors) { //Sends to an Error Page, telling you what is wrong
console.log(errors)
res.render('index', {
errors: errors
});//res.render end
}//if(errors)
else
{ //Correct input sends you to the results page
var search_text = req.body.search_text.replace(/\*/g, "%");//use wildcard character
if(req.body.category === 'title')
{
console.log('title entered');
if(req.body.filter === 'on') {
var sql = 'SELECT tconst, primary_title, title_type, start_year, end_year FROM Titles WHERE primary_title LIKE "' + search_text + '" AND title_type = "' + req.body.type +'";';
} else {
var sql = 'SELECT tconst, primary_title, title_type, start_year, end_year FROM Titles WHERE primary_title LIKE "' + search_text + '";';
}
console.log(sql);
}
else if(req.body.category === 'person')
{
console.log('person entered');
if(req.body.filter === 'on') {
var sql = 'SELECT nconst, primary_name, primary_profession, birth_year, death_year FROM Names WHERE primary_name LIKE "' + search_text + '" AND primary_profession LIKE "%' + req.body.profession +'%";';
} else {
var sql = 'SELECT nconst, primary_name, primary_profession, birth_year, death_year FROM Names WHERE primary_name LIKE "' + search_text + '";';
}
console.log(sql);
}
getData(sql, req.body.category).then(function(sqlHolder) {//promise
console.log('then function');
res.render('results', { //prints out sql to results page
sqlHolder: sqlHolder
});//res.render end
return sqlHolder;
}, function() {
//rejection callback
//gets called if the query returned no results
console.log('query promise rejected')
var errors = [{msg:'No results. Please try another search'}];
res.render('index', {
errors: errors
});//res.render end
});//getData promise end
}//else
});//app.post end
app.post('/updateTitle', (req, res) => {
//console.log(req.headers);
console.log(req.body);
var sql;
if(req.body.type != 'noChange') {
sql = 'UPDATE Titles SET title_type = "' + req.body.type + '" WHERE tconst = "' + req.body.tconst + '"'
console.log(sql);
updateData(sql);
}
if(req.body.genreChange != 'noChange') {
//get current genres
sql = 'SELECT genres FROM Titles WHERE tconst = "' + req.body.tconst + '"'
var myPromise = new Promise(function(resolve, reject) {
var dbs = new sqlite3.Database('./imdb.sqlite3', (err) => {
if(err) {
console.error(err.message);
}//if(err) end
});//dbs end
dbs.get(sql, [], (err, row) => {
if(err) {// logs error
console.log(err);
}//if(err)
else {
var currentGenres = row.genres;
resolve(currentGenres);
console.log(currentGenres);
}
});//dbs end
dbs.close();
});
myPromise.then((currentGenres) => {
if(req.body.genreChange == 'add') {
currentGenres = currentGenres.split(",");
console.log(currentGenres);
if(currentGenres.indexOf(req.body.genre) < 0) {
currentGenres.push(req.body.genre);
console.log(currentGenres);
currentGenres = currentGenres.filter(function(val) { return val; }).join(",");
console.log(currentGenres);
sql = 'UPDATE Titles SET genres = "' + currentGenres + '" WHERE tconst = "' + req.body.tconst + '"'
updateData(sql);
console.log("add genre: " + req.body.genre);
}
}
else {
currentGenres = currentGenres.split(",");
var index = currentGenres.indexOf(req.body.genre);
if(index >= 0) {
currentGenres.splice(index, 1);
console.log(currentGenres);
sql = 'UPDATE Titles SET genres = "' + currentGenres + '" WHERE tconst = "' + req.body.tconst + '"'
updateData(sql);
}
console.log("remove genre: " + req.body.genre);
}
}).catch(function(err) {
console.log('Could not update genre: ' + err.message);
});
}
for(var key in req.body) {
if(key.substring(0, 2) === 'nm') {
req.sanitizeBody(key).trim();
req.sanitizeBody(key).escape();
req.sanitizeBody(key).blacklist('\\(\\)\\;');
var value = req.body[key];
console.log(key + ": " + value);
value = parseInt(value);
if(!isNaN(value)) {
sql = 'UPDATE Principals SET ordering = ' + value + ' WHERE tconst = "' + req.body.tconst + '" AND nconst = "' + key + '"'
console.log(sql);
updateData(sql);
}
}
}
setTimeout(() => {
res.redirect('/title?id=' + req.body.tconst);
}, 5000);
});//app.post end
app.post('/updateName', (req, res) => {
console.log(req.body);
var sql;
req.sanitizeBody('birth_year').trim();
req.sanitizeBody('birth_year').escape();
req.sanitizeBody('birth_year').blacklist('\\(\\)\\;');
req.sanitizeBody('death_year').trim();
req.sanitizeBody('death_year').escape();
req.sanitizeBody('death_year').blacklist('\\(\\)\\;');
var birth = parseInt(req.body.birth_year);
var death = parseInt(req.body.death_year);
if(!isNaN(birth)) {
//update birth year
sql = 'UPDATE Names SET birth_year = ' + birth + ' WHERE nconst = "' + req.body.nconst + '";';
console.log(sql);
updateData(sql);
}
if(!isNaN(death)) {
//update death year
sql = 'UPDATE Names SET death_year = ' + death + ' WHERE nconst = "' + req.body.nconst + '";';
console.log(sql);
updateData(sql);
} else if(req.body.death_year === 'present') {
//set death year to null if they enter 'present', otherwise the database will only be updated if they enter a number
sql = 'UPDATE Names SET death_year = null WHERE nconst = "' + req.body.nconst + '";';
console.log(sql);
updateData(sql);
}
if(req.body.professionChange != 'noChange') {
sql = 'SELECT primary_profession FROM Names WHERE nconst = "' + req.body.nconst + '"'
var myPromise = new Promise(function(resolve, reject) {
var dbs = new sqlite3.Database('./imdb.sqlite3', (err) => {
if(err) {
console.error(err.message);
}//if(err) end
});//dbs end
dbs.get(sql, [], (err, row) => {
if(err) {// logs error
console.log(err);
}//if(err)
else {
var currentProfs = row.primary_profession;
resolve(currentProfs);
console.log(currentProfs);
}
});//dbs end
dbs.close();
});
myPromise.then((currentProfs) => {
if(req.body.professionChange == 'add') {
currentProfs = currentProfs.split(",");
console.log(currentProfs);
if(currentProfs.indexOf(req.body.profession) < 0) {
currentProfs.push(req.body.profession);
console.log(currentProfs);
currentProfs = currentProfs.filter(function(val) { return val; }).join(",");
console.log(currentProfs);
sql = 'UPDATE Names SET primary_profession = "' + currentProfs + '" WHERE nconst = "' + req.body.nconst + '"'
updateData(sql);
console.log("add profession: " + req.body.profession);
}
}
else {
currentProfs = currentProfs.split(",");
var index = currentProfs.indexOf(req.body.profession);
if(index >= 0) {
currentProfs.splice(index, 1);
console.log(currentProfs);
sql = 'UPDATE Names SET primary_profession = "' + currentProfs + '" WHERE nconst = "' + req.body.nconst + '"'
updateData(sql);
}
console.log("remove profession: " + req.body.profession);
}
}).catch(function(err) {
console.log('Could not update profession: ' + err.message);
});
}
setTimeout(() => {
res.redirect('/person?id=' + req.body.nconst);
}, 5000);
});//app.post end
//New Get 4-22-18
//UPDATED CODE BY NOU
app.get('/title', function(req, res){
var titlePromise = getExtendData(req.query.id);
var peoplePromise = getTitlePrincipalData(req.query.id);
var posterImagePromise = posterDataFunction (req.query.id);
console.log(posterImagePromise+' 123456');
Promise.all([titlePromise, peoplePromise, posterImagePromise]).then(function(values){//promise
res.render('title', {
sqlTitleArray: values[0],
sqlTitlePrincipalsArray: values[1],
moviePoster: values[2]
});//res.render end
//console.log(values);
//console.log(values[0]);
console.log('Title Array'+' '+values[0]+' Title Principals Array: '+values[1]);
});//getExtendedData end
})//app.get end
app.get('/person', function(req, res){
var personPromise = getPersonData(req.query.id);
var personPosterPromise = posterPersonDataFunction(req.query.id)
Promise.all([personPromise, personPosterPromise]).then(function(values){//promise
res.render('person', {
sqlPeopleArray: values[0],
personPoster: values[1]
});//res.render end
console.log('People Array'+sqlPeopleArray)
});//getExtendedData end
})//app.get end
app.listen(port, () => {
console.log('Now listening on port ' + port);
});//app.listen end
function getData(sql, method) { //gets the SQL data
var sqlItem = sql;
var rowlength; //to hold rowlength
var currentrowlength = 0; //to hold current row
var myPromise = new Promise(function(resolve, reject) {
var dbs = new sqlite3.Database('./imdb.sqlite3', (err) => {
if(err) {
return console.error(err.message);
}
});
sqlHolder = []; //resets the sqlHolder array to be empty so data doesn't repeat
dbs.all(sqlItem, [], (err, rows) => { rowlength = rows.length; console.log(rowlength);
if(err) {// logs error
console.log(err);
}//if(err)
//reject promise if no rows to prevent infinite loop
if(rowlength < 1) {reject('Unsuitable Data for usage because of 0 length!');}
//fill sqlHolder with data from each row
rows.forEach((row) => { currentrowlength += 1;
if(method === 'title') {
sqlHolder.push({
//set Var name: actual variable
//for results: data for results
type: method,
tconst: row.tconst,
primary_title: row.primary_title,
title_type: row.title_type,
start_year: row.start_year,
end_year: row.end_year
});//sql push
} else if(method === 'person') {
sqlHolder.push({
//set Var name: actual variable
//for results: data for results
type: method,
nconst: row.nconst,
primary_name: row.primary_name,
primary_profession: row.primary_profession,
birth_year: row.birth_year,
death_year: row.death_year
});//sql push
}
if(currentrowlength == rowlength) { resolve(sqlHolder); console.log('Done');}
else if(rowlength < 1) {reject('Unsuitable Data for usage because of 0 length!');}
//Resolving and Rejecting if statements, to determine wait for data
});//forEach
});//dbs end
dbs.close()
});//my Promise End
return myPromise;
}//getData end
//Updated 4/25/18
function getExtendData(inputID) {
//var rowlength; //to hold rowlength
//var currentrowlength = 0; //to hold current row
var sqlItem = 'SELECT Titles.tconst AS tconst, primary_title, title_type, start_year, end_year, runtime_minutes, genres, average_rating, num_votes, directors, writers FROM (SELECT * FROM Titles WHERE tconst="'+inputID+'") AS Titles LEFT OUTER JOIN Ratings ON Titles.tconst=Ratings.tconst LEFT OUTER JOIN Crew ON Titles.tconst=Crew.tconst';
sqlTitleArray = []; //resets the sqlHolder array to be empty so data doesn't repeat
var myPromise = new Promise(function(resolve, reject) {
var dbs = new sqlite3.Database('./imdb.sqlite3', (err) => {
if(err) {
return console.error(err.message);
}//if(err) end
});//dbs end
console.log(sqlItem);
dbs.get(sqlItem, [], (err, row) => {
console.log(row);
if(err) {// logs error
console.log(err);
}//if(err)
//reject promise if no rows to prevent infinite loop
if(row === null) {reject('Unsuitable Data for usage because of 0 length!');}
//fill sqlHolder with data
if(row){
sqlTitleArray = {
tconst: row.tconst,
primary_title: row.primary_title,
title_type: row.title_type,
start_year: row.start_year,
end_year: row.end_year,
runtime_minutes: row.runtime_minutes,
genres: row.genres,
average_rating: row.average_rating,
num_votes: row.num_votes,
directors: row.directors,
writers: row.writers,
directorsNamed: getCastNames(row.directors),
writersNamed: getCastNames(row.writers)
}}
else {
sqlTitleArray = {
tconst: inputID,
primary_title: inputID+' No Data',
title_type: '',
start_year: '',
end_year: '',
runtime_minutes: '',
genres: '',
average_rating: '',
num_votes: '',
directors: '',
writers: '',
directorsNamed: '',
writersNamed: ''
}
}
resolve(sqlTitleArray);
console.log('Done Title Data');
//Resolving and Rejecting if statements, to determine wait for data
//});//forEach
});//dbs end
dbs.close();
});//Promise End
return myPromise;
}//getExtendedData End
//New function 4-22-18
function getTitlePrincipalData(inputID) {
var rowlength; //to hold rowlength
var currentrowlength = 0; //to hold current row
var sqlItem = 'SELECT Titles.tconst AS tconst, ordering, Principals.nconst AS nconst, category, primary_name FROM (SELECT * FROM Titles WHERE tconst='+'"'+inputID+'") AS Titles LEFT OUTER JOIN Principals ON Titles.tconst=Principals.tconst LEFT OUTER JOIN Names ON Principals.nconst=Names.nconst ORDER BY ordering';
sqlTitlePrincipalsArray = []; //resets the sqlHolder array to be empty so data doesn't repeat
var myPromise = new Promise(function(resolve, reject) {
var dbs = new sqlite3.Database('./imdb.sqlite3', (err) => {
if(err) {
return console.error(err.message);
}//if(err) end
});//dbs end
//console.log(sqlItem);
dbs.all(sqlItem, [], (err, rows) => {
if(rows === undefined) {
reject('No query results');
}
console.log(rows);
//console.log('rowlength = rows.length')
if(err) {// logs error
console.log(err);
}//if(err)
var rowlength = rows.length;
//reject promise if no rows to prevent infinite loop
if(rowlength < 1) {reject('Unsuitable Data for usage because of 0 length!');}
//fill sqlHolder with data
rows.forEach((row) => { currentrowlength += 1;
sqlTitlePrincipalsArray.push({
tconst: row.tconst,
ordering: row.ordering,
nconst: row.nconst,
category: row.category,
primary_name: row.primary_name
});//sql push
if(currentrowlength == rowlength) { resolve(sqlTitlePrincipalsArray); console.log('Done Principals Data for Title'); }
});//forEach
});//dbs end
dbs.close();
});//Promise End
return myPromise;
}//getTitlePrincipalData End
//Updated 4/25/18
function getPersonData(inputID) {
var rowlength; //to hold rowlength
var currentrowlength = 0; //to hold current row
var sqlItem = 'SELECT nconst, primary_name, birth_year, death_year, primary_profession, known_for_titles FROM Names WHERE nconst='+'"'+inputID+'"';
sqlPeopleArray = []; //resets the sqlHolder array to be empty so data doesn't repeat
console.log(sqlItem);
console.log(sqlPeopleArray+'Before dbs');
var myPromise = new Promise(function(resolve, reject) {
var dbs = new sqlite3.Database('./imdb.sqlite3', (err) => {
if(err) {
return console.error(err.message);
}//if(err) end
});//dbs end
dbs.all(sqlItem, [], (err, rows) => {
console.log(rows);
console.log('rowlength = rows.length')
var rowlength = rows.length;
if(err) {// logs error
console.log(err);
}//if(err)
//reject promise if no rows to prevent infinite loop
if(rowlength < 1) {reject('Unsuitable Data for usage because of 0 length!');}
//fill sqlHolder with data from each row
rows.forEach((row) => { currentrowlength += 1;
var death_year_input = row.death_year;
if(death_year_input == null) { death_year_input = "present"; }
sqlPeopleArray.push({
nconst: row.nconst,
primary_name: row.primary_name,
primary_profession: row.primary_profession,
birth_year: row.birth_year,
death_year: death_year_input,
known_for_titles: row.known_for_titles,
known_for_titles2: getTitleNames(row.known_for_titles)
});//sql push
if(currentrowlength == rowlength) { resolve(sqlPeopleArray); console.log('Done Person Data');}
//Resolving and Rejecting if statements, to determine wait for data
});//forEach
});//dbs end
dbs.close();
console.log(sqlPeopleArray+'after dbs')
});//Promise End
return myPromise;
}//getPersonData End
//UPDATED CODE BY NOU
function posterDataFunction(title_id) {
var myPromise = new Promise(function(resolve, reject) {
var req_url = {
host: 'www.imdb.com',
path: '/title/' + title_id + '/'
};
var req = https.request(req_url, (res) => {
var body = '';
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
var poster_link_pos = body.indexOf('<a href="/title/' + title_id + '/mediaviewer/');
var poster_img_pos = body.indexOf('<img', poster_link_pos);
var poster_src_pos = body.indexOf('src=', poster_img_pos) + 5;
var poster_end_pos = body.indexOf('"', poster_src_pos);
var poster_url = url.parse(body.substring(poster_src_pos, poster_end_pos));
if (poster_url.host !== null) {
posterImage = {host: poster_url.host, path: poster_url.pathname};
console.log(posterImage+' adasdasdasda');
resolve(posterImage);
}
else {
posterImage = '/default_poster.jpg';
console.log('poster not found');
resolve(posterImage);
}
});
});
req.on('error', (err) => {
console.log(err);
});
req.setTimeout(5000);
req.end();
});//Promise End
return myPromise;
}//End
function posterPersonDataFunction(name_id) {
var myPromise = new Promise(function(resolve, reject) {
var req_url = {
host: 'www.imdb.com',
path: '/name/' + name_id + '/'
};
var req = https.request(req_url, (res) => {
var body = '';
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
var poster_link_pos = body.indexOf('<a href="/name/' + name_id + '/mediaviewer/');
var poster_img_pos = body.indexOf('<img', poster_link_pos);
var poster_src_pos = body.indexOf('src=', poster_img_pos) + 5;
var poster_end_pos = body.indexOf('"', poster_src_pos);
var poster_url = url.parse(body.substring(poster_src_pos, poster_end_pos));
if (poster_url.host !== null) {
posterImage = {host: poster_url.host, path: poster_url.pathname};
console.log(posterImage+' personDataFunction');
resolve(posterImage);}
else {
posterImage = '/default_poster.jpg';
console.log('poster not found');
resolve(posterImage);
}
});
});
req.on('error', (err) => {
console.log(err);
});
req.setTimeout(5000);
req.end();
});//Promise End
return myPromise;
}//End
function getTitleNames(names) {
var rowlength; //to hold rowlength
var currentrowlength = 0;
var nameSplit = names.split(',');
var nameBlock = [];
var sqlItem;
nameSplit.forEach((nameItem) => {
sqlItem = 'SELECT tconst, primary_title FROM Titles WHERE tconst = "'+nameItem+'"';
var dbs = new sqlite3.Database('./imdb.sqlite3', (err) => {
if(err) {
return console.error(err.message);
}//if(err) end
});//dbs end
dbs.all(sqlItem, [], (err, rows) => {
var rowlength = rows.length;
if(err) {// logs error
console.log(err);
}//if(err)
//reject promise if no rows to prevent infinite loop
if(rowlength < 1) { currentrowlength = 1; rowlength = 1;
}
else {
//fill sqlHolder with data from each row
rows.forEach((row) => { currentrowlength += 1;
nameBlock.push({
tconst: row.tconst,
primary_title: row.primary_title
});//sql push
});//forEach
}//else
//if(currentrowlength == rowlength) { resolve(sqlPeopleArray); console.log('Done Person Data');}
});//dbs.all
dbs.close();
});
return nameBlock;
}
function getCastNames(names) {
console.log(names+'getCastNames check');
var rowlength; //to hold rowlength
var currentrowlength = 0;
var nameSplit;
if(names) {nameSplit = names.split(',');}
else {nameSplit = ('none,none').split(',');}
var nameBlock = [];
var sqlItem;
nameSplit.forEach((nameItem) => {
sqlItem = 'SELECT nconst, primary_name FROM Names WHERE nconst = "'+nameItem+'"';
var dbs = new sqlite3.Database('./imdb.sqlite3', (err) => {
if(err) {
return console.error(err.message);
}//if(err) end
});//dbs end
dbs.all(sqlItem, [], (err, rows) => {
var rowlength = rows.length;
if(err) {// logs error
console.log(err);
}//if(err)
//reject promise if no rows to prevent infinite loop
if(rowlength < 1) { currentrowlength = 1; rowlength = 1;
}
else {
//fill sqlHolder with data from each row
rows.forEach((row) => { currentrowlength += 1;
nameBlock.push({
nconst: row.nconst,
primary_name: row.primary_name
});//sql push
console.log(row.nconst+' '+row.primary_name);
});//forEach
}//else
//if(currentrowlength == rowlength) { resolve(sqlPeopleArray); console.log('Done Person Data');}
});//dbs.all
dbs.close();
});
return nameBlock;
}
//END UPDATED CODE BY NOU
function updateData(sql) { //gets the SQL data
var dbs = new sqlite3.Database('./imdb.sqlite3', (err) => {
if(err) {
return console.error(err.message);
}
});
dbs.run(sql, [], (err) => {
if(err) {
return console.error(err.message);
}
});
console.log('Row updated');
dbs.close();
}//getData end
//area for test queriesvar dbs = new sqlite3.Database('./imdb.sqlite3', (err) => {
/*var sqlItem = 'SELECT DISTINCT primary_profession FROM Names';
var dbs = new sqlite3.Database('./imdb.sqlite3', (err) => {
if(err) {
return console.error(err.message);
}//if(err) end
});//dbs end
console.log(sqlItem);
dbs.all(sqlItem, [], (err, rows) => {
//console.log(rows);
console.log('rowlength = rows.length')
var rowlength = rows.length;
if(err) {// logs error
console.log(err);
}//if(err)
profs = [];
rows.forEach((row) => {
//console.log(row.primary_profession);
row_profs = row.primary_profession;
row_profs = row_profs.split(",")
row_profs.forEach((item) => {
profs.push(item);
})
});//forEach
var uniqueArray = profs.filter(function(item, pos) {
return profs.indexOf(item) == pos;
})
console.log(uniqueArray);
});//dbs end*/