UNPKG

un-oj

Version:

Unified Online Judge information collector.

49 lines (47 loc) 2.01 kB
import { NotFoundError, Platform, parseMemory, parseTime } from "../platform-BncnsqE0.js"; import { load } from "cheerio"; //#region src/platforms/codeforces.ts const DEFAULT_BASE_URL = "https://codeforces.com"; /** Codeforces platform. */ var Codeforces = class extends Platform { constructor(options) { super(options, DEFAULT_BASE_URL); } /** * Fetches a problem from Codeforces, extracting information from HTML. * * Automatically switches to gym mode if {@link id} > `100000`. */ async getProblem(id) { const contest = Number.parseInt(id); const num = id.replace(String(contest), ""); const path = contest > 1e5 ? `/gym/${contest}/problem/${num}` : `/problemset/problem/${contest}/${num}`; const $ = load(await this.ofetch(path, { responseType: "text" })); const body = $(".problem-statement"), sidebar = $("#sidebar"); const description = body.find(".header ~ *").not(".sample-test").html(); if (!description) throw new NotFoundError("problem"); const examples = []; for (const el of body.find(".sample-test").children()) { const raw = $(el.lastChild).html() ?? ""; if (el.attribs.class === "output") examples.at(-1).output = raw.replaceAll("<br>", "\n"); else examples.push({ input: raw.replaceAll("<br>", "\n"), output: "" }); } return { id, type: description.includes("This is an interactive problem.") ? "interactive" : "traditional", title: body.find(".header .title").text().replace(`${num}. `, ""), link: new URL(path, this.baseURL).href, description, samples: examples, timeLimit: parseTime(body.find(".time-limit").contents().last().text()), memoryLimit: parseMemory(body.find(".memory-limit").contents().last().text()), difficulty: Number(sidebar.find(".tag-box[title=\"Difficulty\"]").text().trim().slice(1)) || void 0, tags: sidebar.find(".tag-box").not("[title=\"Difficulty\"]").contents().map((_, el) => $(el).text().trim()).get() }; } }; //#endregion export { DEFAULT_BASE_URL, Codeforces as default };