Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
971d7e18
Commit
971d7e18
authored
Feb 23, 2020
by
jiahua.liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
demo plugin
parent
ba4b2253
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
5794 additions
and
5905 deletions
+5794
-5905
mirai-plugins/image-sender/src/main/java/net/mamoe/mirai/imageplugin/ImageProvider.kt
...rc/main/java/net/mamoe/mirai/imageplugin/ImageProvider.kt
+0
-48
mirai-plugins/image-sender/src/main/java/net/mamoe/mirai/imageplugin/ImageSenderMain.kt
.../main/java/net/mamoe/mirai/imageplugin/ImageSenderMain.kt
+60
-11
mirai-plugins/image-sender/src/main/java/net/mamoe/mirai/imageplugin/Test.kt
...-sender/src/main/java/net/mamoe/mirai/imageplugin/Test.kt
+7
-2
mirai-plugins/image-sender/src/main/resources/data.yml
mirai-plugins/image-sender/src/main/resources/data.yml
+5725
-5842
mirai-plugins/image-sender/src/main/resources/plugin.yml
mirai-plugins/image-sender/src/main/resources/plugin.yml
+2
-2
No files found.
mirai-plugins/image-sender/src/main/java/net/mamoe/mirai/imageplugin/ImageProvider.kt
deleted
100644 → 0
View file @
ba4b2253
/*
* Copyright 2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package
net.mamoe.mirai.imageplugin
import
com.alibaba.fastjson.JSON
import
kotlinx.coroutines.*
import
net.mamoe.mirai.contact.Contact
import
net.mamoe.mirai.message.data.Image
import
net.mamoe.mirai.message.uploadAsImage
import
org.jsoup.Jsoup
class
ImageProvider
{
lateinit
var
contact
:
Contact
// `Deferred<Image?>` causes a runtime ClassCastException
val
image
:
Deferred
<
Image
>
by
lazy
{
GlobalScope
.
async
{
withTimeoutOrNull
(
5
*
1000
)
{
withContext
(
Dispatchers
.
IO
)
{
val
result
=
JSON
.
parseArray
(
Jsoup
.
connect
(
"https://yande.re/post.json?limit=1&page=${(Math.random() * 10000).toInt()}"
).
ignoreContentType
(
true
).
timeout
(
10
_0000
).
get
().
body
().
text
()
)
Jsoup
.
connect
(
result
.
getJSONObject
(
0
).
getString
(
"jpeg_url"
))
.
userAgent
(
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; ja-jp) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27"
)
.
timeout
(
10
_0000
)
.
ignoreContentType
(
true
)
.
maxBodySize
(
Int
.
MAX_VALUE
)
.
execute
()
.
bodyStream
()
}
}
?.
uploadAsImage
(
contact
)
?:
error
(
"Unable to download image|连接这个图站需要你的网络在外网"
)
}
}
}
mirai-plugins/image-sender/src/main/java/net/mamoe/mirai/imageplugin/ImageSenderMain.kt
View file @
971d7e18
...
...
@@ -9,15 +9,27 @@
package
net.mamoe.mirai.imageplugin
import
kotlinx.coroutines.ExperimentalCoroutinesApi
import
kotlinx.coroutines.GlobalScope
import
kotlinx.coroutines.*
import
net.mamoe.mirai.console.plugins.Config
import
net.mamoe.mirai.console.plugins.ConfigSection
import
net.mamoe.mirai.event.events.BotOnlineEvent
import
net.mamoe.mirai.event.subscribeAlways
import
net.mamoe.mirai.event.subscribeMessages
import
net.mamoe.mirai.console.plugins.PluginBase
import
net.mamoe.mirai.contact.Contact
import
net.mamoe.mirai.message.data.Image
import
net.mamoe.mirai.message.uploadAsImage
import
net.mamoe.mirai.utils.MiraiExperimentalAPI
import
org.jsoup.Jsoup
import
java.io.File
import
kotlin.random.Random
class
ImageSenderMain
:
PluginBase
()
{
lateinit
var
images
:
Config
lateinit
var
normal
:
List
<
ConfigSection
>
lateinit
var
r18
:
List
<
ConfigSection
>
@ExperimentalCoroutinesApi
@MiraiExperimentalAPI
override
fun
onEnable
()
{
...
...
@@ -25,24 +37,61 @@ class ImageSenderMain : PluginBase() {
GlobalScope
.
subscribeAlways
<
BotOnlineEvent
>
{
logger
.
info
(
"${this.bot.uin} login succeed, it will be controlled by Image Sender Plugin"
)
this
.
bot
.
subscribeMessages
{
case
(
"at me"
)
{
reply
(
sender
.
at
()
+
" ? "
)
(
contains
(
"色图"
))
{
try
{
with
(
normal
.
random
())
{
getImage
(
subject
,
this
.
getString
(
"url"
),
this
.
getString
(
"pid"
)
).
plus
(
this
.
getString
(
"tags"
)).
send
()
}
}
catch
(
e
:
Exception
)
{
reply
(
e
.
message
?:
"unknown error"
)
}
}
(
contains
(
"image"
)
or
contains
(
"图"
))
{
"图片发送中"
.
reply
()
ImageProvider
().
apply
{
this
.
contact
=
sender
}.
image
.
await
().
reply
()
(
contains
(
"不够色"
))
{
try
{
with
(
r18
.
random
())
{
getImage
(
subject
,
this
.
getString
(
"url"
),
this
.
getString
(
"pid"
)
).
plus
(
this
.
getString
(
"tags"
)).
send
()
}
}
catch
(
e
:
Exception
)
{
reply
(
e
.
message
?:
"unknown error"
)
}
}
}
}
}
suspend
fun
getImage
(
contact
:
Contact
,
url
:
String
,
pid
:
String
):
Image
{
return
withTimeoutOrNull
(
20
*
1000
)
{
withContext
(
Dispatchers
.
IO
)
{
Jsoup
.
connect
(
url
)
.
followRedirects
(
true
)
.
timeout
(
180
_000
)
.
ignoreContentType
(
true
)
.
userAgent
(
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; ja-jp) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27"
)
.
referrer
(
"https://www.pixiv.net/member_illust.php?mode=medium&illust_id=$pid"
)
.
ignoreHttpErrors
(
true
)
.
maxBodySize
(
100000000
)
.
execute
().
also
{
check
(
it
.
statusCode
()
==
200
)
{
"Failed to download image"
}
}
}
}
?.
bodyStream
()
?.
uploadAsImage
(
contact
)
?:
error
(
"Unable to download image"
)
}
override
fun
onLoad
()
{
logger
.
info
(
"loading..."
)
logger
.
info
(
"loading local image data"
)
try
{
images
=
Config
.
load
(
this
.
javaClass
.
classLoader
.
getResource
(
"data.yml"
)
!!
.
path
!!
)
}
catch
(
e
:
Exception
)
{
logger
.
info
(
"无法加载本地图片"
)
}
logger
.
info
(
"本地图片版本"
+
images
.
getString
(
"version"
))
logger
.
info
(
"Normal * "
+
images
.
getList
(
"normal"
).
size
)
logger
.
info
(
"R18 * "
+
images
.
getList
(
"R18"
).
size
)
}
override
fun
onDisable
()
{
...
...
mirai-plugins/image-sender/src/main/
resources
/Test.kt
→
mirai-plugins/image-sender/src/main/
java/net/mamoe/mirai/imageplugin
/Test.kt
View file @
971d7e18
...
...
@@ -30,6 +30,7 @@ object Data {
}
fun
main
()
{
val
abstract_file
=
(
File
(
System
.
getProperty
(
"user.dir"
)
+
"/abstractSetu.yml"
)).
loadAsConfig
()
abstract_file
.
setIfAbsent
(
"R18"
,
mutableListOf
<
ConfigSection
>())
...
...
@@ -53,6 +54,7 @@ fun main() {
this
[
"author"
]
=
it
[
"author"
]
!!
this
[
"uid"
]
=
it
[
"uid"
]
!!
this
[
"tags"
]
=
it
[
"tags"
]
!!
this
[
"url"
]
=
it
[
"url"
]
!!
}
)
}
...
...
@@ -73,6 +75,7 @@ fun main() {
this
[
"author"
]
=
it
[
"author"
]
!!
this
[
"uid"
]
=
it
[
"uid"
]
!!
this
[
"tags"
]
=
it
[
"tags"
]
!!
this
[
"url"
]
=
it
[
"url"
]
!!
}
)
}
...
...
@@ -87,6 +90,7 @@ fun main() {
Runtime.getRuntime().addShutdownHook(thread(start = false) {
Data.save()
})
while (true){
try {
val val0 = JSONObject.parseObject(Jsoup
...
...
@@ -116,6 +120,7 @@ fun main() {
configSection["tags"] = content.getJSONArray("tags").map {
it.toString()
}.joinToString(",")
configSection["url"] = content.getString("url")
if(isR18){
Data.R18.add(configSection)
print("获取到了一张R18")
...
...
@@ -131,9 +136,9 @@ fun main() {
}
Data.save()
println("SAVED")
Thread.sleep(1
25
0)
Thread.sleep(1
00
0)
}
*/
*/
}
\ No newline at end of file
mirai-plugins/image-sender/src/main/resources/data.yml
View file @
971d7e18
This diff is collapsed.
Click to expand it.
mirai-plugins/image-sender/src/main/resources/plugin.yml
View file @
971d7e18
name
:
ImageSender
main
:
net.mamoe.mirai.imageplugin.ImageSenderMain
version
:
1.0.0
author
:
mamoe
info
:
a demo plugin of mirai
author
:
不想写代码
info
:
a demo
[hso]
plugin of mirai
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment