From 5bbc3637bec1c60cabfc1d098301b814b33c55c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AD=B1=E5=82=91?= <840465812@qq.com> Date: Mon, 2 Aug 2021 13:25:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20Glot=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BB=A3=E7=A0=81=E6=A8=A1=E6=9D=BF=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/GlotAPI.kt | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/GlotAPI.kt b/src/main/kotlin/GlotAPI.kt index d711424..fa4d035 100644 --- a/src/main/kotlin/GlotAPI.kt +++ b/src/main/kotlin/GlotAPI.kt @@ -10,7 +10,7 @@ import java.security.InvalidKeyException * 通过 [listLanguages] 获取支持在线运行的编程语言列表 * ~~通过 [getVersion] 获取对应语言的最新版本请求地址~~ * 通过 [getSupport] 判断指定编程语言是否支持 - * 通过 [getFilename] 来获取指定编程语言的文件名(runCode需要) + * 通过 [getTemplateFile] 来获取指定编程语言的模板文件(runCode需要) * 以上接口均有缓存,仅首次获取不同数据时会发起请求。因此,首次运行可能较慢。 * 通过 [runCode] 运行代码 * 若觉得原版 [runCode] 使用复杂,还可以使用另一个更简单的重载 [runCode] @@ -22,7 +22,7 @@ object GlotAPI { private const val URL_NEW = "https://glot.io/new/" private const val URL_API = URL + "api/" private const val URL_LIST_LANGUAGES = URL_API + "run" - // 运行代码需要api token,这是的我帐号申请的,可以在[https://glot.io/auth/page/simple/register]注册帐号 + // 运行代码需要api token,这是的我账号申请的,可以在[https://glot.io/auth/page/simple/register]注册帐号 private const val API_TOKEN = "074ef4a7-7a94-47f2-9891-85511ef1fb52" data class Language(val name: String, val url: String) @@ -34,7 +34,7 @@ object GlotAPI { data class RunResult(val stdout: String, val stderr: String, val error: String) private var languages: List? = null - private val filenames: MutableMap = mutableMapOf() + private val templateFiles: MutableMap = mutableMapOf() // val fileExtensions: Map = mapOf("assembly" to "asm", "ats" to "dats", "bash" to "sh", "c" to "c", "clojure" to "clj", "cobol" to "cob", "coffeescript" to "coffee", "cpp" to "cpp", "crystal" to "cr", "csharp" to "cs", "d" to "d", "elixir" to "ex", "elm" to "elm", "erlang" to "erl", "fsharp" to "fs", "go" to "go", "groovy" to "groovy", "haskell" to "hs", "idris" to "idr", "java" to "java", "javascript" to "js", "julia" to "jl", "kotlin" to "kt", "lua" to "lua", "mercury" to "m", "nim" to "nim", "nix" to "nix", "ocaml" to "ml", "perl" to "pl", "php" to "php", "python" to "py", "raku" to "raku", "ruby" to "rb", "rust" to "rs", "scala" to "scala", "swift" to "swift", "typescript" to "ts", "plaintext" to "txt", ) /** @@ -77,20 +77,21 @@ object GlotAPI { listLanguages().find { it.name.equals(language, true) } ?: throw InvalidKeyException("不支持的语言") /** - * 获取指定编程语言文件名(缓存) - * @exception Exception 若不支持或无法获取,将抛出异常 - * @return 建议文件名(通常是main.c之类的,java比较特殊,是Main.java,所以需要请求,避免硬编码) + * 获取指定编程语言的模板文件(缓存) */ - fun getFilename(language: String): String { + fun getTemplateFile(language: String): CodeFile { val lang = getSupport(language) - if (filenames.containsKey(lang.name)) - return filenames[lang.name]!! + if (templateFiles.containsKey(lang.name)) + return templateFiles[lang.name]!! val document = HttpUtil.getDocument(URL_NEW + lang.name) val filename = HttpUtil.documentSelect(document, ".filename").firstOrNull()?.text() ?: throw Exception("无法获取文件名") - filenames[lang.name] = filename - return filename + val fileContent = HttpUtil.documentSelect(document, "#editor-1").text() ?: throw Exception("无法获取模板文件内容") + val templateFile = CodeFile(filename, fileContent) + templateFiles[lang.name] = templateFile + return templateFile } + /** * # 运行代码 * @@ -179,5 +180,5 @@ object GlotAPI { * 导致程序无法在限定时间内返回,将会报告超时异常 */ fun runCode(language: String, code: String, stdin: String? = null): RunResult = - runCode(getSupport(language), RunCodeRequest(stdin, null, listOf(CodeFile(getFilename(language), code)))) + runCode(getSupport(language), RunCodeRequest(stdin, null, listOf(CodeFile(getTemplateFile(language).name, code)))) } \ No newline at end of file