HyeonJun Jeon

Merge branch 'mySQL' into 'master'

......@@ -6,7 +6,7 @@
- Front-End : React
- Back-End : Express
- Storage : LocalForage
- DataBase : MySQL
- Crawl : Puppeteer
<br>
......
......@@ -17,6 +17,7 @@
"express": "^4.18.1",
"fs": "^0.0.1-security",
"localforage": "^1.10.0",
"mysql": "^2.18.1",
"puppeteer": "^14.1.1",
"react": "^18.1.0",
"react-dom": "^18.1.0",
......@@ -4904,6 +4905,14 @@
"node": "*"
}
},
"node_modules/bignumber.js": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz",
"integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A==",
"engines": {
"node": "*"
}
},
"node_modules/binary-extensions": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz",
......@@ -11601,6 +11610,42 @@
"multicast-dns": "cli.js"
}
},
"node_modules/mysql": {
"version": "2.18.1",
"resolved": "https://registry.npmjs.org/mysql/-/mysql-2.18.1.tgz",
"integrity": "sha512-Bca+gk2YWmqp2Uf6k5NFEurwY/0td0cpebAucFpY/3jhrwrVGuxU2uQFCHjU19SJfje0yQvi+rVWdq78hR5lig==",
"dependencies": {
"bignumber.js": "9.0.0",
"readable-stream": "2.3.7",
"safe-buffer": "5.1.2",
"sqlstring": "2.3.1"
},
"engines": {
"node": ">= 0.6"
}
},
"node_modules/mysql/node_modules/readable-stream": {
"version": "2.3.7",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz",
"integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==",
"dependencies": {
"core-util-is": "~1.0.0",
"inherits": "~2.0.3",
"isarray": "~1.0.0",
"process-nextick-args": "~2.0.0",
"safe-buffer": "~5.1.1",
"string_decoder": "~1.1.1",
"util-deprecate": "~1.0.1"
}
},
"node_modules/mysql/node_modules/string_decoder": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz",
"integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==",
"dependencies": {
"safe-buffer": "~5.1.0"
}
},
"node_modules/nanoid": {
"version": "3.3.4",
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.4.tgz",
......@@ -14759,6 +14804,14 @@
"resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz",
"integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw="
},
"node_modules/sqlstring": {
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.1.tgz",
"integrity": "sha1-R1OT/56RR5rqYtyvDKPRSYOn+0A=",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/stable": {
"version": "0.1.8",
"resolved": "https://registry.npmjs.org/stable/-/stable-0.1.8.tgz",
......@@ -20121,6 +20174,11 @@
"resolved": "https://registry.npmjs.org/big.js/-/big.js-5.2.2.tgz",
"integrity": "sha512-vyL2OymJxmarO8gxMr0mhChsO9QGwhynfuu4+MHTAW6czfq9humCB7rKpUjDd9YUiDPU4mzpyupFSvOClAwbmQ=="
},
"bignumber.js": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz",
"integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A=="
},
"binary-extensions": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz",
......@@ -24974,6 +25032,41 @@
"thunky": "^1.0.2"
}
},
"mysql": {
"version": "2.18.1",
"resolved": "https://registry.npmjs.org/mysql/-/mysql-2.18.1.tgz",
"integrity": "sha512-Bca+gk2YWmqp2Uf6k5NFEurwY/0td0cpebAucFpY/3jhrwrVGuxU2uQFCHjU19SJfje0yQvi+rVWdq78hR5lig==",
"requires": {
"bignumber.js": "9.0.0",
"readable-stream": "2.3.7",
"safe-buffer": "5.1.2",
"sqlstring": "2.3.1"
},
"dependencies": {
"readable-stream": {
"version": "2.3.7",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz",
"integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==",
"requires": {
"core-util-is": "~1.0.0",
"inherits": "~2.0.3",
"isarray": "~1.0.0",
"process-nextick-args": "~2.0.0",
"safe-buffer": "~5.1.1",
"string_decoder": "~1.1.1",
"util-deprecate": "~1.0.1"
}
},
"string_decoder": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz",
"integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==",
"requires": {
"safe-buffer": "~5.1.0"
}
}
}
},
"nanoid": {
"version": "3.3.4",
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.4.tgz",
......@@ -27132,6 +27225,11 @@
"resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz",
"integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw="
},
"sqlstring": {
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.1.tgz",
"integrity": "sha1-R1OT/56RR5rqYtyvDKPRSYOn+0A="
},
"stable": {
"version": "0.1.8",
"resolved": "https://registry.npmjs.org/stable/-/stable-0.1.8.tgz",
......
......@@ -12,6 +12,7 @@
"express": "^4.18.1",
"fs": "^0.0.1-security",
"localforage": "^1.10.0",
"mysql": "^2.18.1",
"puppeteer": "^14.1.1",
"react": "^18.1.0",
"react-dom": "^18.1.0",
......
const fs = require("fs");
function parseICal(text) {
const schedules = [];
let sche = {};
......@@ -22,40 +20,41 @@ function parseICal(text) {
const errArr = [];
function process(sche, k, v) {
switch (k) {
case "DTSTAMP":
k = "CREATED";
v = dateTimeToDate(v);
break;
case "UID":
v = v.split("-")[2];
break;
case "DTSTART":
k = "START";
v = dateTimeToDate(v);
break;
sche.scheType = "time";
sche.date = getDatestr(v);
return;
case "DTEND":
k = "END";
v = dateTimeToDate(v);
k = "endTime";
v = getTimestr_z(v);
break;
case "DTSTART;VALUE=DATE":
case "DTEND;VALUE=DATE":
k = "DATE";
v = dateToDate(v);
sche.scheType = "date";
k = "date";
v = getDatestr(v);
break;
case "DTEND;VALUE=DATE":
return;
case "X-ALT-DESC;FMTTYPE=text/html":
k = "DETAIL";
k = "detail";
break;
case "SUMMARY":
k = "LABEL";
let b;
[v, b] = v.split(" [");
sche["SUBJECT"] = b.substring(0, b.length - 1);
k = "label";
v = v.substring(0, v.indexOf(" ["));
break;
case "UID":
k = "uid";
v = v.split("-")[2];
break;
case "DTSTAMP":
case "CLASS":
case "SEQUENCE":
return;
case "DESCRIPTION":
k = "description";
case "URL":
k = "url";
sche.subjectID = v.substring(v.indexOf("se_") + 3, v.indexOf("&m"));
break;
default:
errArr.push(k);
......@@ -70,19 +69,29 @@ function insert(str, idxs, char) {
return nstr;
}
function dateTimeToDate(str) {
const [a, b] = str.split("T");
return new Date(insert(a, [4, 6, 8], "-") + "T" + insert(b, [2, 4, 6], ":"));
/**
* parse DateString
* @param {string} str "YYYYMMDDTHHMMSS"
* @returns {string} "YYYY-MM-DD"
*/
function getDatestr(str) {
const a = str.substring(0, str.indexOf("T"));
return insert(a, [4, 6, 8], "-");
}
function dateToDate(str) {
return new Date(insert(str, [4, 6, 8], "-"));
/**
* parse ZDateString
* @param {string} zstr "YYYYMMDDTHHMMSSZ" (UTC)
* @returns {string} "HH:MM:SS" (UTC+9)
*/
function getTimestr_z(zstr) {
const it = zstr.indexOf("T");
const a = zstr.substring(0, it);
const b = zstr.substring(it + 1, zstr.length - 1);
const date = new Date(
insert(a, [4, 6, 8], "-") + "T" + insert(b, [2, 4, 6], ":") + "+00:00"
);
return date.toTimeString().substring(0, 8);
}
// let result;
// fs.readFile("C:/Users/teddy/Downloads/data.ics", "utf8", (err, data) => {
// if (err) console.log(err);
// else result = parseICal(data);
// });
export { parseICal };
module.exports = parseICal;
......
const mysql = require("mysql");
const fs = require("fs");
const parseICal = require("./ICal");
function jcalToSQL(jcal, userID) {
const fsql = fs.readFileSync("server/libs/sql.pvdata", "utf8");
const [id, pw] = fsql.split("\r\n");
const connection = mysql.createConnection({
host: "localhost",
user: id,
password: pw,
database: "db",
});
connection.connect();
const commonCols = [
"userID",
"uid",
"label",
"subjectID",
"type",
"description",
"url",
"detail",
];
const dateScheCols = [...commonCols, "date"];
const timeScheCols = [...commonCols, "date", "startTime", "endTime"];
const dateQueryString =
"INSERT IGNORE INTO schedules_date (" +
dateScheCols.join(",") +
") VALUES ?";
const timeQueryString =
"INSERT IGNORE INTO schedules_time (" +
timeScheCols.join(",") +
") VALUES ?";
const dateSchedules = [];
const timeSchedules = [];
for (const sche of jcal) {
sche.userID = userID;
sche.type = "assignment";
if (sche.scheType === "date")
dateSchedules.push(dateScheCols.map((col) => sche[col]));
else if (sche.scheType === "time")
timeSchedules.push(timeScheCols.map((col) => sche[col]));
else console.log("unexpected scheType", sche);
}
connection.query(dateQueryString, [dateSchedules], (error, result) => {
if (error) console.log(error);
else console.log(`schedules_date | ${result.affectedRows} rows added`);
});
connection.query(timeQueryString, [timeSchedules], (error, result) => {
if (error) console.log(error);
else console.log(`schedules_time | ${result.affectedRows} rows added`);
});
connection.end();
}
// iCal의 소유주를 DB에 등록 후 userID get
// subjectID가 subjects, userID가 users에 있어야함
const fdata = fs.readFileSync("C:/Users/teddy/Downloads/data.ics", "utf8");
const jcal = parseICal(fdata);
jcalToSQL(jcal, 1);
module.exports = jcalToSQL;
const express = require("express");
const mysql = require("mysql");
const fs = require("fs");
const router = express.Router();
const [id, pw] = fs
.readFileSync("server/libs/sql.pvdata", "utf8")
.split("\r\n");
const connection = mysql.createConnection({
host: "localhost",
user: id,
password: pw,
database: "mydb",
});
router.get("/", (req, res) => {
res.send("DB Root");
});
router.get("/mytable", (req, res) => {
connection.query("SELECT * from mytable", (error, rows) => {
if (error) throw error;
console.log(rows);
res.send(rows);
});
});
module.exports = router;
......@@ -4,7 +4,8 @@ const fs = require("fs");
const cors = require("cors");
const { getPeed, getSubjects, login } = require("./libs/E_Campus.js");
const loginRouter = require("./routers/login");
const loginRouter = require("./routers/Login");
const dbRouter = require("./routers/DB");
const app = express();
......@@ -23,6 +24,7 @@ app.post("/", (req, res) => {
});
app.use("/login", loginRouter);
app.use("/db", dbRouter);
// fs.readFile("server/libs/login.pvdata", "utf8", async (err, data) => {
// if (err) {
......