From 5b31b4bf983a22b4cb93671134d1ad1029aca161 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E5=B0=8F=E5=A2=A8?= <2291200076@qq.com> Date: Fri, 30 Sep 2022 00:51:08 +0800 Subject: [PATCH] add utils --- config.json | 10 +++ index.js | 0 package-lock.json | 175 ++++++++++++++++++++++++++++++++++++++++- package.json | 4 +- utils/dateTimeUtils.js | 31 ++++++++ utils/dbPoolUtils.js | 85 ++++++++++++++++++++ utils/dbUtils.js | 31 ++++++++ utils/fileUtils.js | 49 ++++++++++++ utils/requestUtils.js | 42 ++++++++++ utils/sleepUtils.js | 7 ++ 10 files changed, 432 insertions(+), 2 deletions(-) create mode 100644 config.json create mode 100644 index.js create mode 100644 utils/dateTimeUtils.js create mode 100644 utils/dbPoolUtils.js create mode 100644 utils/dbUtils.js create mode 100644 utils/fileUtils.js create mode 100644 utils/requestUtils.js create mode 100644 utils/sleepUtils.js diff --git a/config.json b/config.json new file mode 100644 index 0000000..c8d4cb3 --- /dev/null +++ b/config.json @@ -0,0 +1,10 @@ +{ + "mysql": { + "charset": "utf8mb4", + "host": "localhost", + "user": "root", + "password": "root", + "port": 3306, + "database": "" + } +} \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..e69de29 diff --git a/package-lock.json b/package-lock.json index 386068c..8234ce3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,9 +11,11 @@ "dependencies": { "cheerio": "^1.0.0-rc.12", "fs": "^0.0.1-security", + "mysql": "^2.18.1", "node-schedule": "^2.1.0", "path": "^0.12.7", - "request": "^2.88.2" + "request": "^2.88.2", + "solarlunar": "^2.0.7" } }, "node_modules/ajv": { @@ -69,6 +71,14 @@ "tweetnacl": "^0.14.3" } }, + "node_modules/bignumber.js": { + "version": "9.0.0", + "resolved": "https://registry.npmmirror.com/bignumber.js/-/bignumber.js-9.0.0.tgz", + "integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A==", + "engines": { + "node": "*" + } + }, "node_modules/boolbase": { "version": "1.0.0", "resolved": "https://registry.npmmirror.com/boolbase/-/boolbase-1.0.0.tgz", @@ -417,6 +427,11 @@ "resolved": "https://registry.npmmirror.com/is-typedarray/-/is-typedarray-1.0.0.tgz", "integrity": "sha512-cyA56iCMHAh5CdzjJIa4aohJyeO1YbwLi3Jc35MmRU6poroFjIGZzUzupGiRPOjgHg9TLu43xbpwXk523fMxKA==" }, + "node_modules/isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmmirror.com/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==" + }, "node_modules/isstream": { "version": "0.1.2", "resolved": "https://registry.npmmirror.com/isstream/-/isstream-0.1.2.tgz", @@ -488,6 +503,25 @@ "node": ">= 0.6" } }, + "node_modules/mysql": { + "version": "2.18.1", + "resolved": "https://registry.npmmirror.com/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/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmmirror.com/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, "node_modules/node-schedule": { "version": "2.1.0", "resolved": "https://registry.npmmirror.com/node-schedule/-/node-schedule-2.1.0.tgz", @@ -564,6 +598,11 @@ "node": ">= 0.6.0" } }, + "node_modules/process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmmirror.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" + }, "node_modules/psl": { "version": "1.9.0", "resolved": "https://registry.npmmirror.com/psl/-/psl-1.9.0.tgz", @@ -585,6 +624,25 @@ "node": ">=0.6" } }, + "node_modules/readable-stream": { + "version": "2.3.7", + "resolved": "https://registry.npmmirror.com/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/readable-stream/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmmirror.com/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, "node_modules/request": { "version": "2.88.2", "resolved": "https://registry.npmmirror.com/request/-/request-2.88.2.tgz", @@ -626,11 +684,24 @@ "resolved": "https://registry.npmmirror.com/safer-buffer/-/safer-buffer-2.1.2.tgz", "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, + "node_modules/solarlunar": { + "version": "2.0.7", + "resolved": "https://registry.npmmirror.com/solarlunar/-/solarlunar-2.0.7.tgz", + "integrity": "sha512-2SfuCCgAAxFU5MTMYuKGbRgRLcPTJQf3azMEw/GmBpHXA7N2eAQJStSqktZJjnq4qRCboBPnqEB866+PCregag==" + }, "node_modules/sorted-array-functions": { "version": "1.3.0", "resolved": "https://registry.npmmirror.com/sorted-array-functions/-/sorted-array-functions-1.3.0.tgz", "integrity": "sha512-2sqgzeFlid6N4Z2fUQ1cvFmTOLRi/sEDzSQ0OKYchqgoPmQBVyM3959qYx3fpS6Esef80KjmpgPeEr028dP3OA==" }, + "node_modules/sqlstring": { + "version": "2.3.1", + "resolved": "https://registry.npmmirror.com/sqlstring/-/sqlstring-2.3.1.tgz", + "integrity": "sha512-ooAzh/7dxIG5+uDik1z/Rd1vli0+38izZhGzSa34FwR7IbelPWCCKSNIl8jlL/F7ERvy8CB2jNeM1E9i9mXMAQ==", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/sshpk": { "version": "1.17.0", "resolved": "https://registry.npmmirror.com/sshpk/-/sshpk-1.17.0.tgz", @@ -655,6 +726,19 @@ "node": ">=0.10.0" } }, + "node_modules/string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmmirror.com/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "dependencies": { + "safe-buffer": "~5.1.0" + } + }, + "node_modules/string_decoder/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmmirror.com/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, "node_modules/tough-cookie": { "version": "2.5.0", "resolved": "https://registry.npmmirror.com/tough-cookie/-/tough-cookie-2.5.0.tgz", @@ -699,6 +783,11 @@ "inherits": "2.0.3" } }, + "node_modules/util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmmirror.com/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" + }, "node_modules/uuid": { "version": "3.4.0", "resolved": "https://registry.npmmirror.com/uuid/-/uuid-3.4.0.tgz", @@ -770,6 +859,11 @@ "tweetnacl": "^0.14.3" } }, + "bignumber.js": { + "version": "9.0.0", + "resolved": "https://registry.npmmirror.com/bignumber.js/-/bignumber.js-9.0.0.tgz", + "integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A==" + }, "boolbase": { "version": "1.0.0", "resolved": "https://registry.npmmirror.com/boolbase/-/boolbase-1.0.0.tgz", @@ -1062,6 +1156,11 @@ "resolved": "https://registry.npmmirror.com/is-typedarray/-/is-typedarray-1.0.0.tgz", "integrity": "sha512-cyA56iCMHAh5CdzjJIa4aohJyeO1YbwLi3Jc35MmRU6poroFjIGZzUzupGiRPOjgHg9TLu43xbpwXk523fMxKA==" }, + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmmirror.com/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==" + }, "isstream": { "version": "0.1.2", "resolved": "https://registry.npmmirror.com/isstream/-/isstream-0.1.2.tgz", @@ -1121,6 +1220,24 @@ "mime-db": "1.52.0" } }, + "mysql": { + "version": "2.18.1", + "resolved": "https://registry.npmmirror.com/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": { + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmmirror.com/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + } + } + }, "node-schedule": { "version": "2.1.0", "resolved": "https://registry.npmmirror.com/node-schedule/-/node-schedule-2.1.0.tgz", @@ -1185,6 +1302,11 @@ "resolved": "https://registry.npmmirror.com/process/-/process-0.11.10.tgz", "integrity": "sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A==" }, + "process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmmirror.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" + }, "psl": { "version": "1.9.0", "resolved": "https://registry.npmmirror.com/psl/-/psl-1.9.0.tgz", @@ -1200,6 +1322,27 @@ "resolved": "https://registry.npmmirror.com/qs/-/qs-6.5.3.tgz", "integrity": "sha512-qxXIEh4pCGfHICj1mAJQ2/2XVZkjCDTcEgfoSQxc/fYivUZxTkk7L3bDBJSoNrEzXI17oUO5Dp07ktqE5KzczA==" }, + "readable-stream": { + "version": "2.3.7", + "resolved": "https://registry.npmmirror.com/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" + }, + "dependencies": { + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmmirror.com/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + } + } + }, "request": { "version": "2.88.2", "resolved": "https://registry.npmmirror.com/request/-/request-2.88.2.tgz", @@ -1237,11 +1380,21 @@ "resolved": "https://registry.npmmirror.com/safer-buffer/-/safer-buffer-2.1.2.tgz", "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, + "solarlunar": { + "version": "2.0.7", + "resolved": "https://registry.npmmirror.com/solarlunar/-/solarlunar-2.0.7.tgz", + "integrity": "sha512-2SfuCCgAAxFU5MTMYuKGbRgRLcPTJQf3azMEw/GmBpHXA7N2eAQJStSqktZJjnq4qRCboBPnqEB866+PCregag==" + }, "sorted-array-functions": { "version": "1.3.0", "resolved": "https://registry.npmmirror.com/sorted-array-functions/-/sorted-array-functions-1.3.0.tgz", "integrity": "sha512-2sqgzeFlid6N4Z2fUQ1cvFmTOLRi/sEDzSQ0OKYchqgoPmQBVyM3959qYx3fpS6Esef80KjmpgPeEr028dP3OA==" }, + "sqlstring": { + "version": "2.3.1", + "resolved": "https://registry.npmmirror.com/sqlstring/-/sqlstring-2.3.1.tgz", + "integrity": "sha512-ooAzh/7dxIG5+uDik1z/Rd1vli0+38izZhGzSa34FwR7IbelPWCCKSNIl8jlL/F7ERvy8CB2jNeM1E9i9mXMAQ==" + }, "sshpk": { "version": "1.17.0", "resolved": "https://registry.npmmirror.com/sshpk/-/sshpk-1.17.0.tgz", @@ -1258,6 +1411,21 @@ "tweetnacl": "~0.14.0" } }, + "string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmmirror.com/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "requires": { + "safe-buffer": "~5.1.0" + }, + "dependencies": { + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmmirror.com/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + } + } + }, "tough-cookie": { "version": "2.5.0", "resolved": "https://registry.npmmirror.com/tough-cookie/-/tough-cookie-2.5.0.tgz", @@ -1296,6 +1464,11 @@ "inherits": "2.0.3" } }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmmirror.com/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" + }, "uuid": { "version": "3.4.0", "resolved": "https://registry.npmmirror.com/uuid/-/uuid-3.4.0.tgz", diff --git a/package.json b/package.json index e738525..05f8ea5 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,10 @@ "dependencies": { "cheerio": "^1.0.0-rc.12", "fs": "^0.0.1-security", + "mysql": "^2.18.1", "node-schedule": "^2.1.0", "path": "^0.12.7", - "request": "^2.88.2" + "request": "^2.88.2", + "solarlunar": "^2.0.7" } } diff --git a/utils/dateTimeUtils.js b/utils/dateTimeUtils.js new file mode 100644 index 0000000..cab8ab1 --- /dev/null +++ b/utils/dateTimeUtils.js @@ -0,0 +1,31 @@ +/** + * refer: https://www.npmjs.com/package/solarlunar + */ +const solarLunar = require('solarlunar'); + + +// 获取 今日 到 阳历指定日期 的间隔 +function getSpanDays_solar(solarYear, solarMonth, solarDay) { + let nowDate_00_00 = new Date(new Date().toLocaleDateString()); /* 当日0点 */ + + let festivalDayDate = new Date(`${solarYear}-${solarMonth}-${solarDay} 00:00:00`); + let festivalSpanDays = ((festivalDayDate - nowDate_00_00) / (24 * 3600 * 1000)); + // console.log(festivalSpanDays, nowDate_00_00, festivalDayDate); + return festivalSpanDays; +} + +// 获取 今日 到 农历指定日期 的间隔 +function getSpanDays_lunar(lunarYear, lunarMonth, lunarDay) { + let nowDate_00_00 = new Date(new Date().toLocaleDateString()); /* 当日0点 */ + + let festivalDay = solarLunar.lunar2solar(lunarYear, lunarMonth, lunarDay); // 农历 + let festivalDayDate = new Date(`${festivalDay.cYear}-${festivalDay.cMonth}-${festivalDay.cDay} 00:00:00`); // 2022-09-09T16:00:00.000Z + let festivalSpanDays = ((festivalDayDate - nowDate_00_00) / (24 * 3600 * 1000)); + // console.log(festivalSpanDays, nowDate_00_00, festivalDayDate); + return festivalSpanDays; +} + +module.exports = { + getSpanDays_solar, + getSpanDays_lunar +} \ No newline at end of file diff --git a/utils/dbPoolUtils.js b/utils/dbPoolUtils.js new file mode 100644 index 0000000..9e89e17 --- /dev/null +++ b/utils/dbPoolUtils.js @@ -0,0 +1,85 @@ +const mysql = require('mysql'); +const fs = require('fs'); +const path = require('path'); + +let globalConfig = JSON.parse(fs.readFileSync(path.join(__dirname, '../config.json'), 'utf8')); +let pool = null; + +function createPool(database) { + let config = { + connectionLimit: 10, //连接数量,默认是10 + ...globalConfig.mysql, + database: database, + }; + console.log(config); + + //创建数据库连接池 + pool = mysql.createPool(config); + + // //从连接池中获取连接时,将触发该事件 + // pool.on('acquire', function (conn) { + // console.log('获取连接', conn.threadId); + // }); + + // //在连接池中建立新连接时,将触发该事件 + // pool.on('connection', function (conn) { + // console.log('建立新连接', conn.threadId); + // }); + + // //等待可用连接时,将触发该事件 + // pool.on('enqueue', function () { + // console.log('等待可用连接'); + // }); + + // //当连接释放回池中时,触发该事件 + // pool.on('release', function (conn) { + // console.log('连接被释放回池中', conn.threadId); + // }); +} + +async function query(sql) { + if (!pool) { + console.error('Database connection pool is not initialized yet.'); + return; + } + return await new Promise(function (resolve, reject) { + //pool.query()方法可以自动的帮我们在连接池中获取可用连接 + pool.query(sql, function (err, data) { + if (err) reject(err); + // console.log(data); + resolve(data); + }); + + // //当然我们也可以手动获取可用连接 + // pool.getConnection(function (err, conn) { + // if (err) { + // throw err; + // } + // conn.query('select * from `order`', function (err, data) { + // if (err) { + // throw err; + // } + // data.forEach(function (value) { + // console.log(value.id, value.order_id, value.user_id); + // }); + + // //连接用完之后,需要释放,重新放回连接池中。 + // //注意这里并没有销毁该连接,该连接仍然可用,但需要重新获取 + // conn.release(); + // }); + // }); + }); +} + +function close() { + if (!pool) return; + pool.end(function (err) { + console.log('关闭连接池', err); + }); + pool = null; +} + +module.exports = { + query: query, + close: close, +} \ No newline at end of file diff --git a/utils/dbUtils.js b/utils/dbUtils.js new file mode 100644 index 0000000..ff3e90b --- /dev/null +++ b/utils/dbUtils.js @@ -0,0 +1,31 @@ +//引入MySQL +const mysql = require('mysql'); +const fs = require('fs'); +const path = require('path'); + +let globalConfig = JSON.parse(fs.readFileSync(path.join(__dirname, '../config.json'), 'utf8')); +let config = { + ...globalConfig.mysql, +}; +console.log(config); + +async function query(sql) { + //通过MySQL中方法创建链接对象 + var connection = mysql.createConnection(config); + //开始连接 + connection.connect(); + //执行SQL语句 (添加、删除、更新、查询) + connection.query(sql, (err, result) => { + if (err) { + console.error('err', err); + return; + } + console.log('result', result); + }) + //最后需要关闭连接 + connection.end(); +} + +module.exports = { + query: query, +} \ No newline at end of file diff --git a/utils/fileUtils.js b/utils/fileUtils.js new file mode 100644 index 0000000..395fe43 --- /dev/null +++ b/utils/fileUtils.js @@ -0,0 +1,49 @@ +const fs = require('fs'); +const path = require('path'); + +// 创建目录 +async function createFolder(folderToCreate) { + let currentFolder = path.join(folderToCreate); + let parentFolder = path.join(currentFolder, '../'); + // console.log({ currentFolder: currentFolder, parentFolder: parentFolder }); + if (!fs.existsSync(currentFolder)) { + // 文件夹不存在,创建文件夹 + createFolder(parentFolder); // 保证父级文件夹存在 + fs.mkdirSync(currentFolder); // 创建当前级文件夹 + } else { + // 否则就什么也不做 + } +} + +// 保存 JSON +function saveJSON({ saveFolder, now, fileNameSuffix, object, compress = true, uncompress = true }) { + if (LATEST_DATA_ONLY) return; + + let year = now.substring(0, 4); + let month = now.substring(5, 7); + let day = now.substring(8, 10); + let hour = now.substring(11, 13); + let minute = now.substring(14, 16); + // console.log(now); + // console.log( "year, month, day, hour, minute: " + year + ", " + month + ", " + day + ", " + hour + ", " + minute); + + // 创建当前文件夹 + let folder = `${saveFolder}/${fileNameSuffix}/${year}/${month}/${day}`; + createFolder(folder); + let fileName = `${folder}/${year}${month}${day}_${hour}${minute}`; + + // 生成文件名 + // '2022-07-23T10:11:38.650Z' => '20220723_1011' + // let fileName = now.replace(/T/, '_').replace(/:\d{2}.\d{3}Z/, '').replace(/[-:]/g, ''); + // console.log(`fileName is ${fileName}`); + + if (compress) + fs.writeFileSync(`${fileName}.min.json`, JSON.stringify(object)); + if (uncompress) + fs.writeFileSync(`${fileName}.json`, JSON.stringify(object, "", "\t")); +} + +module.exports = { + createFolder, + saveJSON, +} \ No newline at end of file diff --git a/utils/requestUtils.js b/utils/requestUtils.js new file mode 100644 index 0000000..0f8ef1a --- /dev/null +++ b/utils/requestUtils.js @@ -0,0 +1,42 @@ +const request = require('request'); + +async function get(url) { + return await query({ + method: 'GET', + url: url, + json: true, + }); +} + +async function getApiResult(url, params = null) { + return await query({ + method: 'GET', + url: url, + ...params ?? {} + }); +} + +async function query(opts) { + var return_data = await new Promise((resolve, reject) => { + // console.log("opts", opts); + request(opts, (error, response, result) => { + // console.log("error, result", error, result); + if (!error && (response.statusCode == 200)) { + // 请求成功 + resolve(result); + } else { + // 请求失败 + console.error(`error is ${error}`); + reject(error); + } + }); + }); + // console.log(`return_data is ${JSON.stringify(return_data)}`); + return return_data; +} + +module.exports = { + get: get, + getApiResult: getApiResult, + query: query, +} diff --git a/utils/sleepUtils.js b/utils/sleepUtils.js new file mode 100644 index 0000000..8cbf16d --- /dev/null +++ b/utils/sleepUtils.js @@ -0,0 +1,7 @@ +async function sleep(ms) { + await new Promise(function (resolve, reject) { + setTimeout(resolve, ms); + }); +} + +module.exports = sleep; \ No newline at end of file