Skip to content

Commit d91be3f

Browse files
committed
Fix: All Pic Url Error
1 parent 5e9395d commit d91be3f

File tree

12 files changed

+187
-92
lines changed

12 files changed

+187
-92
lines changed

build.gradle.kts

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ plugins {
1212
}
1313

1414
group = "com.hcyacg"
15-
version = "1.7.7"
15+
version = "1.7.8"
1616

1717
repositories {
1818
// maven("https://maven.aliyun.com/repository/public")
@@ -63,6 +63,7 @@ allOpen{
6363
val compileKotlin: KotlinCompile by tasks
6464
compileKotlin.kotlinOptions {
6565
jvmTarget = "1.8"
66+
6667
}
6768

6869
val compileTestKotlin: KotlinCompile by tasks

src/main/kotlin/com/hcyacg/Nsfw.kt

+160-69
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,39 @@
11
package com.hcyacg
22

33

4+
import com.hcyacg.initial.Config
5+
import com.hcyacg.search.Yandex
46
import com.hcyacg.utils.*
57
import kotlinx.serialization.json.*
68
import net.mamoe.mirai.console.MiraiConsole
79
import net.mamoe.mirai.event.events.GroupMessageEvent
10+
import net.mamoe.mirai.message.data.At
811
import net.mamoe.mirai.message.data.MessageChain
912
import net.mamoe.mirai.message.data.QuoteReply
1013
import net.mamoe.mirai.utils.MiraiLogger
1114
import okhttp3.*
1215
import okhttp3.MediaType.Companion.toMediaTypeOrNull
16+
import okhttp3.RequestBody.Companion.asRequestBody
1317
import okhttp3.RequestBody.Companion.toRequestBody
18+
import org.jsoup.Jsoup
19+
import org.jsoup.nodes.Document
1420
import java.io.File
1521
import java.math.RoundingMode
22+
import java.net.InetSocketAddress
23+
import java.net.Proxy
1624
import java.text.DecimalFormat
25+
import java.util.concurrent.TimeUnit
26+
1727

1828
object Nsfw {
1929
private val logger = MiraiLogger.Factory.create(this::class.java)
2030
private val headers = Headers.Builder()
21-
22-
private val tag = mutableMapOf<String,String>()
31+
private var requestBody: RequestBody? = null
32+
private val client = OkHttpClient().newBuilder().connectTimeout(60000, TimeUnit.MILLISECONDS)
33+
.readTimeout(60000, TimeUnit.MILLISECONDS).followRedirects(false)
34+
private val tags = mutableMapOf<String,String>()
2335
private val json = Json
24-
const val url = "https://raw.fastgit.org/Nekoer/mirai-plugins-pixiv/master/src/main/resources/tags.json"
36+
const val url = "https://fastly.jsdelivr.net/gh/Nekoer/mirai-plugins-pixiv@master/src/main/resources/tags.json"
2537
init {
2638
val file = File(MiraiConsole.pluginManager.pluginsConfigFolder.path + File.separator +"com.hcyacg.pixiv"+ File.separator+ "tags.json")
2739

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

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

@@ -75,12 +87,12 @@ object Nsfw {
7587
)
7688
}else{
7789
jsonTemp.jsonObject["data"]!!.jsonObject.forEach { t, u ->
78-
tag[t] = u.jsonPrimitive.content
90+
tags[t] = u.jsonPrimitive.content
7991
}
8092
}
8193
}else{
8294
jsonTemp.jsonObject["data"]!!.jsonObject.forEach { t, u ->
83-
tag[t] = u.jsonPrimitive.content
95+
tags[t] = u.jsonPrimitive.content
8496
}
8597
}
8698
}
@@ -89,91 +101,170 @@ object Nsfw {
89101
}
90102

91103
suspend fun load(event: GroupMessageEvent) {
92-
println("检测中")
93-
val picUri = DataUtil.getSubString(event.message.toString().replace(" ", ""), "[overflow:image:{", "}.")!!
94-
.replace("-", "")
95-
val url = "https://gchat.qpic.cn/gchatpic_new/0/0-0-${picUri}/0?"
104+
println("监控中……")
105+
event.subject.sendMessage(At(event.sender).plus("检测中,请稍后"));
106+
107+
val picUri = DataUtil.getSubString(event.message.toString().replace("\\s*".toRegex(), "").replace(" ", ""), "[overflow:image,url=", "]")!!
108+
109+
// val uri = "http://${Config.deepdanbooru}/deepdanbooru"
110+
// val file = ImageUtil.getImage(picUri, CacheUtil.Type.NONSUPPORT).toByteArray()
111+
// val bodies = file.toRequestBody(
112+
// "image/jpeg".toMediaTypeOrNull(),
113+
// 0, file.size
114+
// )
115+
// val body = MultipartBody.Builder().setType(MultipartBody.FORM)
116+
// .addFormDataPart("image","${picUri}.jpeg", bodies)
117+
// .build()
118+
// headers.add("Content-Type", "multipart/form-data;boundary=ebf9f03029db4c2799ae16b5428b06bd1")
119+
// headers.add("Accept", "application/json")
120+
// val data = RequestUtil.request(RequestUtil.Companion.Method.POST, uri,body,headers.build())
121+
// println(data?.jsonPrimitive?.content)
96122

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

99124

100125
val requestBody = MultipartBody.Builder().setType(MultipartBody.FORM)
101-
val body = ImageUtil.getImage(url, CacheUtil.Type.NONSUPPORT).toByteArray()
126+
val body = ImageUtil.getImage(picUri, CacheUtil.Type.NONSUPPORT).toByteArray()
102127
val bodies = body.toRequestBody(
103-
"image/*".toMediaTypeOrNull(),
104-
0, body.size
105-
)
106-
requestBody.addFormDataPart("img", "${picUri}.png", bodies)
107-
108-
headers.add(
109-
"User-Agent",
110-
"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"
128+
"multipart/form-data".toMediaTypeOrNull(),
129+
0, body.size
111130
)
131+
requestBody.addFormDataPart("file", "${picUri}.jpeg", bodies)
132+
requestBody.addFormDataPart("network_type", "general")
133+
134+
val uri = "http://dev.kanotype.net:8003/deepdanbooru/upload"
135+
136+
val host = Config.proxy.host
137+
val port = Config.proxy.port
138+
headers.add("Content-Type", "multipart/form-data;boundary=----WebKitFormBoundaryHxQHPpxAl7v7sCSa")
139+
val response: Response = if (host.isBlank() || port == -1) {
140+
client.build().newCall(Request.Builder().url(uri).headers(headers.build()).post(requestBody.build()).build()).execute()
141+
} else {
142+
val proxy = Proxy(Proxy.Type.HTTP, InetSocketAddress(host, port))
143+
client.proxy(proxy).build().newCall(Request.Builder().url(uri).headers(headers.build()).post(requestBody.build()).build()).execute()
144+
}
112145

113-
114-
val data = RequestUtil.request(RequestUtil.Companion.Method.POST, uri, requestBody.build(), headers.build())
115-
116-
117-
if (null == data) {
146+
val location = response.header("location")
147+
if (response.code != 302){
148+
logger.warning("HTTP代码:${response.code}")
118149
return
119150
}
151+
152+
var doc = Jsoup.connect("http://dev.kanotype.net:8003$location").get()
153+
doc = Jsoup.parse(doc.html())
154+
val tables = doc.body().getElementsByTag("table")
155+
156+
//构建消息体
120157
var quoteReply: MessageChain = QuoteReply(event.message).plus("")
121158
val format = DecimalFormat("#.##")
122159
//舍弃规则,RoundingMode.FLOOR表示直接舍弃。
123160
format.roundingMode = RoundingMode.FLOOR
124-
//安全性 system
125-
quoteReply = quoteReply.plus("===安全性===\n")
126-
data.jsonObject["system"]?.jsonObject?.forEach { t, u ->
127-
var lv = format.format(u.jsonPrimitive.content.toFloat()).replace("0.", "")
128161

129-
if (lv.length < 2) {
130-
lv = lv.plus("0")
131-
}
162+
tables.forEach { table ->
163+
val theads = table.select("thead > tr > th")
164+
val tbody = table.getElementsByTag("tbody")
165+
theads.forEach { thead ->
166+
val title = thead.text()
167+
title.let {
168+
if (it.contentEquals("General Tags")){
169+
if (tbody.size>=1){
170+
val trs = tbody[0].select("tr")
171+
//其他标签 general
172+
quoteReply = quoteReply.plus("===其他标签===\n")
173+
trs.forEach {tr ->
174+
val td = tr.select("td")
175+
val tag = td[0].getElementsByTag("a").text()
176+
val score = td[1].text()
177+
var lv = format.format(score.toFloat()).replace("0.", "")
178+
if (lv.length < 2) {
179+
lv = lv.plus("0")
180+
}
181+
if (lv.toInt() >= 90) {
182+
lv = lv.plus("%")
183+
quoteReply = if (null != tags[tag] && tags[tag] != ""){
184+
quoteReply.plus("${tags[tag]}(${tag}):$lv\n")
185+
}else{
186+
quoteReply.plus("${tag}:$lv\n")
187+
}
188+
}
189+
}}
132190

133-
lv = lv.plus("%")
134-
quoteReply = if (null != tag[t] && tag[t] != ""){
135-
quoteReply.plus("${tag[t]}(${t}):$lv\n")
136-
}else{
137-
quoteReply.plus("${t}:$lv\n")
138-
}
191+
}
192+
if (it.contentEquals("Character Tags")){
193+
if (tbody.size>=2){
194+
val trs = tbody[1].select("tr")
195+
//角色识别 character
196+
quoteReply = quoteReply.plus("===角色识别===\n")
197+
trs.forEach {tr ->
198+
println(tr)
199+
val td = tr.select("td")
200+
val tag = td[0].getElementsByTag("a").text()
201+
val score = td[1].text()
202+
203+
var lv = format.format(score.toFloat()).replace("0.", "")
204+
if (lv.length < 2) {
205+
lv = lv.plus("0")
206+
}
207+
lv = lv.plus("%")
208+
quoteReply = if (null != tags[tag] && tags[tag] != ""){
209+
quoteReply.plus("${tags[tag]}(${tag}):$lv\n")
210+
}else{
211+
quoteReply.plus("${tag}:$lv\n")
212+
}
213+
}
214+
}
139215

140-
}
216+
}
217+
if (it.contentEquals("System Tags")){
218+
if (tbody.size>=3){
219+
val trs = tbody[2].select("tr")
220+
//安全性 system
221+
quoteReply = quoteReply.plus("===安全性===\n")
222+
trs.forEach {tr ->
223+
val td = tr.select("td")
224+
val tag = td[0].getElementsByTag("a").text()
225+
val score = td[1].text()
226+
227+
var lv = format.format(score.toFloat()).replace("0.", "")
228+
229+
if (lv.length < 2) {
230+
lv = lv.plus("0")
231+
}
141232

142-
//角色识别 character
143-
quoteReply = quoteReply.plus("===角色识别===\n")
233+
lv = lv.plus("%")
234+
quoteReply = if (null != tags[tag] && tags[tag] != ""){
235+
quoteReply.plus("${tags[tag]}(${tag}):$lv\n")
236+
}else{
237+
quoteReply.plus("${tag}:$lv\n")
238+
}
239+
}
240+
}
144241

145-
data.jsonObject["character"]?.jsonObject?.forEach { t, u ->
146-
var lv = format.format(u.jsonPrimitive.content.toFloat()).replace("0.", "")
147-
if (lv.length < 2) {
148-
lv = lv.plus("0")
149-
}
150-
if (lv.toInt() >= 90) {
151-
lv = lv.plus("%")
152-
quoteReply = if (null != tag[t] && tag[t] != ""){
153-
quoteReply.plus("${tag[t]}(${t}):$lv\n")
154-
}else{
155-
quoteReply.plus("${t}:$lv\n")
156-
}
157-
}
158-
}
159-
//其他标签 general
160-
quoteReply = quoteReply.plus("===其他标签===\n")
161-
data.jsonObject["general"]?.jsonObject?.forEach { t, u ->
162-
var lv = format.format(u.jsonPrimitive.content.toFloat()).replace("0.", "")
163-
if (lv.length < 2) {
164-
lv = lv.plus("0")
165-
}
166-
if (lv.toInt() >= 90) {
167-
lv = lv.plus("%")
168-
quoteReply = if (null != tag[t] && tag[t] != ""){
169-
quoteReply.plus("${tag[t]}(${t}):$lv\n")
170-
}else{
171-
quoteReply.plus("${t}:$lv\n")
242+
}
172243
}
173244
}
174245

246+
247+
175248
}
176249

250+
// //其他标签 general
251+
// quoteReply = quoteReply.plus("===其他标签===\n")
252+
// data.jsonObject["general"]?.jsonObject?.forEach { t, u ->
253+
// var lv = format.format(u.jsonPrimitive.content.toFloat()).replace("0.", "")
254+
// if (lv.length < 2) {
255+
// lv = lv.plus("0")
256+
// }
257+
// if (lv.toInt() >= 90) {
258+
// lv = lv.plus("%")
259+
// quoteReply = if (null != tag[t] && tag[t] != ""){
260+
// quoteReply.plus("${tag[t]}(${t}):$lv\n")
261+
// }else{
262+
// quoteReply.plus("${t}:$lv\n")
263+
// }
264+
// }
265+
//
266+
// }
267+
//
177268
event.subject.sendMessage(quoteReply.plus("\n").plus("本功能不保证长期使用,并且标签为机翻,如果有错误请到Github仓库PR"))
178269
}
179270

src/main/kotlin/com/hcyacg/Pixiv.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ object Pixiv : KotlinPlugin(
8080
findUserWorksById.matcher(message.contentToString()).find() && !Setting.black.contains(group.id.toString())
8181
} quoteReply { UserDetails.findUserWorksById(this) }
8282
//测试成功
83-
val searchInfoByPic: Pattern = Pattern.compile("(?i)^(${Command.searchInfoByPic}).+$")
83+
val searchInfoByPic: Pattern = Pattern.compile("(?i)^(${Command.searchInfoByPic})(\\n){0,1}.+\$")
8484
content { searchInfoByPic.matcher(message.contentToString()).find() && !Setting.black.contains(group.id.toString()) } quoteReply {
8585
Trace.searchInfoByPic(
8686
this

src/main/kotlin/com/hcyacg/initial/Config.kt

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ object Config : AutoSavePluginConfig("Config") {
5555
@ValueName("loliconSize")
5656
@ValueDescription("lolicon图大小:original,regular,small,thumb,mini")
5757
var loliconSize:String by value("original")
58+
59+
// @ValueName("deepdanbooru")
60+
// @ValueDescription("https://github.com/nanoskript/deepdanbooru-docker")
61+
// var deepdanbooru: String by value("192.168.10.108:7777")
5862
}
5963

6064
@NoArgOpenDataClass

src/main/kotlin/com/hcyacg/search/Ascii2d.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ object Ascii2d {
6464
val sslsf = SSLConnectionSocketFactory(sc)
6565
val httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build()
6666

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

src/main/kotlin/com/hcyacg/search/Google.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ object Google {
3434
try{
3535
val host = Config.proxy.host
3636
val port = Config.proxy.port
37-
val uri = "${Config.googleConfig.googleUrl}/searchbyimage?image_url=https://gchat.qpic.cn/gchatpic_new/0/0-0-${picUri}/0&hl=zh-CN"
37+
val uri = "${Config.googleConfig.googleUrl}/searchbyimage?image_url=${picUri}&hl=zh-CN"
3838

3939
val response: Response = if (host.isBlank() || port == -1) {
4040
client.build().newCall(Request.Builder().url(uri).headers(headers.build()).get().build()).execute()

src/main/kotlin/com/hcyacg/search/Iqdb.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ object Iqdb {
2525
val list = mutableListOf<Message>()
2626

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

3231

33-
val doc: Document = Jsoup.connect(lqdb).data("url",url).timeout(60000).post()
32+
val doc: Document = Jsoup.connect(lqdb).data("url",picUri).timeout(60000).post()
3433
val select = doc.select("#pages div")
3534
select.forEach {
3635
if(!it.html().contains("Your image")){

0 commit comments

Comments
 (0)