Skip to content

Commit 5cdd854

Browse files
authored
Make luogu url configurable (#29)
1 parent 2d461f4 commit 5cdd854

File tree

9 files changed

+23
-5
lines changed

9 files changed

+23
-5
lines changed

packages/archive/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"@logtail/pino": "^0.4.22",
2020
"@prisma/client": "^5.15.0",
2121
"@socket.io/admin-ui": "^0.5.1",
22+
"dotenv": "^16.4.5",
2223
"fastify": "^4.28.0",
2324
"fastify-plugin": "^4.5.1",
2425
"jsdom": "^22.1.0",

packages/archive/src/lib/activity.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { PrismaClient, PrismaPromise } from "@prisma/client";
55
import { getResponse } from "./parser";
66
import type { UserSummary } from "./user";
77
import { upsertUserSnapshot } from "./user";
8+
import lgUrl from "../utils/url";
89

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

packages/archive/src/lib/judgement.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { PrismaClient, PrismaPromise } from "@prisma/client";
22
import type { BaseLogger } from "pino";
33
import { getResponse } from "./parser";
44
import { UserSummary, upsertUserSnapshot } from "./user";
5+
import lgUrl from "../utils/url";
56

67
interface JudgementBody {
78
user: UserSummary;
@@ -23,7 +24,7 @@ export default async function saveJudgements(
2324
) {
2425
const res = await getResponse(
2526
logger,
26-
`https://www.luogu.com.cn/judgement?_contentOnly`,
27+
lgUrl(`/judgement?_contentOnly`, false),
2728
false,
2829
).then((response): Promise<JudgementResponse> => response.json());
2930

packages/archive/src/lib/list.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { PrismaClient } from "@prisma/client";
33
import type { UserSummary } from "./user";
44
import type { ForumData, ReplyContent } from "./post";
55
import { getResponse } from "./parser";
6+
import lgUrl from "../utils/url";
67

78
interface PostData {
89
id: number;
@@ -40,7 +41,7 @@ export default async function getPostList(
4041
) {
4142
const response = await getResponse(
4243
logger,
43-
`https://www.luogu.com/discuss?_contentOnly&page=${page}`,
44+
lgUrl(`/discuss?_contentOnly&page=${page}`),
4445
false,
4546
);
4647
const {

packages/archive/src/lib/paste.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { PrismaClient } from "@prisma/client";
33
import { getResponse } from "./parser";
44
import { type UserSummary } from "./user";
55
import { upsertUserSnapshot } from "./user";
6+
import lgUrl from "../utils/url";
67

78
interface Paste {
89
data: string;
@@ -25,7 +26,7 @@ export default async function savePaste(
2526
) {
2627
const response = await getResponse(
2728
logger,
28-
`https://www.luogu.com.cn/paste/${id}?_contentOnly`,
29+
lgUrl(`/paste/${id}?_contentOnly`, false),
2930
);
3031
const json = (await response.json()) as
3132
| { code: 403 | 404; currentData: LuoguError }

packages/archive/src/lib/post.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { PostSnapshot, PrismaClient } from "@prisma/client";
55
import { getResponse } from "./parser";
66
import type { ServerToClientEvents } from "../plugins/socket.io";
77
import { UserSummary, upsertUserSnapshot } from "./user";
8+
import lgUrl from "../utils/url";
89

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

packages/archive/src/server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import routes from "./plugins/routes";
66
import io from "./plugins/socket.io";
77
import cron from "./plugins/cron";
88

9+
import "dotenv/config";
10+
911
const fastify = Fastify({
1012
logger: process.env.SOURCE_TOKEN
1113
? pino(

packages/archive/src/utils/url.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const backendGlobal = process.env.LUOGU_URL ?? "https://www.luogu.com";
2+
const backendCN = process.env.LUOGU_CN_URL ?? "https://www.luogu.com.cn";
3+
4+
export default function lgUrl(path: string, global = true): string {
5+
if (global) return `${backendGlobal}${path}`;
6+
return `${backendCN}${path}`;
7+
}

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)