1
0
mirror of https://github.com/mamoe/mirai.git synced 2025-04-25 13:03:35 +08:00

Fixed image

This commit is contained in:
Him188 2019-10-27 21:49:50 +08:00
parent c973fbd0d1
commit 3f56faae75
4 changed files with 38 additions and 62 deletions
mirai-core/src
commonMain/kotlin/net.mamoe.mirai/utils
jvmMain/kotlin/net/mamoe/mirai/utils
mirai-demos/mirai-demo-gentleman/src/main/kotlin/demo/gentleman

View File

@ -66,7 +66,7 @@ suspend fun httpPostFriendImage(
imageInput = imageInput,
inputSize = inputSize,
uKeyHex = uKeyHex
) as HttpStatusCode).value.also { println(it) } == 200
) as HttpStatusCode).value == 200
/**
* 上传群图片
@ -85,7 +85,7 @@ suspend fun httpPostGroupImage(
imageInput = imageInput,
inputSize = inputSize,
uKeyHex = uKeyHex
) as HttpStatusCode).value.also { println(it) } == 200
) as HttpStatusCode).value == 200
@Suppress("SpellCheckingInspection")
private suspend inline fun <reified T> HttpClient.postImage(
@ -95,28 +95,31 @@ private suspend inline fun <reified T> HttpClient.postImage(
imageInput: Input,
inputSize: Long,
uKeyHex: String
): T = post {
url {
protocol = URLProtocol.HTTP
host = "htdata2.qq.com"
path("cgi-bin/httpconn")
): T = try {
post {
url {
protocol = URLProtocol.HTTP
host = "htdata2.qq.com"
path("cgi-bin/httpconn")
parameters["htcmd"] = htcmd
parameters["uin"] = uin.toLong().toString()
parameters["htcmd"] = htcmd
parameters["uin"] = uin.toLong().toString()
if (groupcode != null) parameters["groupcode"] = groupcode.value.toLong().toString()
if (groupcode != null) parameters["groupcode"] = groupcode.value.toLong().toString()
parameters["term"] = "pc"
parameters["ver"] = "5603"
parameters["filesize"] = inputSize.toString()
parameters["range"] = 0.toString()
parameters["ukey"] = uKeyHex
parameters["term"] = "pc"
parameters["ver"] = "5603"
parameters["filesize"] = inputSize.toString()
parameters["range"] = 0.toString()
parameters["ukey"] = uKeyHex
userAgent("QQClient")
userAgent("QQClient")
}
configureBody(inputSize, imageInput)
}
println(url.buildString())
configureBody(inputSize, imageInput)
} finally {
imageInput.close()
}
internal expect fun HttpRequestBuilder.configureBody(inputSize: Long, input: Input)

View File

@ -59,39 +59,6 @@ actual fun solveIpAddress(hostname: String): String = InetAddress.getByName(host
actual fun localIpAddress(): String = InetAddress.getLocalHost().hostAddress
/*
actual suspend fun httpPostFriendImageOld(
uKeyHex: String,
botNumber: UInt,
imageData: ByteReadPacket
): Boolean = Jsoup.connect(
"http://htdata2.qq.com/cgi-bin/httpconn" +
"?htcmd=0x6ff0070" +
"&ver=5603" +
"&ukey=$uKeyHex" +
"&filesize=${imageData.remaining}" +
"&range=0" +
"&uin=$botNumber"
)
.postImage(imageData)
private suspend fun Connection.postImage(image: ByteReadPacket): Boolean = this
.userAgent("QQClient")
.header("Content-Length", image.remaining.toString())
.requestBody(String(image.readBytes(), Charsets.ISO_8859_1))
.method(Connection.Method.POST)
.postDataCharset("ISO_8859_1")
.header("Content-type", "image/gif")
.ignoreContentType(true)
.suspendExecute()
.statusCode() == 200
private suspend fun Connection.suspendExecute(): Connection.Response = withContext(Dispatchers.IO) {
execute()
}
*/
internal actual val httpClient: HttpClient = HttpClient(CIO)
internal actual fun HttpRequestBuilder.configureBody(

View File

@ -4,7 +4,6 @@ import kotlinx.coroutines.*
import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.message.Image
import net.mamoe.mirai.message.upload
import net.mamoe.mirai.utils.MiraiLogger
import org.jsoup.Jsoup
class GentleImage {
@ -42,7 +41,7 @@ class GentleImage {
// CompletableDeferred(suspend {
delay((Math.random() * 5000L).toLong())
MiraiLogger.logPurple("Downloading image: $name")
// MiraiLogger.logPurple("Downloading image: $name")
withContext(Dispatchers.IO) {
Jsoup.connect(sample_url)
.userAgent(UserAgent.randomUserAgent)
@ -52,7 +51,7 @@ class GentleImage {
.execute()
.bodyStream()
}.upload(contact).also {
MiraiLogger.logPurple("Downloaded image: $name")
//MiraiLogger.logPurple("Downloaded image: $name")
}
// }())
// }

View File

@ -1,17 +1,16 @@
package demo.gentleman
import com.alibaba.fastjson.JSONArray
import kotlinx.coroutines.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
import net.mamoe.mirai.contact.Contact
import org.jsoup.Connection
import org.jsoup.Jsoup
/**
* 最少缓存的图片数量
*/
private const val IMAGE_BUFFER_CAPACITY: Int = 5
private const val IMAGE_BUFFER_CAPACITY: Int = 50
/**
* 每次补充的数量
@ -30,12 +29,20 @@ class Gentleman(private val contact: Contact) : Channel<GentleImage> by Channel(
GlobalScope.launch {
while (!isClosedForSend) {
send(GentleImage().apply {
sample_url = "http://dev.itxtech.org:10322/randomImg.uue?tdsourcetag=s_pctim_aiomsg"
contact = this@Gentleman.contact
image.await()
})
/*
val response = withContext(Dispatchers.IO) {
tryNTimes(2) {
Jsoup.connect("https://yande.re/post.json?")
.userAgent(UserAgent.randomUserAgent)
.data("limit", "20")
.data("page", (Math.random() * 4000).toString())
.data("page", (Random.Default.nextInt(12000)).toString())
.ignoreContentType(true)
.timeout(20_000)
.method(Connection.Method.GET)
@ -57,7 +64,7 @@ class Gentleman(private val contact: Contact) : Channel<GentleImage> by Channel(
it.image//start downloading
send(it)
}
}*/
}
}
}