un-oj
Version:
Unified Online Judge information collector.
60 lines (58 loc) • 1.81 kB
JavaScript
import { NotFoundError, Platform, UnOJError, UnexpectedResponseError } from "../platform-BncnsqE0.js";
//#region src/platforms/lyrio.ts
const DEFAULT_BASE_URL = "https://api.loj.ac";
/**
* Lyrio platform.
*
* I18n is supported.
*/
var Lyrio = class extends Platform {
constructor(options) {
super(options, DEFAULT_BASE_URL);
}
/** Fetches a problem from LibreOJ using API. */
async getProblem(id) {
const displayId = Number.parseInt(id);
if (Number.isNaN(displayId)) throw new NotFoundError("problem");
let data;
try {
data = await this.ofetch("/api/problem/getProblem", {
method: "POST",
body: {
displayId,
localizedContentsOfLocale: this.locale || "zh_CN",
tagsOfLocale: this.locale || "zh_CN",
samples: true,
judgeInfo: true,
judgeInfoToBePreprocessed: true,
statistics: false,
discussionCount: false,
permissionOfCurrentUser: false,
lastSubmissionAndLastAcceptedSubmission: false
},
responseType: "json"
});
} catch (e) {
throw new UnOJError(`Failed to fetch problem ${id}`, { cause: e });
}
if (data.error === "NO_SUCH_PROBLEM") throw new NotFoundError("problem");
if (!data.meta || !data.localizedContentsOfLocale) throw new UnexpectedResponseError(data);
return {
id,
type: data.meta.type,
title: data.localizedContentsOfLocale.title,
link: `https://loj.ac/p/${displayId}`,
description: JSON.stringify(data.localizedContentsOfLocale.contentSections),
samples: data.samples.map((sample) => ({
input: sample.inputData,
output: sample.outputData
})),
timeLimit: data.judgeInfo.timeLimit,
memoryLimit: data.judgeInfo.memoryLimit * 1024 * 1024,
tags: data.tagsOfLocale || [],
difficulty: void 0
};
}
};
//#endregion
export { DEFAULT_BASE_URL, Lyrio as default };