diff --git a/database/build.js b/database/build.js index b7a0d67..5d42161 100644 --- a/database/build.js +++ b/database/build.js @@ -3,7 +3,7 @@ var con = require('./db'); con.connect(function(err) { if (err) throw err; console.log("Connected!"); - con.query("CREATE DATABASE unesco", function (err, res) { + con.query("CREATE DATABASE unesco CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;", function (err, res) { if (err) throw err; console.log('"unesco" Database created.'); }); diff --git a/database/fill-database.js b/database/fill-database.js deleted file mode 100644 index e8e3710..0000000 --- a/database/fill-database.js +++ /dev/null @@ -1,35 +0,0 @@ -var con = require('./db'); - -con.connect(function(err) { - if (err) throw err; - console.log("Connected!"); - - con.query("USE unesco", function (err, res) { - if (err) throw err; - console.log('"unesco" Database selected.'); - }); - - sql = "INSERT INTO sites (category, in_danger, date_inscribed, unesco_url, latitude, longitude, description, site, unesco_unique) VALUES ?"; - val = [ - ['Natural', true, '2007','https://whc.unesco.org/en/list/1133','49.0097222222', '22.3388888889', '<p><span>This transboundary property stretches over 12 countries. Since the end of the last Ice Age, European Beech spread from a few isolated refuge areas in the Alps, Carpathians</span><span>, Dinarides</span><span>, Mediterranean and Pyrenees over a short period of a few thousand years in a process that is still ongoing. The successful expansion across a whole continent is related to the tree’s </span><span>adaptability and tolerance of different climatic, geographical and physical conditions. </span></p>', 'Ancient and Primeval Beech Forests of the Carpathians and Other Regions of Europe', '2152'], - ['Cultural', false, '2014','https://whc.unesco.org/en/list/1459','-18.2500000000', '-69.5916666667', '<p>This site is an extensive Inca communication, trade and defence network of roads covering 30,000 km. Constructed by the Incas over several centuries and partly based on pre-Inca infrastructure, this extraordinary network through one of the world’s most extreme geographical terrains linked the snow-capped peaks of the Andes – at an altitude of more than 6,000 m – to the coast, running through hot rainforests, fertile valleys and absolute deserts. It reached its maximum expansion in the 15th century, when it spread across the length a significance', 'Qhapaq Ñan, Andean Road System', '2003'] - ]; - - con.query(sql, [val], function(err, res){ - if (err) throw err; - console.log("sites" + ": records inserted: " + res.affectedRows); - }); - - sql = "INSERT INTO visits (date, img, site_id) VALUES ?"; - val = [ - ['10-15-2019','https://seanland.ca', '1'], - ['10-17-2019','https://seanland.ca', '1'] - ]; - - con.query(sql, [val], function(err, res){ - if (err) throw err; - console.log("visits" + ": records inserted: " + res.affectedRows); - process.exit(); - }); - -}); diff --git a/database/fill-sites.js b/database/fill-sites.js new file mode 100644 index 0000000..d5096c2 --- /dev/null +++ b/database/fill-sites.js @@ -0,0 +1,77 @@ +var con = require('./db'); +var fs = require('fs'); +var parser = require('fast-xml-parser'); + +var string = fs.readFileSync(process.cwd() + '/data/unesco.xml', 'utf8').toString(); + +var val = parser.parse(string); +var arr = []; + +val.query.row.forEach(function(site){ + var s = []; + s.push(site.category); + s.push(site.danger != '' || false); + s.push(site.date_inscribed); + s.push(site.http_url); + s.push(site.latitude); + s.push(site.longitude); + s.push(site.short_description); + s.push(site.site); + s.push(site.unique_number); + // console.log(s); + arr.push(s); +}); + +console.log(arr); + +con.connect(function(err) { + if (err) throw err; + console.log("Connected!"); + + sql = "INSERT INTO sites (category, in_danger, date_inscribed, unesco_url, latitude, longitude, description, site, unesco_unique) VALUES ?"; + + con.query(sql, [arr], function(err, res){ + if (err) throw err; + console.log("sites" + ": records inserted: " + res.affectedRows); + }); +}); + + + // fs.readFile(process.cwd() +'/data/unesco.xml', function(err, data) { + // parser.parseString(data, function (err, result) { + // result.query.row.forEach(function(site){ + // var danger = true; + // var s = ['1','2','3','4','5','6','7','8','9']; + // // var s = [site.category, site.danger, site.date_inscribed, site.http_url, site.latitude, site.longitude, site.short_description, site.site, site.unique_number]; + // con.query(sql, [[site.category, 1, site.date_inscribed, site.http_url, site.latitude, site.longitude, site.short_description, site.site, site.unique_number]], function(err, res){ + // if (err) throw err; + // console.log("Site Imported: " + site.site); + // }); + // // console.log(s); + // }); + // console.log('Done'); + // }); + // }); + // console.log(val); + + // con.query(sql, [val.query.row], function(err, res){ + // if (err) throw err; + // console.log("sites" + ": records inserted: " + res.affectedRows); + // }); + + + + // sql = "INSERT INTO visits (date, img, site_id) VALUES ?"; + // val = [ + // ['10-15-2019','https://seanland.ca', '1'], + // ['10-17-2019','https://seanland.ca', '1'] + // ]; + // + // con.query(sql, [val], function(err, res){ + // if (err) throw err; + // console.log("visits" + ": records inserted: " + res.affectedRows); + // process.exit(); + // }); + + +// }); diff --git a/package-lock.json b/package-lock.json index d9fb696..889bc5b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -534,6 +534,11 @@ } } }, + "fast-xml-parser": { + "version": "3.15.1", + "resolved": "https://registry.npmjs.org/fast-xml-parser/-/fast-xml-parser-3.15.1.tgz", + "integrity": "sha512-MStlD6aNPZCd9msF5wBh2VJ0jAE2zz85ipk+OIPO+pZi64ckY//oGi5kskcTVRj2bMSmBI5F2SY1IGWHWZzbCA==" + }, "fill-range": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", @@ -566,6 +571,11 @@ "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" }, + "fs": { + "version": "0.0.1-security", + "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", + "integrity": "sha1-invTcYa23d84E/I4WLV+yq9eQdQ=" + }, "fsevents": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.1.2.tgz", @@ -1345,11 +1355,6 @@ "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, - "sax": { - "version": "1.2.4", - "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz", - "integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==" - }, "semver": { "version": "5.7.1", "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", @@ -1696,20 +1701,6 @@ "resolved": "https://registry.npmjs.org/xdg-basedir/-/xdg-basedir-3.0.0.tgz", "integrity": "sha1-SWsswQnsqNus/i3HK2A8F8WHCtQ=" }, - "xml2js": { - "version": "0.4.23", - "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.23.tgz", - "integrity": "sha512-ySPiMjM0+pLDftHgXY4By0uswI3SPKLDw/i3UXbnO8M/p28zqexCUoPmQFrYD+/1BzhGJSs2i1ERWKJAtiLrug==", - "requires": { - "sax": ">=0.6.0", - "xmlbuilder": "~11.0.0" - } - }, - "xmlbuilder": { - "version": "11.0.1", - "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-11.0.1.tgz", - "integrity": "sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA==" - }, "yallist": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz", diff --git a/package.json b/package.json index 3245eb5..f8af89a 100644 --- a/package.json +++ b/package.json @@ -7,17 +7,18 @@ "dev": "nodemon ./bin/www", "build-db": "node database/build.js", "drop-db": "node database/drop-database.js", - "fill-db": "node database/fill-database.js" + "fill-sites": "node database/fill-sites.js" }, "dependencies": { "cookie-parser": "~1.4.4", "debug": "~2.6.9", "express": "~4.16.1", + "fast-xml-parser": "^3.15.1", + "fs": "0.0.1-security", "http-errors": "~1.6.3", "morgan": "~1.9.1", "mysql": "^2.17.1", "nodemon": "^2.0.2", - "pug": "^2.0.4", - "xml2js": "^0.4.23" + "pug": "^2.0.4" } }