-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.mjs
53 lines (43 loc) · 1.25 KB
/
dump.mjs
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
import fetch from 'node-fetch';
import process from 'process';
import fs from 'fs/promises';
const base = process.argv[2];
async function fetchPage(pn) {
console.log(`Fetching page ${pn}`);
const listReq = await fetch(`${base}/posts/${pn}`);
const list = await listReq.json();
return list;
}
async function fetchPost(url) {
const postReq = await fetch(`${base}/post/${url}`);
const post = await postReq.json();
const publish = new Date(post.post_time);
const md = `---
title: ${post.topic}
tags: ${post.tags.join(', ')}
force_publish_time: ${publish.toISOString()}
force_update_time: ${new Date(post.update_time).toISOString()}
---
${post.content}
`;
const month = (publish.getMonth()+1).toString().padStart(2, '0');
const date = publish.getDate().toString().padStart(2, '0');
const prefix = `${publish.getFullYear()}-${month}-${date}`;
const filename = `${prefix}-${post.url}.md`;
console.log(`Writing ${post.url}`);
await fs.writeFile(filename, md);
return post;
}
async function work() {
let posts = [];
let pn = 1;
while(true) {
const l = await fetchPage(pn);
posts = posts.concat(l.posts);
if(!l.hasNext) break;
pn += 1;
}
// console.log(posts);
await Promise.all(posts.map(p => fetchPost(p.url)));
}
work();