Skip to content

Commit

Permalink
Make luogu url configurable (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
immccn123 authored Oct 6, 2024
1 parent 2d461f4 commit 5cdd854
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/archive/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@logtail/pino": "^0.4.22",
"@prisma/client": "^5.15.0",
"@socket.io/admin-ui": "^0.5.1",
"dotenv": "^16.4.5",
"fastify": "^4.28.0",
"fastify-plugin": "^4.5.1",
"jsdom": "^22.1.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/archive/src/lib/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { PrismaClient, PrismaPromise } from "@prisma/client";
import { getResponse } from "./parser";
import type { UserSummary } from "./user";
import { upsertUserSnapshot } from "./user";
import lgUrl from "../utils/url";

export interface Activity {
content: string;
Expand All @@ -30,7 +31,7 @@ export async function saveActivityPage(
) {
const res = await getResponse(
logger,
`https://www.luogu.com.cn/api/feed/list?page=${page}`,
lgUrl(`/api/feed/list?page=${page}`, false),
false,
).then((response): Promise<Body> => response.json());

Expand Down
3 changes: 2 additions & 1 deletion packages/archive/src/lib/judgement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { PrismaClient, PrismaPromise } from "@prisma/client";
import type { BaseLogger } from "pino";
import { getResponse } from "./parser";
import { UserSummary, upsertUserSnapshot } from "./user";
import lgUrl from "../utils/url";

interface JudgementBody {
user: UserSummary;
Expand All @@ -23,7 +24,7 @@ export default async function saveJudgements(
) {
const res = await getResponse(
logger,
`https://www.luogu.com.cn/judgement?_contentOnly`,
lgUrl(`/judgement?_contentOnly`, false),
false,
).then((response): Promise<JudgementResponse> => response.json());

Expand Down
3 changes: 2 additions & 1 deletion packages/archive/src/lib/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { PrismaClient } from "@prisma/client";
import type { UserSummary } from "./user";
import type { ForumData, ReplyContent } from "./post";
import { getResponse } from "./parser";
import lgUrl from "../utils/url";

interface PostData {
id: number;
Expand Down Expand Up @@ -40,7 +41,7 @@ export default async function getPostList(
) {
const response = await getResponse(
logger,
`https://www.luogu.com/discuss?_contentOnly&page=${page}`,
lgUrl(`/discuss?_contentOnly&page=${page}`),
false,
);
const {
Expand Down
3 changes: 2 additions & 1 deletion packages/archive/src/lib/paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { PrismaClient } from "@prisma/client";
import { getResponse } from "./parser";
import { type UserSummary } from "./user";
import { upsertUserSnapshot } from "./user";
import lgUrl from "../utils/url";

interface Paste {
data: string;
Expand All @@ -25,7 +26,7 @@ export default async function savePaste(
) {
const response = await getResponse(
logger,
`https://www.luogu.com.cn/paste/${id}?_contentOnly`,
lgUrl(`/paste/${id}?_contentOnly`, false),
);
const json = (await response.json()) as
| { code: 403 | 404; currentData: LuoguError }
Expand Down
3 changes: 2 additions & 1 deletion packages/archive/src/lib/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { PostSnapshot, PrismaClient } from "@prisma/client";
import { getResponse } from "./parser";
import type { ServerToClientEvents } from "../plugins/socket.io";
import { UserSummary, upsertUserSnapshot } from "./user";
import lgUrl from "../utils/url";

const PAGES_PER_SAVE = parseInt(process.env.PAGES_PER_SAVE ?? "64", 10);
export const emitters: Record<number, EventEmitter> = {};
Expand Down Expand Up @@ -65,7 +66,7 @@ export async function savePost(
const fetchPage = (page: number) =>
getResponse(
logger,
`https://www.luogu.com/discuss/${id}?_contentOnly&page=${page}`,
lgUrl(`/discuss/${id}?_contentOnly&page=${page}`),
false,
).then((response): Promise<ResponseBody> => response.json());

Expand Down
2 changes: 2 additions & 0 deletions packages/archive/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import routes from "./plugins/routes";
import io from "./plugins/socket.io";
import cron from "./plugins/cron";

import "dotenv/config";

const fastify = Fastify({
logger: process.env.SOURCE_TOKEN
? pino(
Expand Down
7 changes: 7 additions & 0 deletions packages/archive/src/utils/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const backendGlobal = process.env.LUOGU_URL ?? "https://www.luogu.com";
const backendCN = process.env.LUOGU_CN_URL ?? "https://www.luogu.com.cn";

export default function lgUrl(path: string, global = true): string {
if (global) return `${backendGlobal}${path}`;
return `${backendCN}${path}`;
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5cdd854

Please sign in to comment.