-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-fetch.ts
More file actions
157 lines (139 loc) · 3.92 KB
/
api-fetch.ts
File metadata and controls
157 lines (139 loc) · 3.92 KB
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
import { Mwn } from "mwn";
import * as fs from "fs";
import git from "isomorphic-git";
import dayjs from "dayjs";
import { join } from "path";
import {
createCommitForRevision,
getRepoDir,
ensureRepoInitialized,
RevisionWithArticle,
sanitizeArticleName,
} from "./wiki-as-git";
const readExistingCommits = async (dir: string) => {
const commitMap = new Map<
string,
{ timestamp: number; articleName: string }
>();
try {
const commits = await git.log({ fs, dir, depth: 10000 });
for (const commit of commits) {
const timestamp = commit.commit.committer.timestamp;
const message = commit.commit.message;
const key = `${timestamp}:${message}`;
commitMap.set(key, { timestamp, articleName: "" });
}
} catch (err) {
void err;
}
return commitMap;
};
const rebuildRepoWithMergedHistory = async (
dir: string,
language: string,
newRevisions: RevisionWithArticle[],
vvv?: boolean,
) => {
console.debug(
`Rebuilding repository with ${newRevisions.length} new revisions`,
);
const otherArticles = new Map<string, string>();
const updatingArticleName = sanitizeArticleName(
newRevisions[0]?.articleName || "",
);
if (fs.existsSync(dir)) {
const files = fs.readdirSync(dir);
for (const file of files) {
if (file.endsWith(".wiki") && file !== ".wiki") {
const filePath = join(dir, file);
try {
const stat = fs.statSync(filePath);
if (stat.isFile() && file !== `${updatingArticleName}.wiki`) {
otherArticles.set(file, fs.readFileSync(filePath, "utf-8"));
}
} catch (err) {
void err;
}
}
}
}
const existingCommits = await readExistingCommits(dir);
const allRevisions: RevisionWithArticle[] = [];
for (const revData of newRevisions) {
const timestamp = dayjs(revData.revision.timestamp).unix();
const message = (revData.revision.comment || "").substring(0, 100) || "\n";
const key = `${timestamp}:${message}`;
if (!existingCommits.has(key)) {
allRevisions.push(revData);
}
}
if (allRevisions.length === 0) {
console.info(`No new revisions to add`);
return;
}
allRevisions.sort((a, b) => {
const dateA = dayjs(a.revision.timestamp).unix();
const dateB = dayjs(b.revision.timestamp).unix();
return dateA - dateB;
});
for (const revisionData of allRevisions) {
try {
for (const [fileName, content] of otherArticles.entries()) {
const filePath = join(dir, fileName);
if (!fs.existsSync(filePath)) {
fs.writeFileSync(filePath, content);
}
}
await createCommitForRevision(revisionData, dir, language, vvv);
} catch (error) {
console.error(
`Error processing revision for article ${revisionData.articleName}:`,
error,
);
}
}
};
export const fetchFromApi = async (
articleName: string,
language: string,
rvcontinue?: number,
vvv?: boolean,
) => {
const dir = getRepoDir(language);
await ensureRepoInitialized(dir);
const mwn = new Mwn({
apiUrl: `https://${language}.wikipedia.org/w/api.php`,
});
await mwn.getSiteInfo();
console.info(
`Retrieving article history for ${articleName} from ${
rvcontinue || "the beginning of history"
}`,
);
const newRevisions: RevisionWithArticle[] = [];
try {
for await (const revision of new mwn.Page(articleName).historyGen(
["timestamp", "user", "comment", "content"],
{
redirects: true,
format: "json",
rvslots: "main",
rvlimit: "max",
rvdir: "newer",
},
)) {
newRevisions.push({
revision,
articleName,
isXml: false,
});
}
} catch (err) {
console.error(err);
return;
}
console.info(`Fetched ${newRevisions.length} revisions for ${articleName}`);
if (newRevisions.length > 0) {
await rebuildRepoWithMergedHistory(dir, language, newRevisions, vvv);
}
};