Commit d4dd720e authored by Him188's avatar Him188

update demo

parent d3270bb3
......@@ -94,7 +94,6 @@ kotlin {
}
compileKotlinJvm {
}
compileTestJava{
......
......@@ -37,11 +37,7 @@ class GentleImage {
lateinit var contact: Contact
val image: Deferred<Image> by lazy {
GlobalScope.async {
// runBlocking {
// CompletableDeferred(suspend {
delay((Math.random() * 5000L).toLong())
// MiraiLogger.logPurple("Downloading image: $name")
withContext(Dispatchers.IO) {
Jsoup.connect(sample_url)
.userAgent(UserAgent.randomUserAgent)
......@@ -50,11 +46,7 @@ class GentleImage {
.maxBodySize(Int.MAX_VALUE)
.execute()
.bodyStream()
}.upload(contact).also {
//MiraiLogger.logPurple("Downloaded image: $name")
}
// }())
// }
}.upload(contact)
}
}
}
......
......@@ -10,20 +10,20 @@ import net.mamoe.mirai.contact.Contact
/**
* 最少缓存的图片数量
*/
private const val IMAGE_BUFFER_CAPACITY: Int = 50
private const val IMAGE_BUFFER_CAPACITY: Int = 5
/**
* 每次补充的数量
* 为不同的联系人提供图片
*/
private const val FILL_COUNT: Int = IMAGE_BUFFER_CAPACITY
@ExperimentalUnsignedTypes
@ExperimentalCoroutinesApi
object Gentlemen : MutableMap<UInt, Gentleman> by mutableMapOf() {
fun getOrPut(key: Contact): Gentleman = this.getOrPut(key.id) { Gentleman(key) }
}
/**
* 工作是缓存图片
*/
@ExperimentalCoroutinesApi
class Gentleman(private val contact: Contact) : Channel<GentleImage> by Channel(IMAGE_BUFFER_CAPACITY) {
init {
......@@ -34,38 +34,8 @@ class Gentleman(private val contact: Contact) : Channel<GentleImage> by Channel(
sample_url = "http://dev.itxtech.org:10322/randomImg.uue?tdsourcetag=s_pctim_aiomsg&size=large"
contact = this@Gentleman.contact
image.await()
image// start downloading
})
/*
val response = withContext(Dispatchers.IO) {
tryNTimes(2) {
Jsoup.connect("https://yande.re/post.json?")
.userAgent(UserAgent.randomUserAgent)
.data("limit", "20")
.data("page", (Random.Default.nextInt(12000)).toString())
.ignoreContentType(true)
.timeout(20_000)
.method(Connection.Method.GET)
.execute()
}
}
check(response.statusCode() == 200) { "failed to get resources" }
JSONArray.parseArray(response.body(), GentleImage::class.java)
.filterNot { it.tags in ForbiddenKeyWords }
.sortedBy { it.score }
.let {
if (it.size <= FILL_COUNT) {
it
} else it.slice(0..FILL_COUNT)
}
.forEach {
it.contact = contact
it.image//start downloading
send(it)
}*/
}
}
}
......
......@@ -7,7 +7,6 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import net.mamoe.mirai.Bot
import net.mamoe.mirai.BotAccount
import net.mamoe.mirai.event.subscribeGroupMessages
import net.mamoe.mirai.event.subscribeMessages
import net.mamoe.mirai.login
import net.mamoe.mirai.network.protocol.tim.packet.login.requireSuccess
......@@ -55,16 +54,5 @@ suspend fun main() {
}
bot.subscribeGroupMessages {
startsWith("图片", removePrefix = true) {
HPictureSession(group, sender, it)
}
startsWith("minstar=", removePrefix = true) {
minstar = it.toInt()
reply("minStar set to $minstar")
}
}
bot.network.awaitDisconnection()//等到直到断开连接
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment