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
|
const MongoClient = require('mongodb').MongoClient;
require('dotenv').config();
// This is for passing the parameters of the search to check and see if it already exists in the database
// if it does exist, we're gonna call another function in another file.
const scraper = require('./scraper/scrape')
exports.performCheck = async function performCheck(id, query, type) {
const uri = process.env.MONGO_URI;
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
var result;
await client.connect();
const database = client.db("db");
const tv = database.collection("tv");
const movie = database.collection('movie')
const search = { id: `${id}` };
// check to see if the title is already in the database
switch (type) {
case 'tv':
result = await tv.findOne(search);
break;
case 'movie':
result = await movie.findOne(search);
break;
}
if (result == null) {
console.log('no db entry found')
await scraper.performSearch(id, query, type)
}
// (if it needed to be scraped, it now is, and its stored. next, we perform the database search for the newly saved entry)
console.log('start db search')
var array = await performDatabaseSearch(id, type); // returns values
await client.close();
return array;
}
async function performDatabaseSearch(id, type) {
// do database search
const uri = process.env.MONGO_URI;
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
var result;
await client.connect();
const database = client.db("db");
const tv = database.collection("tv");
const movie = database.collection('movie')
const search = { id: `${id}` };
// check to see if the title is already in the database
switch (type) {
case 'tv':
result = await tv.findOne(search)
break;
case 'movie':
result = await movie.findOne(search)
break;
}
client.close()
console.log('db search finished')
return [result.service, result.price]
}
|