mirror of
https://github.com/jie65535/mirai-console-jcc-plugin.git
synced 2025-08-04 19:29:13 +08:00
修改 Glot接口增加代码模板文件的获取
This commit is contained in:
parent
f167c99525
commit
5bbc3637be
@ -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<Language>? = null
|
||||
private val filenames: MutableMap<String, String> = mutableMapOf()
|
||||
private val templateFiles: MutableMap<String, CodeFile> = mutableMapOf()
|
||||
// val fileExtensions: Map<String, String> = 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))))
|
||||
}
|
Loading…
Reference in New Issue
Block a user