reserve/data.js
2023-09-20 19:01:22 +08:00

51 lines
1.3 KiB
JavaScript

const fs = require('fs')
const moment = require('moment');
function get_data() {
const now = moment();
const weekYear = now.isoWeekYear()
const weekNumber = now.isoWeek()
const filename = `./data/current.json`
if(!fs.existsSync(filename)) {
// create data of this week
clear()
}
const content = fs.readFileSync(filename).toString();
json = JSON.parse(content)
return json;
}
function clear() {
const now = moment();
const weekYear = now.isoWeekYear()
const weekNumber = now.isoWeek()
const filename = `./data/current.json`
fs.writeFileSync(`./data/${weekYear}-${weekNumber}.json`, fs.readFileSync(filename).toString())
let tmp_json = JSON.parse(fs.readFileSync("./example.json").toString())
tmp_json['ganshi'] = generateRandomString(8)
tmp_json['yuedui'] = generateRandomString(8)
tmp_json['sheyuan'] = generateRandomString(8)
fs.writeFileSync(filename, JSON.stringify(tmp_json))
}
function generateRandomString(length) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
module.exports = { get_data, clear }