-
Notifications
You must be signed in to change notification settings - Fork 3
/
seedDB.js
139 lines (127 loc) · 5.91 KB
/
seedDB.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
const superagent = require('superagent');
const fs = require('fs');
const parser = require('xml2json');
const mysql = require("mysql");
/**
* Create DB connection
*/
const connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
port: 8889,
database: "housing-finder",
socketPath: "/Applications/MAMP/tmp/mysql/mysql.sock"
});
connection.connect(function (err) {
if (err)
// throw err;
console.log("Connected!");
});
//Parse the specified hard-coded SQL file and return an list of valid string SQL statements
function parseSQL(file) {
return new Promise ((resolve) => {
fs.readFile('./public/static-sql/' + file, 'utf8', (err, data) => {
if(err) {
console.log(err);
} else {
var sql = data.split(';');
sql.forEach((e) => {
e.trim();
var statement = e + ';';
})
sql.pop();
resolve(sql);
}
})
}
)
}
//Get list of static SQL statements and strore each entry in DB
async function storeStatic(db, file) {
return new Promise (async (resolve) => {
let commands = await parseSQL(file).catch((err) => console.log(err));
commands.forEach((statement) => {
db.query(statement, (err, success) => {
if(err) {
console.log(err);
} else {
console.log(`Successfully executed ${statement}`);
}
})
})
resolve('success');
})
}
// Initialize the DB with all required static data and 3rd party API data
async function initialize(db) {
//running predefined SQL scripts
success = await storeStatic(db, 'province.sql').catch((err) => console.log(err));
success = await storeStatic(db, 'city.sql').catch((err) => console.log(err));
success = await storeStatic(db, 'neighbourhood.sql').catch((err) => console.log(err));
success = await storeStatic(db, 'parks_recreation.sql').catch((err) => console.log(err));
success = await storeStatic(db, 'hospital.sql').catch((err) => console.log(err));
success = await storeStatic(db, 'school.sql').catch((err) => console.log(err));
success = await storeStatic(db, 'landlord.sql').catch((err) => console.log(err));
success = await storeStatic(db, 'rental_unit.sql').catch((err) => console.log(err));
success = await storeStatic(db, 'feature_list.sql').catch((err) => console.log(err));
success = await storeStatic(db, 'tables.sql').catch((err) => console.log(err));
success = await storeStatic(db, 'translinkType.sql').catch((err) => console.log(err));
success = await storeStatic(db, 'translink.sql').catch((err) => console.log(err));
//Inserting data from Yelp and Translink APIs for each neighbourhood
db.query('SELECT postal_code FROM neighbourhood;', (err, neighbourhoods) => {
neighbourhoods.forEach(async (neighbourhood, index) => {
await superagent
.get(`http://geocoder.ca?postal=${neighbourhood.postal_code}1c3&geoit=XML`)
.then(response => {
setTimeout(() => {
var json = JSON.parse(parser.toJson(response.text, {reversible: false}));
console.log(JSON.stringify(json.geodata.latt));
console.log(JSON.stringify(json.geodata.longt));
superagent
.get(`https://api.yelp.com/v3/businesses/search?term=restaurant&latitude=${JSON.stringify(json.geodata.latt).replace('"', '').replace('"', '')}&longitude=${JSON.stringify(json.geodata.longt).replace('"', '').replace('"', '')}&radius=2500&limit=6`)
.set('Accept', 'application/json')
.set('Authorization', 'Bearer uqiByH8CvARU3-_0UHYblksffNoFkxNCh4GefygRJsUGktYgG0DDFyhV-cIvYbzxKXNHmb8CkC4pYiGnH5_o384lO8IqiHlR6588br8RPjjWoLuLcq9ngyCH7Uv1XHYx')
.then(response => {
console.log(response.body.businesses[0]);
response.body.businesses.forEach(business => {
if(business.location.zip_code.includes(neighbourhood.postal_code)) {
db.query(`INSERT INTO restaurant VALUES
(${JSON.stringify(business.location.address1)}, ${JSON.stringify(business.name)},
${JSON.stringify(business.categories[0].title)}, ${JSON.stringify(neighbourhood.postal_code)})`,
(err, rows) => {
if(err) console.log(err);
})
}
})
}).catch(err => console.log(err))
}, index * 1000);
setTimeout(() => {
var json = JSON.parse(parser.toJson(response.text, {reversible: false}));
console.log(JSON.stringify(json.geodata.latt));
console.log(JSON.stringify(json.geodata.longt));
superagent
.get(`https://api.yelp.com/v3/businesses/search?categories=grocery&latitude=${JSON.stringify(json.geodata.latt).replace('"', '').replace('"', '')}&longitude=${JSON.stringify(json.geodata.longt).replace('"', '').replace('"', '')}&radius=2500&limit=6`)
.set('Accept', 'application/json')
.set('Authorization', 'Bearer uqiByH8CvARU3-_0UHYblksffNoFkxNCh4GefygRJsUGktYgG0DDFyhV-cIvYbzxKXNHmb8CkC4pYiGnH5_o384lO8IqiHlR6588br8RPjjWoLuLcq9ngyCH7Uv1XHYx')
.then(response => {
console.log(response.body.businesses[0]);
response.body.businesses.forEach(business => {
if(business.location.zip_code.includes(neighbourhood.postal_code)) {
db.query(`INSERT INTO supermarket VALUES
(${JSON.stringify(business.location.address1)}, ${JSON.stringify(business.name)},
${JSON.stringify(business.categories[0].title)}, ${JSON.stringify(neighbourhood.postal_code)})`,
(err, rows) => {
if(err) console.log(err);
})
}
})
}).catch(err => console.log(err))
}, index * 1000);
})
.catch(err => console.log(err))
});
})
}
initialize(connection);
module.exports = initialize;