-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.ts
executable file
·222 lines (198 loc) · 7.21 KB
/
main.ts
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
import {
COOKIE,
DATA_PORTABILITY_ARCHIVE_PATH,
ENABLE_JAVASCRIPT,
POSTS,
PROJECTS,
SKIP_POSTS,
SKIP_LIKES,
} from "./src/config.ts";
import { CohostContext, POST_URL_REGEX } from "./src/context.ts";
import { loadAllLikedPosts } from "./src/likes.ts";
import { FROM_POST_PAGE_TO_ROOT, loadPostPage } from "./src/post-page.ts";
import { loadAllProjectPosts } from "./src/project.ts";
import { IPost } from "./src/model.ts";
import { readDataPortabilityArchiveItems } from "./src/data-portability-archive.ts";
import { loadCohostSource } from "./src/cohost-source.ts";
import { generateAllScripts } from "./src/scripts/index.ts";
import { rewritePost } from "./src/post.ts";
import { generateAllIndices } from "./src/post-index.ts";
import { checkForUpdates } from "./src/changelog.ts";
await checkForUpdates();
const ctx = new CohostContext(COOKIE, "out");
await ctx.init();
let isLoggedIn = false;
let currentProjectHandle = null;
{
// check that login actually worked
const loginStateResponse = await ctx.get(
"https://cohost.org/api/v1/trpc/login.loggedIn,projects.listEditedProjects?batch=1&input=%7B%7D",
);
const loginState = await loginStateResponse.json();
if (!loginState[0].result.data.loggedIn) {
console.error(
"\x1b[33mwarning:\nNot logged in. Please update your cookie configuration if cohost.org still exists\x1b[m\n\n",
);
} else {
const currentProjectId = loginState[0].result.data.projectId;
const currentProject = loginState[1].result.data.projects.find((proj: { projectId: number }) =>
proj.projectId === currentProjectId
);
if (!currentProject) {
throw new Error(
`invalid state: logged in as project ${currentProjectId}, but this is not an edited project`,
);
}
currentProjectHandle = currentProject.handle;
console.log(
`logged in as ${
loginState[0].result.data.email
} / @${currentProjectHandle}`,
);
isLoggedIn = true;
}
}
// JSON data
if (isLoggedIn) {
// legacy liked posts
if (await ctx.hasFile('liked.json')) {
console.log('');
console.log(`There’s a list of liked posts here using an older format. It’s unclear what page you were logged in as when loading them.`);
let likedPostsHandle: string | null = null;
if (confirm(`Did you load these liked posts as @${currentProjectHandle}?`)) {
likedPostsHandle = currentProjectHandle;
} else {
while (true) {
likedPostsHandle = prompt("What’s the handle of the page these liked posts are for?")?.trim() ?? null;
if (likedPostsHandle) {
if (confirm(`It was @${likedPostsHandle}?`)) {
break;
}
} else {
break;
}
}
}
if (!likedPostsHandle) {
console.log('no input. exiting');
Deno.exit(1);
}
await Deno.mkdir(ctx.getCleanPath(likedPostsHandle), { recursive: true });
await Deno.rename(ctx.getCleanPath('liked.json'), ctx.getCleanPath(`${likedPostsHandle}/liked.json`));
}
// load all liked posts for the current page
if (!(await ctx.hasFile(`${currentProjectHandle}/liked.json`)) && !SKIP_LIKES) {
console.log(`loading likes for @${currentProjectHandle}`);
const liked = await loadAllLikedPosts(ctx);
await ctx.writeLargeJson(`${currentProjectHandle}/liked.json`, liked);
}
// load all project posts
for (const handle of PROJECTS) {
if (!(await ctx.hasFile(`${handle}/posts.json`))) {
const posts = await loadAllProjectPosts(ctx, handle);
await ctx.write(`${handle}/posts.json`, JSON.stringify(posts));
}
}
} else {
console.log(
"\x1b[33mnot logged in: skipping liked posts and project posts \x1b[m",
);
}
// javascript
if (ENABLE_JAVASCRIPT) {
const dir = await loadCohostSource(ctx);
await generateAllScripts(ctx, dir);
}
const errors: { url: string; error: Error }[] = [];
// Single post pages
{
const allProjectDirsProbably: string[] = [];
for await (const item of Deno.readDir(ctx.getCleanPath(''))) {
if (item.isDirectory) allProjectDirsProbably.push(item.name);
}
const likedPosts = await Promise.all(
allProjectDirsProbably.map(async (handle) => {
if (SKIP_LIKES) return [];
const file = `${handle}/liked.json`;
if (await ctx.hasFile(file)) {
return ctx.readLargeJson(`${handle}/liked.json`);
} else {
return [];
}
}),
) as IPost[][];
const projectPosts = await Promise.all(
allProjectDirsProbably.map(async (handle) => {
const file = `${handle}/posts.json`;
if (await ctx.hasFile(file)) {
return ctx.readJson(`${handle}/posts.json`);
} else {
return [];
}
}),
) as IPost[][];
const allPosts = [
...likedPosts.flatMap(x => x),
...projectPosts.flatMap((x) => x),
];
const loadPostPageAndCollectError = async (url: string) => {
try {
await loadPostPage(ctx, url);
} catch (error) {
console.error(`\x1b[31mFailed! ${error}\x1b[m`);
errors.push({ url, error });
}
};
for (const post of allPosts) {
if (SKIP_POSTS.includes(post.postId)) continue;
console.log(`~~ processing post ${post.singlePostPageUrl}`);
await loadPostPageAndCollectError(post.singlePostPageUrl);
}
// it can happen that we've cached data for a post that is now a 404.
// I suppose we can try loading resources for those as well?
for (const post of allPosts) {
try {
await rewritePost(ctx, post, FROM_POST_PAGE_TO_ROOT);
} catch {
// oh well!!
}
}
const dpaPostURLs: string[] = [];
if (DATA_PORTABILITY_ARCHIVE_PATH) {
const items = await readDataPortabilityArchiveItems(
DATA_PORTABILITY_ARCHIVE_PATH,
);
for (const ask of items.asks) {
if (ask.responsePost) {
dpaPostURLs.push(ask.responsePost);
}
}
for (const comment of items.comments) {
if (comment.post) {
dpaPostURLs.push(comment.post);
} else {
console.log(`comment ${comment.commentId} has no post`);
}
}
}
for (const post of [...POSTS, ...dpaPostURLs]) {
const probablyThePostId = +(post.match(POST_URL_REGEX)?.[2] || "");
if (SKIP_POSTS.includes(probablyThePostId)) continue;
console.log(`~~ processing additional post ${post}`);
await loadPostPageAndCollectError(post);
}
}
{
await generateAllIndices(ctx, errors);
}
await ctx.finalize();
if (errors.length) {
console.log(
`\x1b[32mDone, \x1b[33mwith ${errors.length} error${
errors.length === 1 ? "" : "s"
}\x1b[m`,
);
for (const { url, error } of errors) console.log(`${url}: ${error}`);
} else {
console.log("\x1b[32mDone\x1b[m");
}