forked from 2049bbs/2049bbs.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.js
257 lines (226 loc) · 6.71 KB
/
backup.js
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
// @ts-check
const youbbsBackupHelper = require("youbbs-backup-helper")
const { move, readdir, ensureDir, unlink, existsSync, pathExists, readFile } = require("fs-extra")
const path = require("path")
/** @typedef {import("youbbs-backup-helper/src/types").Article} Article */
/** @typedef {import("youbbs-backup-helper/src/types").Category} Category */
/** @typedef {import("youbbs-backup-helper/src/types").User} User */
const baseURL = "https://2049bbs.xyz"
const outputDir = "tmp"
const SEC = 1000 // ms
const MIN = 60 * SEC
const HOUR = 60 * MIN
const DAY = 24 * HOUR
const SCRIPT_START_TIME = new Date()
/**
* @param {string} src
* @param {string} dest
*/
const moveR = async (src, dest) => {
const files = await readdir(src)
await Promise.all(
files.map(async (f) => {
await ensureDir(path.dirname(path.join(dest, f)))
await move(path.join(src, f), path.join(dest, f), { overwrite: true })
})
)
}
/**
* @param {string} dir
*/
const _readDir = async (dir) => {
if (await pathExists(dir)) {
return readdir(dir)
} else {
return []
}
}
const categoryBackupHelper = new youbbsBackupHelper({
baseURL,
outputDir,
types: ["category"],
serializer: "markdown",
maxConcurrent: 10,
})
/** @type {Map<number, Category>} */
const categories = new Map()
categoryBackupHelper.pipe(
/** @param {Category} obj */
(obj) => {
categories.set(obj.cid, obj)
return obj
}
)
/** @type {Map<number, Date[]>} */
const addTimes = new Map()
const addTimeReg = /^\s*addTime: (\d{4}-\d{2}-\d{2}.*)/mg
const readPostAddTimes = () => {
return _readDir("_posts").then(async (files) => {
return await Promise.all(
files.map(async (f) => {
const data = await readFile(`_posts/${f}`, "utf-8")
const aid = +f.match(/(\d+).md/)[1]
const addTimeList = Array.from(data.matchAll(addTimeReg), m => new Date(m[1]))
addTimes.set(aid, addTimeList)
})
)
})
}
/**
* @param {Date} d
*/
const getDateString = (d) => {
return d.toISOString().match(/^(.+)T/)[1]
}
/**
* @param {Date} d
*/
const getTimeString = (d) => {
return d.toISOString().match(/T(.+)\.\d+Z/)[1]
}
/**
* @param {Date} d
*/
const getPast20Min = (d = SCRIPT_START_TIME) => {
const a = +d % (20 * MIN)
return new Date(+d - a)
}
/**
* @param {Date} d
*/
const isUninitializedTime = (d) => {
return getTimeString(d) == "16:00:00"
}
/**
* @param {Date} d
*/
const isToday = (d) => {
const now = +new Date()
const dif = +d - now
return dif < DAY && dif > -DAY
}
Promise.all([
categoryBackupHelper.start(),
readPostAddTimes()
]).then(() => {
const helper = new youbbsBackupHelper({
baseURL,
outputDir,
types: ["article"],
serializer: "markdown",
maxConcurrent: 5,
})
helper.pipe(
/**
* @param {Article} obj
*/
async (obj) => {
if (addTimes.has(obj.aid)) {
const addTimeList = addTimes.get(obj.aid)
if (addTimeList.length > 0) {
const [postAddTime, ...commentAddTimeList] = addTimeList
obj.addTime = postAddTime
obj.comments.forEach((comment, index) => {
if (commentAddTimeList[index]) {
comment.addTime = commentAddTimeList[index]
} else if (isUninitializedTime(comment.addTime) && isToday(comment.addTime)) {
comment.addTime = getPast20Min()
}
})
}
} else {
if (isUninitializedTime(obj.addTime) && isToday(obj.addTime)) {
obj.addTime = getPast20Min()
}
obj.comments.forEach((comment) => {
if (isUninitializedTime(comment.addTime) && isToday(comment.addTime)) {
comment.addTime = getPast20Min()
}
})
}
// update time
if (obj.comments.length > 0) {
obj["date"] = obj.comments[obj.comments.length - 1].addTime
} else {
obj["date"] = obj.addTime
}
const { cid } = obj
const cJson = categories.get(cid)
const category = cJson.name
obj["category"] = category
obj.content = obj.content.replace(/{{/g, "{ {") // add U+200A HAIR SPACE
.replace(/}}/g, "} }")
.replace(/{%/g, "{ %")
.replace(/%}/g, "% }")
return obj
}
)
helper.setFileNameFn(
/**
* @param {Article} obj
*/
(obj, id, fileExt) => {
const date = getDateString(obj.addTime)
return `${date}-${id}.${fileExt}`
}
)
return helper.start()
}).then(async () => {
await moveR(`${outputDir}/article`, "_posts")
await moveR(`${outputDir}/category`, "_category_info")
})
/** @type {Map<number, Date>} */
const usersRegTime = new Map()
_readDir("_users").then(async (files) => {
await Promise.all(
files.map(async (f) => {
const data = await readFile(`_users/${f}`, "utf-8")
const userID = parseInt(f.match(/(\d+).md/)[1])
const regTime = new Date(data.match(/^regTime: (\d{4}-\d{2}-\d{2}.*)/m)[1])
usersRegTime.set(userID, regTime)
})
)
}).then(() => {
const userBackupHelper = new youbbsBackupHelper({
baseURL,
outputDir,
types: ["user"],
serializer: "markdown",
maxConcurrent: 5,
})
userBackupHelper.pipe(
/**
* @param {User} obj
*/
async (obj) => {
const url = obj.url
delete obj.url
obj["userURL"] = url
if (usersRegTime.has(obj.userID)) {
obj.regTime = usersRegTime.get(obj.userID)
} else if (isUninitializedTime(obj.regTime) && isToday(obj.regTime)) {
obj.regTime = getPast20Min()
}
return obj
}
)
userBackupHelper.setFileNameFn(
/**
* @param {User} obj
*/
(obj, id, fileExt) => {
if (obj.avatar.length < 100 || !obj.avatar.startsWith("data:image/")) {
return ".fail"
} else {
return `${id}.${fileExt}`
}
}
)
userBackupHelper.start().then(() => {
const failFile = `${outputDir}/user/.fail`
if (existsSync(failFile)) {
unlink(failFile)
}
return moveR(`${outputDir}/user`, "_users")
})
})