Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复无法解析 LentilleDataResponse 的问题 #31

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions packages/archive/src/lib/judgement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ export default async function saveJudgements(
logger: BaseLogger,
prisma: PrismaClient,
) {
const res = await getResponse(
logger,
lgUrl(`/judgement?_contentOnly`, false),
false,
).then((response): Promise<JudgementResponse> => response.json());
const res = await getResponse(logger, lgUrl(`/judgement`, false), false).then(
(response): Promise<JudgementResponse> => response.json(),
);

const operations: PrismaPromise<unknown>[] = [];
const judgements = res.currentData.logs;
Expand Down
8 changes: 3 additions & 5 deletions packages/archive/src/lib/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ interface PostData {
}

interface PostListResponse {
code: 200;
currentTemplate: "DiscussList";
currentData: {
data: {
forum: ForumData | null;
publicForums: ForumData[];
posts: {
Expand All @@ -41,11 +39,11 @@ export default async function getPostList(
) {
const response = await getResponse(
logger,
lgUrl(`/discuss?_contentOnly&page=${page}`),
lgUrl(`/discuss?page=${page}`),
false,
);
const {
currentData: {
data: {
posts: { result },
},
} = (await response.json()) as PostListResponse;
Expand Down
6 changes: 5 additions & 1 deletion packages/archive/src/lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export async function getResponse(
retries = 1,
) {
const response = await fetch(url, {
headers: cookie ? { cookie: process.env.COOKIE! } : undefined,
headers: {
"x-luogu-type": "content-only",
"x-lentille-request": "content-only",
...(cookie ? { cookie: process.env.COOKIE! } : {}),
},
cache: "no-cache",
});
logger.info(
Expand Down
5 changes: 1 addition & 4 deletions packages/archive/src/lib/paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ export default async function savePaste(
prisma: PrismaClient,
id: string,
) {
const response = await getResponse(
logger,
lgUrl(`/paste/${id}?_contentOnly`, false),
);
const response = await getResponse(logger, lgUrl(`/paste/${id}`, false));
const json = (await response.json()) as
| { code: 403 | 404; currentData: LuoguError }
| { code: 200; currentData: { paste: Paste } };
Expand Down
14 changes: 6 additions & 8 deletions packages/archive/src/lib/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ interface ReplyData {
}

interface ResponseBody {
currentData: {
data: {
forum: ForumData;
post: PostData;
replies: ReplyData;
Expand All @@ -64,11 +64,9 @@ export async function savePost(
let allReplies: ReplyContent[] = [];

const fetchPage = (page: number) =>
getResponse(
logger,
lgUrl(`/discuss/${id}?_contentOnly&page=${page}`),
false,
).then((response): Promise<ResponseBody> => response.json());
getResponse(logger, lgUrl(`/discuss/${id}?page=${page}`), false).then(
(response): Promise<ResponseBody> => response.json(),
);

const saveReplies = async (replies: ReplyContent[]) => {
// eslint-disable-next-line no-restricted-syntax
Expand Down Expand Up @@ -127,7 +125,7 @@ export async function savePost(
});
};

const { post, replies, forum } = (await fetchPage(1)).currentData;
const { post, replies, forum } = (await fetchPage(1)).data;
const postTime = new Date(post.time * 1000);

await upsertUserSnapshot(prisma, post.author);
Expand Down Expand Up @@ -186,7 +184,7 @@ export async function savePost(
)[0];
for (let i = Math.min(pages, maxPages); i > 0; i -= 1) {
// eslint-disable-next-line no-await-in-loop
const { replies: newReplies } = (await fetchPage(i)).currentData;
const { replies: newReplies } = (await fetchPage(i)).data;
// eslint-disable-next-line no-await-in-loop
await saveReplies(newReplies.result);
if (newReplies.result[newReplies.result.length - 1].id <= lastReply)
Expand Down
Loading