Skip to content

Commit

Permalink
Merge pull request #75 from EvolvedGhost/master
Browse files Browse the repository at this point in the history
换用原生方式获取图片链接
  • Loading branch information
Nekoer authored May 10, 2024
2 parents adb6555 + 49609c3 commit 7082947
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/main/kotlin/com/hcyacg/search/Saucenao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import com.hcyacg.utils.RequestUtil
import com.hcyacg.entity.SaucenaoItem
import com.hcyacg.initial.Command
import com.hcyacg.initial.Config
import com.hcyacg.utils.DataUtil.Companion.getImageLinkFromImage
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.*
import net.mamoe.mirai.event.events.GroupMessageEvent
import net.mamoe.mirai.message.data.At
import net.mamoe.mirai.message.data.Image
import net.mamoe.mirai.message.data.Image.Key.queryUrl
import net.mamoe.mirai.message.data.Message
import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource
import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsImage
Expand Down Expand Up @@ -66,23 +66,23 @@ object Saucenao {
val rotate90 = rotate(withContext(Dispatchers.IO) {
ImageIO.read(URL(picUri))
}, 90).toByteArray().toExternalResource()
val code90 = getImageLinkFromImage(rotate90.uploadAsImage(event.group))
val code90 = rotate90.uploadAsImage(event.group).queryUrl()
withContext(Dispatchers.IO) {
rotate90.close()
}

val rotate180 = rotate(withContext(Dispatchers.IO) {
ImageIO.read(URL(picUri))
}, 180).toByteArray().toExternalResource()
val code180 = getImageLinkFromImage(rotate180.uploadAsImage(event.group))
val code180 = rotate180.uploadAsImage(event.group).queryUrl()
withContext(Dispatchers.IO) {
rotate180.close()
}

val rotate270 = rotate(withContext(Dispatchers.IO) {
ImageIO.read(URL(picUri))
}, 270).toByteArray().toExternalResource()
val code270 = getImageLinkFromImage(rotate270.uploadAsImage(event.group))
val code270 = rotate270.uploadAsImage(event.group).queryUrl()
withContext(Dispatchers.IO) {
rotate270.close()
}
Expand Down
15 changes: 3 additions & 12 deletions src/main/kotlin/com/hcyacg/utils/DataUtil.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.hcyacg.utils;

import net.mamoe.mirai.message.data.Image
import net.mamoe.mirai.message.data.Image.Key.queryUrl
import net.mamoe.mirai.message.data.MessageChain
import java.io.UnsupportedEncodingException
import java.net.URLEncoder
Expand All @@ -13,25 +14,15 @@ import java.text.NumberFormat
*/
class DataUtil {
companion object {
fun getImageLink(chain: MessageChain): String? {
suspend fun getImageLink(chain: MessageChain): String? {
chain.forEach {
if (it is Image) {
return getImageLinkFromImage(it)
return it.queryUrl()
}
}
return null
}

fun getImageLinkFromImage(image: Image): String {
val pic = image.toString()
return if (pic.contains("overflow:image")) {
image.imageId
} else {
val picUri = image.imageId.replace("-", "")
"https://gchat.qpic.cn/gchatpic_new/0/0-0-${picUri}/0?"
}
}

fun urlEncode(url: String): String {
return URLEncoder.encode(url, "UTF-8")
}
Expand Down

0 comments on commit 7082947

Please sign in to comment.