1
1
package com.hcyacg
2
2
3
3
4
+ import com.hcyacg.initial.Config
5
+ import com.hcyacg.search.Yandex
4
6
import com.hcyacg.utils.*
5
7
import kotlinx.serialization.json.*
6
8
import net.mamoe.mirai.console.MiraiConsole
7
9
import net.mamoe.mirai.event.events.GroupMessageEvent
10
+ import net.mamoe.mirai.message.data.At
8
11
import net.mamoe.mirai.message.data.MessageChain
9
12
import net.mamoe.mirai.message.data.QuoteReply
10
13
import net.mamoe.mirai.utils.MiraiLogger
11
14
import okhttp3.*
12
15
import okhttp3.MediaType.Companion.toMediaTypeOrNull
16
+ import okhttp3.RequestBody.Companion.asRequestBody
13
17
import okhttp3.RequestBody.Companion.toRequestBody
18
+ import org.jsoup.Jsoup
19
+ import org.jsoup.nodes.Document
14
20
import java.io.File
15
21
import java.math.RoundingMode
22
+ import java.net.InetSocketAddress
23
+ import java.net.Proxy
16
24
import java.text.DecimalFormat
25
+ import java.util.concurrent.TimeUnit
26
+
17
27
18
28
object Nsfw {
19
29
private val logger = MiraiLogger .Factory .create(this ::class .java)
20
30
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 >()
23
35
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"
25
37
init {
26
38
val file = File (MiraiConsole .pluginManager.pluginsConfigFolder.path + File .separator + " com.hcyacg.pixiv" + File .separator+ " tags.json" )
27
39
@@ -34,7 +46,7 @@ object Nsfw {
34
46
logger.info(" tags.json下载成功" )
35
47
val jsonTemp = json.parseToJsonElement(file.readText())
36
48
jsonTemp.jsonObject[" data" ]!! .jsonObject.forEach { t, u ->
37
- tag [t] = u.jsonPrimitive.content
49
+ tags [t] = u.jsonPrimitive.content
38
50
}
39
51
}
40
52
@@ -60,7 +72,7 @@ object Nsfw {
60
72
override fun onDownloadSuccess () {
61
73
logger.info(" tags.json下载成功" )
62
74
jsonTemp.jsonObject[" data" ]!! .jsonObject.forEach { t, u ->
63
- tag [t] = u.jsonPrimitive.content
75
+ tags [t] = u.jsonPrimitive.content
64
76
}
65
77
}
66
78
@@ -75,12 +87,12 @@ object Nsfw {
75
87
)
76
88
}else {
77
89
jsonTemp.jsonObject[" data" ]!! .jsonObject.forEach { t, u ->
78
- tag [t] = u.jsonPrimitive.content
90
+ tags [t] = u.jsonPrimitive.content
79
91
}
80
92
}
81
93
}else {
82
94
jsonTemp.jsonObject[" data" ]!! .jsonObject.forEach { t, u ->
83
- tag [t] = u.jsonPrimitive.content
95
+ tags [t] = u.jsonPrimitive.content
84
96
}
85
97
}
86
98
}
@@ -89,91 +101,170 @@ object Nsfw {
89
101
}
90
102
91
103
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)
96
122
97
- val uri = " https://api.dnlab.net/animepic/upload"
98
123
99
124
100
125
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()
102
127
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
111
130
)
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
+ }
112
145
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} " )
118
149
return
119
150
}
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
+ // 构建消息体
120
157
var quoteReply: MessageChain = QuoteReply (event.message).plus(" " )
121
158
val format = DecimalFormat (" #.##" )
122
159
// 舍弃规则,RoundingMode.FLOOR表示直接舍弃。
123
160
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." , " " )
128
161
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
+ }}
132
190
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
+ }
139
215
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
+ }
141
232
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
+ }
144
241
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
+ }
172
243
}
173
244
}
174
245
246
+
247
+
175
248
}
176
249
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
+ //
177
268
event.subject.sendMessage(quoteReply.plus(" \n " ).plus(" 本功能不保证长期使用,并且标签为机翻,如果有错误请到Github仓库PR" ))
178
269
}
179
270
0 commit comments