demo plugin

This commit is contained in:
jiahua.liu 2020-02-23 18:01:23 +08:00
parent ba4b225371
commit 971d7e182f
5 changed files with 6029 additions and 6140 deletions

View File

@ -1,48 +0,0 @@
/*
* Copyright 2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.imageplugin
import com.alibaba.fastjson.JSON
import kotlinx.coroutines.*
import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.message.data.Image
import net.mamoe.mirai.message.uploadAsImage
import org.jsoup.Jsoup
class ImageProvider {
lateinit var contact: Contact
// `Deferred<Image?>` causes a runtime ClassCastException
val image: Deferred<Image> by lazy {
GlobalScope.async {
withTimeoutOrNull(5 * 1000) {
withContext(Dispatchers.IO) {
val result = JSON.parseArray(
Jsoup.connect("https://yande.re/post.json?limit=1&page=${(Math.random() * 10000).toInt()}").ignoreContentType(
true
).timeout(
10_0000
).get().body().text()
)
Jsoup.connect(result.getJSONObject(0).getString("jpeg_url"))
.userAgent("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; ja-jp) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27")
.timeout(10_0000)
.ignoreContentType(true)
.maxBodySize(Int.MAX_VALUE)
.execute()
.bodyStream()
}
}?.uploadAsImage(contact) ?: error("Unable to download image|连接这个图站需要你的网络在外网")
}
}
}

View File

@ -9,15 +9,27 @@
package net.mamoe.mirai.imageplugin package net.mamoe.mirai.imageplugin
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.*
import kotlinx.coroutines.GlobalScope import net.mamoe.mirai.console.plugins.Config
import net.mamoe.mirai.console.plugins.ConfigSection
import net.mamoe.mirai.event.events.BotOnlineEvent import net.mamoe.mirai.event.events.BotOnlineEvent
import net.mamoe.mirai.event.subscribeAlways import net.mamoe.mirai.event.subscribeAlways
import net.mamoe.mirai.event.subscribeMessages import net.mamoe.mirai.event.subscribeMessages
import net.mamoe.mirai.console.plugins.PluginBase import net.mamoe.mirai.console.plugins.PluginBase
import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.message.data.Image
import net.mamoe.mirai.message.uploadAsImage
import net.mamoe.mirai.utils.MiraiExperimentalAPI import net.mamoe.mirai.utils.MiraiExperimentalAPI
import org.jsoup.Jsoup
import java.io.File
import kotlin.random.Random
class ImageSenderMain : PluginBase() { class ImageSenderMain : PluginBase() {
lateinit var images: Config
lateinit var normal: List<ConfigSection>
lateinit var r18: List<ConfigSection>
@ExperimentalCoroutinesApi @ExperimentalCoroutinesApi
@MiraiExperimentalAPI @MiraiExperimentalAPI
override fun onEnable() { override fun onEnable() {
@ -25,24 +37,61 @@ class ImageSenderMain : PluginBase() {
GlobalScope.subscribeAlways<BotOnlineEvent> { GlobalScope.subscribeAlways<BotOnlineEvent> {
logger.info("${this.bot.uin} login succeed, it will be controlled by Image Sender Plugin") logger.info("${this.bot.uin} login succeed, it will be controlled by Image Sender Plugin")
this.bot.subscribeMessages { this.bot.subscribeMessages {
(contains("色图")) {
case("at me") { try {
reply(sender.at() + " ? ") with(normal.random()) {
getImage(
subject, this.getString("url"), this.getString("pid")
).plus(this.getString("tags")).send()
}
} catch (e: Exception) {
reply(e.message ?: "unknown error")
}
} }
(contains("image") or contains("")) { (contains("不够色")) {
"图片发送中".reply() try {
ImageProvider().apply { with(r18.random()) {
this.contact = sender getImage(
}.image.await().reply() subject, this.getString("url"), this.getString("pid")
).plus(this.getString("tags")).send()
}
} catch (e: Exception) {
reply(e.message ?: "unknown error")
}
} }
} }
} }
} }
suspend fun getImage(contact: Contact, url: String, pid: String): Image {
return withTimeoutOrNull(20 * 1000) {
withContext(Dispatchers.IO) {
Jsoup
.connect(url)
.followRedirects(true)
.timeout(180_000)
.ignoreContentType(true)
.userAgent("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; ja-jp) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27")
.referrer("https://www.pixiv.net/member_illust.php?mode=medium&illust_id=$pid")
.ignoreHttpErrors(true)
.maxBodySize(100000000)
.execute().also { check(it.statusCode() == 200) { "Failed to download image" } }
}
}?.bodyStream()?.uploadAsImage(contact) ?: error("Unable to download image")
}
override fun onLoad() { override fun onLoad() {
logger.info("loading...") logger.info("loading local image data")
try {
images = Config.load(this.javaClass.classLoader.getResource("data.yml")!!.path!!)
} catch (e: Exception) {
logger.info("无法加载本地图片")
}
logger.info("本地图片版本" + images.getString("version"))
logger.info("Normal * " + images.getList("normal").size)
logger.info("R18 * " + images.getList("R18").size)
} }
override fun onDisable() { override fun onDisable() {

View File

@ -30,6 +30,7 @@ object Data {
} }
fun main() { fun main() {
val abstract_file = (File(System.getProperty("user.dir") + "/abstractSetu.yml")).loadAsConfig() val abstract_file = (File(System.getProperty("user.dir") + "/abstractSetu.yml")).loadAsConfig()
abstract_file.setIfAbsent("R18", mutableListOf<ConfigSection>()) abstract_file.setIfAbsent("R18", mutableListOf<ConfigSection>())
@ -53,6 +54,7 @@ fun main() {
this["author"] = it["author"]!! this["author"] = it["author"]!!
this["uid"] = it["uid"]!! this["uid"] = it["uid"]!!
this["tags"] = it["tags"]!! this["tags"] = it["tags"]!!
this["url"] = it["url"]!!
} }
) )
} }
@ -73,6 +75,7 @@ fun main() {
this["author"] = it["author"]!! this["author"] = it["author"]!!
this["uid"] = it["uid"]!! this["uid"] = it["uid"]!!
this["tags"] = it["tags"]!! this["tags"] = it["tags"]!!
this["url"] = it["url"]!!
} }
) )
} }
@ -87,6 +90,7 @@ fun main() {
Runtime.getRuntime().addShutdownHook(thread(start = false) { Runtime.getRuntime().addShutdownHook(thread(start = false) {
Data.save() Data.save()
}) })
while (true){ while (true){
try { try {
val val0 = JSONObject.parseObject(Jsoup val val0 = JSONObject.parseObject(Jsoup
@ -116,6 +120,7 @@ fun main() {
configSection["tags"] = content.getJSONArray("tags").map { configSection["tags"] = content.getJSONArray("tags").map {
it.toString() it.toString()
}.joinToString(",") }.joinToString(",")
configSection["url"] = content.getString("url")
if(isR18){ if(isR18){
Data.R18.add(configSection) Data.R18.add(configSection)
print("获取到了一张R18") print("获取到了一张R18")
@ -131,9 +136,9 @@ fun main() {
} }
Data.save() Data.save()
println("SAVED") println("SAVED")
Thread.sleep(1250) Thread.sleep(1000)
} }
*/ */
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
name: ImageSender name: ImageSender
main: net.mamoe.mirai.imageplugin.ImageSenderMain main: net.mamoe.mirai.imageplugin.ImageSenderMain
version: 1.0.0 version: 1.0.0
author: mamoe author: 不想写代码
info: a demo plugin of mirai info: a demo[hso] plugin of mirai