Image Provider

This commit is contained in:
jiahua.liu 2020-02-04 15:37:09 +08:00
parent 44585d2df6
commit 5daf97015a
3 changed files with 61 additions and 20 deletions

View File

@ -8,6 +8,7 @@ import net.mamoe.mirai.qqandroid.QQAndroidBot
import net.mamoe.mirai.qqandroid.io.serialization.writeProtoBuf
import net.mamoe.mirai.qqandroid.network.QQAndroidClient
import net.mamoe.mirai.qqandroid.network.protocol.data.proto.OidbSso
import net.mamoe.mirai.qqandroid.network.protocol.packet.EMPTY_BYTE_ARRAY
import net.mamoe.mirai.qqandroid.network.protocol.packet.OutgoingPacket
import net.mamoe.mirai.qqandroid.network.protocol.packet.OutgoingPacketFactory
import net.mamoe.mirai.qqandroid.network.protocol.packet.buildOutgoingUniPacket
@ -47,14 +48,34 @@ internal object TroopManagement {
)
}
}
object Response : Packet
}
internal object MuteAll : OutgoingPacketFactory<LoginPacket.LoginPacketResponse>("OidbSvc.0x89a_0") {
override suspend fun ByteReadPacket.decode(bot: QQAndroidBot): LoginPacket.LoginPacketResponse {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
internal object MuteAll : OutgoingPacketFactory<MuteAll.Response>("OidbSvc.0x89a_0") {
override suspend fun ByteReadPacket.decode(bot: QQAndroidBot): Response {
return Response
}
operator fun invoke(
client: QQAndroidClient,
groupCode: Long
): OutgoingPacket {
return buildOutgoingUniPacket(client) {
writeProtoBuf(
OidbSso.OIDBSSOPkg.serializer(),
OidbSso.OIDBSSOPkg(
command = 2202,
serviceType = 0,
result = 0,
bodybuffer = EMPTY_BYTE_ARRAY//TODO
)
)
}
}
object Response : Packet
}
internal object EditNametag : OutgoingPacketFactory<LoginPacket.LoginPacketResponse>("OidbSvc.0x8fc_2") {

View File

@ -2,6 +2,7 @@ package demo.gentleman
import com.alibaba.fastjson.JSON
import kotlinx.coroutines.*
import kotlinx.serialization.json.JsonObject
import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.message.data.Image
import net.mamoe.mirai.message.uploadAsImage
@ -12,31 +13,41 @@ class GentleImage {
// `Deferred<Image?>` causes a runtime ClassCastException
val image: Deferred<Image> by lazy {
GlobalScope.async {
//delay((Math.random() * 5000L).toLong())
class Result {
var id: String = ""
}
val image: Deferred<Image> by lazy { getImage(0) }
val seImage: Deferred<Image> by lazy { getImage(1) }
fun getImage(r18: Int): Deferred<Image> {
GlobalScope.async {
withTimeoutOrNull(5 * 1000) {
withContext(Dispatchers.IO) {
val result = JSON.parseObject(
Jsoup.connect("http://dev.itxtech.org:10322/v2/randomImg.uue").ignoreContentType(true).timeout(10_0000).get().body().text(),
Result::class.java
)
Jsoup.connect("https://api.lolicon.app/setu/?r18=$r18").ignoreContentType(true).timeout(10_0000).get().body().text(),
)
Jsoup.connect("http://dev.itxtech.org:10322/img.uue?size=large&id=${result.id}")
.userAgent(UserAgent.randomUserAgent)
.timeout(10_0000)
var url = "";
var pid = "";
with(result.getJSONArray("data").getJSONObject(0)) {
url = this.getString("url")
pid = this.getString("pid")
}
val image = Jsoup
.connect(url)
.followRedirects(true)
.timeout(180_000)
.ignoreContentType(true)
.maxBodySize(Int.MAX_VALUE)
.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(10000000)
.execute()
.bodyStream()
if (image.statusCode() != 200) error("Failed to download image")
}
}?.uploadAsImage(contact) ?: error("Unable to download image")
image.bodyStream().uploadAsImage(contact) ?: error("Unable to download image")
}
}
}
}

View File

@ -151,7 +151,7 @@ suspend fun main() {
file.sendAsImageTo(subject)
}
startsWith("随机图片", removePrefix = true) {
startsWith("色图", removePrefix = true) {
repeat(it.toIntOrNull() ?: 1) {
GlobalScope.launch {
delay(Random.Default.nextLong(100, 1000))
@ -160,6 +160,15 @@ suspend fun main() {
}
}
startsWith("不够色", removePrefix = true) {
repeat(it.toIntOrNull() ?: 1) {
GlobalScope.launch {
delay(Random.Default.nextLong(100, 1000))
Gentlemen.provide(subject).receive().seImage.await().send()
}
}
}
startsWith("添加好友", removePrefix = true) {
reply(bot.addFriend(it.toLong()).toString())
}