mirai-console-jcc-plugin/src/main/kotlin/UbuntuPastebinHelper.kt
jie65535 f374a446ee 更新到v1.0
弃用pastebin粘贴
增加pastebin运行支持
增加pastebin运行时增加输入
更新依赖
2022-03-30 11:19:59 +08:00

88 lines
3.4 KiB
Kotlin
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import okhttp3.FormBody
import okhttp3.OkHttpClient
import okhttp3.Request
import org.jsoup.Jsoup
import java.io.IOException
/**
* # ubuntu pastebin 帮助类
* [https://paste.ubuntu.com] 是一个用于共享文档的网站
* 由于pastebin本身没有对外提供api所以本类使用解析html的方式实现
* 通过 [getSyntaxList] 获取支持的语法列表(缓存)
* 通过 [get] 获取链接的内容
* 通过 [paste] 来粘贴内容
*
* @author jie65535@github
*/
object UbuntuPastebinHelper {
private const val URL = "https://paste.ubuntu.com"
private var syntaxList: Map<String, String>? = null
/**
* 获取支持的语法列表(缓存)
* @return 返回一个map其中key是给人看的value是作为参数传递的
*/
@Deprecated("paste.ubuntu.com 现在需要登录 首页不再显示符号列表,因此该方法弃用")
fun getSyntaxList(): Map<String, String> {
if (syntaxList != null)
return syntaxList!!
val document = HttpUtil.getDocument(URL)
val element = HttpUtil.documentSelect(document, "select#id_syntax > option")
val map = mutableMapOf<String, String>()
for (opt in element)
map[opt.text()] = opt.`val`()
syntaxList = map
return map
}
fun checkUrl(url: String): Boolean {
return url.startsWith("https://pastebin.ubuntu.com/p/") && url.endsWith('/') && url.length < 45
}
/**
* 获取内容
* @param url pastebin地址https://paste.ubuntu.com/p/nmn8yKMtND/
* @return 返回链接中贴的内容
*/
fun get(url: String): String {
if (!checkUrl(url))
throw Exception("非法的url")
val document = HttpUtil.getDocument(url)
return HttpUtil.documentSelect(document, "#hidden-content").text()
}
/**
* 上传内容
* @param content 上传内容
* @param syntax 语法例如c/cpp 可以通过getSyntaxList得到所有支持的语法传入pair的value 默认值text
* @param poster 主题文本最大长度30字符 默认值:"temp"
* @param expiration 过期时间((empty)/day/week/month/year 默认值:"day"
* @return 返回访问地址https://paste.ubuntu.com/p/nmn8yKMtND/
*/
@Deprecated("paste.ubuntu.com 现在需要登录,因此不能再粘贴")
fun paste(content: String, syntax: String = "text", poster: String = "temp", expiration: String = "day"): String {
if (poster.length > 30)
throw Exception("poster length too long!")
if (content.isEmpty())
throw Exception("content cannot be empty!")
val okHttpClient = OkHttpClient().newBuilder()
.followRedirects(false)
.build()
val requestBody = FormBody.Builder()
.add("poster", poster)
.add("syntax", syntax)
.add("expiration", expiration)
.add("content", content)
.build()
val request = Request.Builder()
.url(URL)
.post(requestBody)
.build()
val response = okHttpClient.newCall(request).execute()
if (response.code == 200)
throw Exception("请求已经成功,但无法执行动作,请检查参数")
return if (response.code == 302)
URL + response.header("Location")
else
throw IOException("请求失败,请检查网络或参数")
}
}