UNPKG

un-oj

Version:

Unified Online Judge information collector.

98 lines (96 loc) 3.03 kB
import { NotFoundError, Platform, UnOJError, addHeaders } from "../platform-BncnsqE0.js"; import { FetchError } from "ofetch"; //#region src/platforms/luogu.ts /** Maps {@link Difficulty} to a string label in Chinese. */ const DifficultyLabel = { 0: "暂无评定", 1: "入门", 2: "普及-", 3: "普及/提高-", 4: "普及+/提高", 5: "提高+/省选-", 6: "省选/提高-", 7: "NOI/NOI+/CTSC" }; const ContestFormatLabel = { 1: "IOI", 2: "OI", 3: "ICPC", 4: "乐多", 5: "Codeforces" }; const DEFAULT_BASE_URL = "https://www.luogu.com.cn"; /** * Luogu platform. * * I18n is supported. */ var Luogu = class extends Platform { constructor(options) { super({ ...options, ofetchDefaults: { ...options?.ofetchDefaults, headers: addHeaders(options?.ofetchDefaults?.headers, [["x-lentille-request", "content-only"], ["x-luogu-type", "content-only"]]) } }, DEFAULT_BASE_URL); } /** Fetches a problem from Luogu using internal API. */ async getProblem(id) { const path = `/problem/${id}`; let data; try { ({data} = await this.ofetch(path, { responseType: "json" })); } catch (e) { if (e instanceof FetchError && e.statusCode === 404) throw new NotFoundError("problem"); throw new UnOJError(`Failed to fetch problem ${id}`, { cause: e }); } const content = this.locale ? data.translations[this.locale] || data.problem.content : data.problem.content; const tags = data.problem.tags; return { id, type: tags.includes(103) ? "interactive" : tags.includes(104) ? "submission" : tags.includes(351) ? "communication" : "traditional", title: content.name, link: new URL(path, this.baseURL).href, description: { background: content.background || "", details: content.description || "", input: content.formatI || "", output: content.formatO || "", hint: content.hint || "" }, samples: data.problem.samples.map(([a, b]) => ({ input: a, output: b })), timeLimit: data.problem.limits.time, memoryLimit: data.problem.limits.memory.map((v) => v * 1024), difficulty: data.problem.difficulty, tags: tags.map((t) => ({ id: t })) }; } async getContest(id) { const path = `/contest/${id}`; let contest, contestProblems; try { const data = await this.ofetch(path, { responseType: "json" }); if (data.code !== 200) throw new NotFoundError("contest"); ({contest, contestProblems} = data.currentData); } catch (e) { if (e instanceof UnOJError) throw e; if (e instanceof FetchError && e.statusCode === 404) throw new NotFoundError("contest"); throw new UnOJError(`Failed to fetch contest ${id}`, { cause: e }); } return { id, title: contest.name, description: contest.description, startTime: contest.startTime && new Date(contest.startTime * 1e3), endTime: contest.endTime && new Date(contest.endTime * 1e3), problems: contestProblems ?? [], format: contest.ruleType }; } }; //#endregion export { ContestFormatLabel, DEFAULT_BASE_URL, DifficultyLabel, Luogu as default };