-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmount.js
More file actions
382 lines (301 loc) · 11.2 KB
/
mount.js
File metadata and controls
382 lines (301 loc) · 11.2 KB
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
const express = require('express');
const bodyParser = require('body-parser');
const db = require('./models');
const path = require("path");
const fs = require("fs");
const axios = require('axios');
const Op = db.Sequelize.Op;
;(async function main() {
try {
await sync_db();
console.log('DB Sync complete.');
// await test();
// need to load city data first due to foreign key constraints
await load_city_data();
// after mounting city data, mount the rest
await mount_data();
// await testAxios();
await mount_specialcity_data();
} catch (error) {
console.error('Database mounting unsuccessful:', error)
}
})()
async function test() {
// if you need to test anything on mount.js, write your code here
// and uncomment the calling of test in main
}
async function mount_specialcity_data() {
console.log("mount special city data")
const specialCities = ["서울 서울", "부산 부산", "인천 인천", "대구 대구", "대전 대전", "울산 울산", "광주 광주"]
const express_search = ["express_direct"]
const suburbs_search = ["suburbs_direct"]
for (var i = 0; i < specialCities.length; i++) {
var origin = specialCities[i]
console.log("origin: ", origin)
var express_destination_list = new Set()
var suburbs_destination_list = new Set()
// console.log("express body: ", JSON.stringify(expressBody))
var express_res = await axios.post("https://adonde-kr.herokuapp.com/search/mount", {
theme: [],
distance: 10000,
population: [0, 100000],
transportation: express_search,
origin: origin
})
// console.log("post express res")
for (var j = 0; j < express_res.data.length; j++) {
express_destination_list.add(express_res.data[j]['sido_sgg'])
// console.log(express_res.data[j])
}
const express_specialcity = await db.specialexpress.create({
sido_sgg : origin,
destinations : Array.from(express_destination_list)
})
var suburbs_res = await axios.post("https://adonde-kr.herokuapp.com/search/mount", {
theme: [],
distance: 10000,
population: [0, 100000],
transportation: suburbs_search,
origin: origin
})
// console.log("post suburb res")
for (var k = 0; k < suburbs_res.data.length; k++) {
// console.log(suburbs_res.data[k])
suburbs_destination_list.add(suburbs_res.data[k]['sido_sgg'])
}
const suburbs_specialcity = await db.specialsuburbs.create({
sido_sgg : origin,
destinations : Array.from(suburbs_destination_list)
})
console.log("express res: ", express_specialcity)
console.log("suburbs res: ", suburbs_specialcity)
}
}
async function mount_specialcity_express(express_res, origin) {
var express_list = []
express_res = express_res.data.response.body
console.log("express res: ", express_res)
for (var i = 0; i < express_res.length; i++) {
express_list.push(express_res[i]['sido_sgg'])
}
const specialcity_express = await db.specialexpress.create({
sido_sgg: origin,
destinations: express_list
})
console.log("create special city express")
return res.json(specialcity_express)
}
function completeBody(transportation, origin) {
var base_body = {
"theme": [],
"distance": 10000,
"population": [0, 100000],
"transportation": [],
"origin": ""
}
base_body['transportation'].push(transportation)
base_body['origin'] = origin
return base_body
}
async function sync_db() {
await db.sequelize.sync({force: true});
// await db.sequelize.authenticate();
}
async function read_train_csv(filePath) {
var filePath = path.join(__dirname, filePath);
console.log(filePath)
var data = fs.readFileSync(filePath, {encoding: "utf8"});
var rows = data.split("\n");
var result = [];
for (var rowIndex in rows) {
// console.log(rows[rowIndex])
var city_destinations = rows[rowIndex].split("\"");
var comma_split = rows[rowIndex].split(",")
if (rowIndex == rows.length - 1) {
continue;
}
if (rowIndex != "0") {
var city = city_destinations[0]
var destinations = city_destinations[1]
var sido = city.split(",").slice(0,1).toString();
var sgg = city.split(",").slice(1,2).toString();
var sido_sgg = comma_split.slice(comma_split.length-1, comma_split.length).toString();
combination_list = destinations.toString().slice(1, destinations.length-1).split(",")
var destination_list = []
var destination_sido = ""
var destination_sgg = ""
for (var i = 0; i < combination_list.length / 2; i++) {
destination_sido = eval(combination_list[i*2]).replace(/\s+/g, '')
destination_sgg = eval(combination_list[i*2+1]).replace(/\s+/g, '')
destination_list.push([destination_sido, destination_sgg])
}
row = [sido, sgg, destination_list, sido_sgg]
}
if (rowIndex === "0") { var columns = rows[0].split(","); console.log(columns); }
else {
var data = {}; // 빈 객체를 생성하고 여기에 데이터를 추가한다.
for (var columnIndex in columns) { // 칼럼 갯수만큼 돌면서 적절한 데이터 추가하기.
var column = columns[columnIndex];
data[column] = row[columnIndex];
}
result.push(data);
}
}
// console.log(result);
return result
}
async function read_csv(filePath) {
var filePath = path.join(__dirname, filePath);
console.log(filePath)
var data = fs.readFileSync(filePath, {encoding: "utf8"});
var rows = data.split("\n");
var result = [];
for (var rowIndex in rows) {
var row = rows[rowIndex].split(",");
if (rowIndex === "0") { var columns = row; }
else {
var data = {}; // 빈 객체를 생성하고 여기에 데이터를 추가한다.
for (var columnIndex in columns) { // 칼럼 갯수만큼 돌면서 적절한 데이터 추가하기.
// console.log("row[columnIndex]: ", row[columnIndex])
var column = columns[columnIndex];
var eachData = row[columnIndex]
if (typeof eachData == 'string') {
// console.log("replace: ", eachData.replace(/\[\.\]/g, ','))
eachData = eachData.replace(/\[\.\]/g, ',')
}
data[column] = eachData;
}
result.push(data);
}
}
// console.log(result);
return result
}
async function load_city_data() {
// reading city data from a local csv file in data folder
const city_data = await read_csv("data/city/city_20210813.csv");
// pushing city data into city table in database
for (var i = 0; i < city_data.length - 1; i++) {
console.log(city_data[i])
await db.City.create({
sido: city_data[i]['sido'],
sgg: city_data[i]['sgg'],
population: city_data[i]['population'],
latitude: city_data[i]['lat'],
longitude: city_data[i]['long'],
sido_sgg: city_data[i]['sido_sgg'],
sido_code: city_data[i]['sido_code'],
sgg_code: city_data[i]['sgg_code'],
image_src: city_data[i]['image_src'],
description: city_data[i]['description'],
tourism_link: city_data[i]['tourism_link'],
mountains: city_data[i]['mountains'],
valleys: city_data[i]['valleys'],
beaches: city_data[i]['beaches'],
rivers: city_data[i]['rivers'],
})
}
}
async function addImageSrc(city_without_image) {
const sido_code = city_without_image['sido_code']
const sgg_code = city_without_image['sgg_code']
}
async function load_express_data() {
// reading express bus terminal data from a local csv file in data folder
const express_data = await read_csv("data/express/express_combined.csv");
// pushing express bus terminal data into express table in database
for (var i = 0; i < express_data.length - 1; i++) {
// console.log(express_data[i])
await db.Express.create({
...express_data[i]
})
}
// manually adding express bus terminal entries
const manual_express_data = [
{
"id": "NAEK010",
"name": "서울경부",
"sido": "서울",
"sgg": "서울",
"sido_sgg": "서울 서울",
},
{
"id": "NAEK801",
"name": "동대구",
"sido": "대구",
"sgg": "대구",
"sido_sgg": "대구 대구",
},
{
"id": "NAEK396",
"name": "안면도",
"sido": "충청남도",
"sgg": "태안",
"sido_sgg": "충청남도 태안",
},
{
"id": "NAEK500",
"name": "광주(유·스퀘어)",
"sido": "광주",
"sgg": "광주",
"sido_sgg": "광주 광주",
},
{
"id": "NAEK580",
"name": "장흥",
"sido": "전라남도",
"sgg": "장흥",
"sido_sgg": "전라남도 장흥",
},
];
for (var i = 0; i < manual_express_data.length; i++) {
await db.Express.create({
...manual_express_data[i]
})
}
}
async function load_suburbs_data() {
// reading suburban bus terminal data from a local csv file in data folder
const suburbs_data = await read_csv("data/suburbs/suburbs_active.csv");
// pushing suburban bus terminal data into suburbs table in database
for (var i = 0; i < suburbs_data.length - 1; i++) {
// console.log(suburbs_data[i])
await db.Suburbs.create({
...suburbs_data[i]
})
}
}
async function load_train_data() {
// reading train data from a local csv file in data folder
const train_data = await read_train_csv("data/train/train_combined.csv");
for (var i = 0; i < train_data.length - 1; i++) {
await db.Train.create({
...train_data[i]
})
}
}
async function mount_data() {
await load_train_data();
await load_express_data();
await load_suburbs_data();
await load_places_data();
}
async function load_places_data() {
const place_data = await read_csv("data/places/city_places_theme.csv");
for (var i = 0; i < place_data.length - 1; i++) {
console.log(place_data[i])
await db.Place.create({
...place_data[i]
})
}
}
async function testAxios() {
var res = await axios.post("https://adonde-kr.herokuapp.com/search/", {
theme: [],
distance: 10000,
population: [0, 100000],
transportation: ["train_direct"],
origin: "부산 부산"
})
console.log(res)
}