Skip to content

Commit

Permalink
Fix: All Pic Url Error
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekoer committed Apr 17, 2024
1 parent 5e9395d commit d91be3f
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 92 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {
}

group = "com.hcyacg"
version = "1.7.7"
version = "1.7.8"

repositories {
// maven("https://maven.aliyun.com/repository/public")
Expand Down Expand Up @@ -63,6 +63,7 @@ allOpen{
val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions {
jvmTarget = "1.8"

}

val compileTestKotlin: KotlinCompile by tasks
Expand Down
229 changes: 160 additions & 69 deletions src/main/kotlin/com/hcyacg/Nsfw.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
package com.hcyacg


import com.hcyacg.initial.Config
import com.hcyacg.search.Yandex
import com.hcyacg.utils.*
import kotlinx.serialization.json.*
import net.mamoe.mirai.console.MiraiConsole
import net.mamoe.mirai.event.events.GroupMessageEvent
import net.mamoe.mirai.message.data.At
import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.message.data.QuoteReply
import net.mamoe.mirai.utils.MiraiLogger
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import java.io.File
import java.math.RoundingMode
import java.net.InetSocketAddress
import java.net.Proxy
import java.text.DecimalFormat
import java.util.concurrent.TimeUnit


object Nsfw {
private val logger = MiraiLogger.Factory.create(this::class.java)
private val headers = Headers.Builder()

private val tag = mutableMapOf<String,String>()
private var requestBody: RequestBody? = null
private val client = OkHttpClient().newBuilder().connectTimeout(60000, TimeUnit.MILLISECONDS)
.readTimeout(60000, TimeUnit.MILLISECONDS).followRedirects(false)
private val tags = mutableMapOf<String,String>()
private val json = Json
const val url = "https://raw.fastgit.org/Nekoer/mirai-plugins-pixiv/master/src/main/resources/tags.json"
const val url = "https://fastly.jsdelivr.net/gh/Nekoer/mirai-plugins-pixiv@master/src/main/resources/tags.json"
init {
val file = File(MiraiConsole.pluginManager.pluginsConfigFolder.path + File.separator +"com.hcyacg.pixiv"+ File.separator+ "tags.json")

Expand All @@ -34,7 +46,7 @@ object Nsfw {
logger.info("tags.json下载成功")
val jsonTemp = json.parseToJsonElement(file.readText())
jsonTemp.jsonObject["data"]!!.jsonObject.forEach { t, u ->
tag[t] = u.jsonPrimitive.content
tags[t] = u.jsonPrimitive.content
}
}

Expand All @@ -60,7 +72,7 @@ object Nsfw {
override fun onDownloadSuccess() {
logger.info("tags.json下载成功")
jsonTemp.jsonObject["data"]!!.jsonObject.forEach { t, u ->
tag[t] = u.jsonPrimitive.content
tags[t] = u.jsonPrimitive.content
}
}

Expand All @@ -75,12 +87,12 @@ object Nsfw {
)
}else{
jsonTemp.jsonObject["data"]!!.jsonObject.forEach { t, u ->
tag[t] = u.jsonPrimitive.content
tags[t] = u.jsonPrimitive.content
}
}
}else{
jsonTemp.jsonObject["data"]!!.jsonObject.forEach { t, u ->
tag[t] = u.jsonPrimitive.content
tags[t] = u.jsonPrimitive.content
}
}
}
Expand All @@ -89,91 +101,170 @@ object Nsfw {
}

suspend fun load(event: GroupMessageEvent) {
println("检测中")
val picUri = DataUtil.getSubString(event.message.toString().replace(" ", ""), "[overflow:image:{", "}.")!!
.replace("-", "")
val url = "https://gchat.qpic.cn/gchatpic_new/0/0-0-${picUri}/0?"
println("监控中……")
event.subject.sendMessage(At(event.sender).plus("检测中,请稍后"));

val picUri = DataUtil.getSubString(event.message.toString().replace("\\s*".toRegex(), "").replace(" ", ""), "[overflow:image,url=", "]")!!

// val uri = "http://${Config.deepdanbooru}/deepdanbooru"
// val file = ImageUtil.getImage(picUri, CacheUtil.Type.NONSUPPORT).toByteArray()
// val bodies = file.toRequestBody(
// "image/jpeg".toMediaTypeOrNull(),
// 0, file.size
// )
// val body = MultipartBody.Builder().setType(MultipartBody.FORM)
// .addFormDataPart("image","${picUri}.jpeg", bodies)
// .build()
// headers.add("Content-Type", "multipart/form-data;boundary=ebf9f03029db4c2799ae16b5428b06bd1")
// headers.add("Accept", "application/json")
// val data = RequestUtil.request(RequestUtil.Companion.Method.POST, uri,body,headers.build())
// println(data?.jsonPrimitive?.content)

val uri = "https://api.dnlab.net/animepic/upload"


val requestBody = MultipartBody.Builder().setType(MultipartBody.FORM)
val body = ImageUtil.getImage(url, CacheUtil.Type.NONSUPPORT).toByteArray()
val body = ImageUtil.getImage(picUri, CacheUtil.Type.NONSUPPORT).toByteArray()
val bodies = body.toRequestBody(
"image/*".toMediaTypeOrNull(),
0, body.size
)
requestBody.addFormDataPart("img", "${picUri}.png", bodies)

headers.add(
"User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36 Edg/85.0.564.44"
"multipart/form-data".toMediaTypeOrNull(),
0, body.size
)
requestBody.addFormDataPart("file", "${picUri}.jpeg", bodies)
requestBody.addFormDataPart("network_type", "general")

val uri = "http://dev.kanotype.net:8003/deepdanbooru/upload"

val host = Config.proxy.host
val port = Config.proxy.port
headers.add("Content-Type", "multipart/form-data;boundary=----WebKitFormBoundaryHxQHPpxAl7v7sCSa")
val response: Response = if (host.isBlank() || port == -1) {
client.build().newCall(Request.Builder().url(uri).headers(headers.build()).post(requestBody.build()).build()).execute()
} else {
val proxy = Proxy(Proxy.Type.HTTP, InetSocketAddress(host, port))
client.proxy(proxy).build().newCall(Request.Builder().url(uri).headers(headers.build()).post(requestBody.build()).build()).execute()
}


val data = RequestUtil.request(RequestUtil.Companion.Method.POST, uri, requestBody.build(), headers.build())


if (null == data) {
val location = response.header("location")
if (response.code != 302){
logger.warning("HTTP代码:${response.code}")
return
}

var doc = Jsoup.connect("http://dev.kanotype.net:8003$location").get()
doc = Jsoup.parse(doc.html())
val tables = doc.body().getElementsByTag("table")

//构建消息体
var quoteReply: MessageChain = QuoteReply(event.message).plus("")
val format = DecimalFormat("#.##")
//舍弃规则,RoundingMode.FLOOR表示直接舍弃。
format.roundingMode = RoundingMode.FLOOR
//安全性 system
quoteReply = quoteReply.plus("===安全性===\n")
data.jsonObject["system"]?.jsonObject?.forEach { t, u ->
var lv = format.format(u.jsonPrimitive.content.toFloat()).replace("0.", "")

if (lv.length < 2) {
lv = lv.plus("0")
}
tables.forEach { table ->
val theads = table.select("thead > tr > th")
val tbody = table.getElementsByTag("tbody")
theads.forEach { thead ->
val title = thead.text()
title.let {
if (it.contentEquals("General Tags")){
if (tbody.size>=1){
val trs = tbody[0].select("tr")
//其他标签 general
quoteReply = quoteReply.plus("===其他标签===\n")
trs.forEach {tr ->
val td = tr.select("td")
val tag = td[0].getElementsByTag("a").text()
val score = td[1].text()
var lv = format.format(score.toFloat()).replace("0.", "")
if (lv.length < 2) {
lv = lv.plus("0")
}
if (lv.toInt() >= 90) {
lv = lv.plus("%")
quoteReply = if (null != tags[tag] && tags[tag] != ""){
quoteReply.plus("${tags[tag]}(${tag}):$lv\n")
}else{
quoteReply.plus("${tag}:$lv\n")
}
}
}}

lv = lv.plus("%")
quoteReply = if (null != tag[t] && tag[t] != ""){
quoteReply.plus("${tag[t]}(${t}):$lv\n")
}else{
quoteReply.plus("${t}:$lv\n")
}
}
if (it.contentEquals("Character Tags")){
if (tbody.size>=2){
val trs = tbody[1].select("tr")
//角色识别 character
quoteReply = quoteReply.plus("===角色识别===\n")
trs.forEach {tr ->
println(tr)
val td = tr.select("td")
val tag = td[0].getElementsByTag("a").text()
val score = td[1].text()

var lv = format.format(score.toFloat()).replace("0.", "")
if (lv.length < 2) {
lv = lv.plus("0")
}
lv = lv.plus("%")
quoteReply = if (null != tags[tag] && tags[tag] != ""){
quoteReply.plus("${tags[tag]}(${tag}):$lv\n")
}else{
quoteReply.plus("${tag}:$lv\n")
}
}
}

}
}
if (it.contentEquals("System Tags")){
if (tbody.size>=3){
val trs = tbody[2].select("tr")
//安全性 system
quoteReply = quoteReply.plus("===安全性===\n")
trs.forEach {tr ->
val td = tr.select("td")
val tag = td[0].getElementsByTag("a").text()
val score = td[1].text()

var lv = format.format(score.toFloat()).replace("0.", "")

if (lv.length < 2) {
lv = lv.plus("0")
}

//角色识别 character
quoteReply = quoteReply.plus("===角色识别===\n")
lv = lv.plus("%")
quoteReply = if (null != tags[tag] && tags[tag] != ""){
quoteReply.plus("${tags[tag]}(${tag}):$lv\n")
}else{
quoteReply.plus("${tag}:$lv\n")
}
}
}

data.jsonObject["character"]?.jsonObject?.forEach { t, u ->
var lv = format.format(u.jsonPrimitive.content.toFloat()).replace("0.", "")
if (lv.length < 2) {
lv = lv.plus("0")
}
if (lv.toInt() >= 90) {
lv = lv.plus("%")
quoteReply = if (null != tag[t] && tag[t] != ""){
quoteReply.plus("${tag[t]}(${t}):$lv\n")
}else{
quoteReply.plus("${t}:$lv\n")
}
}
}
//其他标签 general
quoteReply = quoteReply.plus("===其他标签===\n")
data.jsonObject["general"]?.jsonObject?.forEach { t, u ->
var lv = format.format(u.jsonPrimitive.content.toFloat()).replace("0.", "")
if (lv.length < 2) {
lv = lv.plus("0")
}
if (lv.toInt() >= 90) {
lv = lv.plus("%")
quoteReply = if (null != tag[t] && tag[t] != ""){
quoteReply.plus("${tag[t]}(${t}):$lv\n")
}else{
quoteReply.plus("${t}:$lv\n")
}
}
}



}

// //其他标签 general
// quoteReply = quoteReply.plus("===其他标签===\n")
// data.jsonObject["general"]?.jsonObject?.forEach { t, u ->
// var lv = format.format(u.jsonPrimitive.content.toFloat()).replace("0.", "")
// if (lv.length < 2) {
// lv = lv.plus("0")
// }
// if (lv.toInt() >= 90) {
// lv = lv.plus("%")
// quoteReply = if (null != tag[t] && tag[t] != ""){
// quoteReply.plus("${tag[t]}(${t}):$lv\n")
// }else{
// quoteReply.plus("${t}:$lv\n")
// }
// }
//
// }
//
event.subject.sendMessage(quoteReply.plus("\n").plus("本功能不保证长期使用,并且标签为机翻,如果有错误请到Github仓库PR"))
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/hcyacg/Pixiv.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ object Pixiv : KotlinPlugin(
findUserWorksById.matcher(message.contentToString()).find() && !Setting.black.contains(group.id.toString())
} quoteReply { UserDetails.findUserWorksById(this) }
//测试成功
val searchInfoByPic: Pattern = Pattern.compile("(?i)^(${Command.searchInfoByPic}).+$")
val searchInfoByPic: Pattern = Pattern.compile("(?i)^(${Command.searchInfoByPic})(\\n){0,1}.+\$")
content { searchInfoByPic.matcher(message.contentToString()).find() && !Setting.black.contains(group.id.toString()) } quoteReply {
Trace.searchInfoByPic(
this
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/com/hcyacg/initial/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ object Config : AutoSavePluginConfig("Config") {
@ValueName("loliconSize")
@ValueDescription("lolicon图大小:original,regular,small,thumb,mini")
var loliconSize:String by value("original")

// @ValueName("deepdanbooru")
// @ValueDescription("https://github.com/nanoskript/deepdanbooru-docker")
// var deepdanbooru: String by value("192.168.10.108:7777")
}

@NoArgOpenDataClass
Expand Down
3 changes: 1 addition & 2 deletions src/main/kotlin/com/hcyacg/search/Ascii2d.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ object Ascii2d {
val sslsf = SSLConnectionSocketFactory(sc)
val httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build()

val url = "https://gchat.qpic.cn/gchatpic_new/0/0-0-${picUri}/0?"
val ascii2d = "https://ascii2d.net/search/url/$url"
val ascii2d = "https://ascii2d.net/search/url/$picUri"
// val headers = mutableMapOf<String,String>()
// headers["User-Agent"] = "PostmanRuntime/7.28.4"

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/hcyacg/search/Google.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ object Google {
try{
val host = Config.proxy.host
val port = Config.proxy.port
val uri = "${Config.googleConfig.googleUrl}/searchbyimage?image_url=https://gchat.qpic.cn/gchatpic_new/0/0-0-${picUri}/0&hl=zh-CN"
val uri = "${Config.googleConfig.googleUrl}/searchbyimage?image_url=${picUri}&hl=zh-CN"

val response: Response = if (host.isBlank() || port == -1) {
client.build().newCall(Request.Builder().url(uri).headers(headers.build()).get().build()).execute()
Expand Down
3 changes: 1 addition & 2 deletions src/main/kotlin/com/hcyacg/search/Iqdb.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ object Iqdb {
val list = mutableListOf<Message>()

try{
val url = "https://gchat.qpic.cn/gchatpic_new/0/0-0-${picUri}/0?"
val lqdb = "https://www.iqdb.org"
val message: Message = At(event.sender).plus("\n")


val doc: Document = Jsoup.connect(lqdb).data("url",url).timeout(60000).post()
val doc: Document = Jsoup.connect(lqdb).data("url",picUri).timeout(60000).post()
val select = doc.select("#pages div")
select.forEach {
if(!it.html().contains("Your image")){
Expand Down
Loading

0 comments on commit d91be3f

Please sign in to comment.