From 9bb94f101dbdfc37c12f0b5cf98f59232b63d8e2 Mon Sep 17 00:00:00 2001 From: undefined Date: Sun, 3 May 2020 18:24:11 +0800 Subject: [PATCH] problem-import-syzoj --- module/problem-import-syzoj/handler.js | 52 ++++++++++++++++++++++++++ module/problem-import-syzoj/hydro.json | 4 ++ 2 files changed, 56 insertions(+) create mode 100644 module/problem-import-syzoj/handler.js create mode 100644 module/problem-import-syzoj/hydro.json diff --git a/module/problem-import-syzoj/handler.js b/module/problem-import-syzoj/handler.js new file mode 100644 index 00000000..46e1c65c --- /dev/null +++ b/module/problem-import-syzoj/handler.js @@ -0,0 +1,52 @@ +const assert = require('assert'); + +const download = global.Hydro['lib.download']; +const axios = global.Hydro['lib.axios']; +const { ValidationError, RemoteOnlineJudgeError } = global.Hydro.error; + +async function syzoj(url) { + const RE_SYZOJ = /https?:\/\/([a-zA-Z0-9.]+)\/problem\/([0-9]+)\/?/i; + assert(url.match(RE_SYZOJ), new ValidationError('url')); + if (!url.endsWith('/')) url += '/'; + const [, host, pid] = RE_SYZOJ.exec(url); + const res = await axios.get(`${url}export`); + assert(res.status === 200, new RemoteOnlineJudgeError('Cannot connect to target server')); + assert(res.data.success, new RemoteOnlineJudgeError((res.data.error || {}).message)); + const p = res.data.obj; + const content = [ + this.translate('problem.import.problem_description'), + p.description, + this.translate('problem.import.input_format'), + p.input_format, + this.translate('problem.import.output_format'), + p.output_format, + this.translate('problem.import.hint'), + p.hint, + this.translate('problem.import.limit_and_hint'), + p.limit_and_hint, + ]; + if (p.have_additional_file) { + content.push( + this.translate('problem.import.additional_file'), + `${url}download/additional_file`, + ); + } + const pdoc = { + title: p.title, + content: content.join(' \n'), + owner: this.user._id, + from: url, + pid: `${host}_${pid}`, + config: { + time: p.time_limit, + memory: p.memory_limit * 1024, + filename: p.file_io_input_name, + type: p.type, + tags: p.tags, + }, + }; + const r = await download(`${url}testdata/download`); + return [pdoc, r]; +} + +global.Hydro.handler.import.ProblemImportHandler.from_syzoj = syzoj; diff --git a/module/problem-import-syzoj/hydro.json b/module/problem-import-syzoj/hydro.json new file mode 100644 index 00000000..4e49ed6b --- /dev/null +++ b/module/problem-import-syzoj/hydro.json @@ -0,0 +1,4 @@ +{ + "name": "problem-import-syzoj", + "version": "1.0.0" +} \ No newline at end of file