From 7d260a9a6e4213b77836ed6eabdb2e733fde9be3 Mon Sep 17 00:00:00 2001 From: Imken Luo Date: Sun, 6 Oct 2024 22:40:50 +0800 Subject: [PATCH] make luogu url configurable --- packages/archive/package.json | 1 + packages/archive/src/lib/activity.ts | 7 ++++--- packages/archive/src/lib/judgement.ts | 7 ++++--- packages/archive/src/lib/list.ts | 3 ++- packages/archive/src/lib/paste.ts | 7 ++++--- packages/archive/src/lib/post.ts | 9 +++++---- packages/archive/src/lib/user.ts | 2 +- packages/archive/src/server.ts | 2 ++ packages/archive/src/utils/url.ts | 7 +++++++ pnpm-lock.yaml | 3 +++ 10 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 packages/archive/src/utils/url.ts diff --git a/packages/archive/package.json b/packages/archive/package.json index c9fd7c3..d863490 100644 --- a/packages/archive/package.json +++ b/packages/archive/package.json @@ -18,6 +18,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", diff --git a/packages/archive/src/lib/activity.ts b/packages/archive/src/lib/activity.ts index 5504921..33bc935 100644 --- a/packages/archive/src/lib/activity.ts +++ b/packages/archive/src/lib/activity.ts @@ -4,7 +4,8 @@ import type { BaseLogger } from "pino"; import type { PrismaClient, PrismaPromise } from "@prisma/client"; import { getResponse } from "./parser"; import type { UserSummary } from "./user"; -import { upsertUserSnapshotHook } from "./user"; +import { upsertUserSnapshot } from "./user"; +import lgUrl from "../utils/url"; export interface Activity { content: string; @@ -30,14 +31,14 @@ 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 => response.json()); // eslint-disable-next-line no-restricted-syntax for (const { user } of res.feeds.result) { // eslint-disable-next-line no-await-in-loop - await upsertUserSnapshotHook(prisma, user); + await upsertUserSnapshot(prisma, user); } res.feeds.result.forEach((activity) => { diff --git a/packages/archive/src/lib/judgement.ts b/packages/archive/src/lib/judgement.ts index eb06513..f53fa66 100644 --- a/packages/archive/src/lib/judgement.ts +++ b/packages/archive/src/lib/judgement.ts @@ -1,7 +1,8 @@ import type { PrismaClient, PrismaPromise } from "@prisma/client"; import type { BaseLogger } from "pino"; import { getResponse } from "./parser"; -import { UserSummary, upsertUserSnapshotHook } from "./user"; +import { UserSummary, upsertUserSnapshot } from "./user"; +import lgUrl from "../utils/url"; interface JudgementBody { user: UserSummary; @@ -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 => response.json()); @@ -43,7 +44,7 @@ export default async function saveJudgements( // eslint-disable-next-line no-restricted-syntax for (const judgement of judgements) { // eslint-disable-next-line no-await-in-loop - await upsertUserSnapshotHook(prisma, judgement.user); + await upsertUserSnapshot(prisma, judgement.user); if (new Date(judgement.time * 1000) <= latestJudgement.time) break; operations.push( prisma.judgement.upsert({ diff --git a/packages/archive/src/lib/list.ts b/packages/archive/src/lib/list.ts index 9c35ad2..78b5122 100644 --- a/packages/archive/src/lib/list.ts +++ b/packages/archive/src/lib/list.ts @@ -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; @@ -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 { diff --git a/packages/archive/src/lib/paste.ts b/packages/archive/src/lib/paste.ts index 4cde22b..e3c04dc 100644 --- a/packages/archive/src/lib/paste.ts +++ b/packages/archive/src/lib/paste.ts @@ -2,7 +2,8 @@ import type { BaseLogger } from "pino"; import type { PrismaClient } from "@prisma/client"; import { getResponse } from "./parser"; import { type UserSummary } from "./user"; -import { upsertUserSnapshotHook } from "./user"; +import { upsertUserSnapshot } from "./user"; +import lgUrl from "../utils/url"; interface Paste { data: string; @@ -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 } @@ -49,7 +50,7 @@ export default async function savePaste( } if (json.code !== 200) throw Error(json.currentData.errorMessage); const { paste } = json.currentData; - await upsertUserSnapshotHook(prisma, paste.user); + await upsertUserSnapshot(prisma, paste.user); await prisma.$transaction([ prisma.paste.upsert({ where: { id: paste.id }, diff --git a/packages/archive/src/lib/post.ts b/packages/archive/src/lib/post.ts index 3cddda8..969f047 100644 --- a/packages/archive/src/lib/post.ts +++ b/packages/archive/src/lib/post.ts @@ -4,7 +4,8 @@ import type { BroadcastOperator } from "socket.io"; import type { PostSnapshot, PrismaClient } from "@prisma/client"; import { getResponse } from "./parser"; import type { ServerToClientEvents } from "../plugins/socket.io"; -import { UserSummary, upsertUserSnapshotHook } from "./user"; +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 = {}; @@ -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 => response.json()); @@ -73,7 +74,7 @@ export async function savePost( // eslint-disable-next-line no-restricted-syntax for (const { author } of replies) { // eslint-disable-next-line no-await-in-loop - await upsertUserSnapshotHook(prisma, author); + await upsertUserSnapshot(prisma, author); } allReplies = [...allReplies, ...replies]; }; @@ -129,7 +130,7 @@ export async function savePost( const { post, replies, forum } = (await fetchPage(1)).currentData; const postTime = new Date(post.time * 1000); - await upsertUserSnapshotHook(prisma, post.author); + await upsertUserSnapshot(prisma, post.author); await prisma.$transaction( async (tx) => { diff --git a/packages/archive/src/lib/user.ts b/packages/archive/src/lib/user.ts index 147a798..ce6d5fe 100644 --- a/packages/archive/src/lib/user.ts +++ b/packages/archive/src/lib/user.ts @@ -11,7 +11,7 @@ export interface UserSummary { isRoot?: true; } -export const upsertUserSnapshotHook = async ( +export const upsertUserSnapshot = async ( prisma: PrismaClient, user: UserSummary, ) => { diff --git a/packages/archive/src/server.ts b/packages/archive/src/server.ts index 98e7061..e937c8c 100644 --- a/packages/archive/src/server.ts +++ b/packages/archive/src/server.ts @@ -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( diff --git a/packages/archive/src/utils/url.ts b/packages/archive/src/utils/url.ts new file mode 100644 index 0000000..485406b --- /dev/null +++ b/packages/archive/src/utils/url.ts @@ -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}`; +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 81ce3c3..ccb6815 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -54,6 +54,9 @@ importers: '@socket.io/admin-ui': specifier: ^0.5.1 version: 0.5.1(socket.io@4.7.5(bufferutil@4.0.8)(utf-8-validate@5.0.10)) + dotenv: + specifier: ^16.4.5 + version: 16.4.5 fastify: specifier: ^4.28.0 version: 4.28.0