Commit 22f2c508 authored by Him188's avatar Him188

Reformat code

parent aa7a3206
...@@ -9,7 +9,7 @@ import net.mamoe.mirai.message.MessagePacket ...@@ -9,7 +9,7 @@ import net.mamoe.mirai.message.MessagePacket
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext import kotlin.coroutines.EmptyCoroutineContext
tailrec fun generateSessionKey():String{ tailrec fun generateSessionKey(): String {
fun generateRandomSessionKey(): String { fun generateRandomSessionKey(): String {
val all = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm" val all = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm"
return buildString(capacity = 8) { return buildString(capacity = 8) {
...@@ -20,27 +20,27 @@ tailrec fun generateSessionKey():String{ ...@@ -20,27 +20,27 @@ tailrec fun generateSessionKey():String{
} }
val key = generateRandomSessionKey() val key = generateRandomSessionKey()
if(!SessionManager.allSession.containsKey(key)){ if (!SessionManager.allSession.containsKey(key)) {
return key return key
} }
return generateSessionKey() return generateSessionKey()
} }
object SessionManager { internal object SessionManager {
val allSession:MutableMap<String,Session> = mutableMapOf() val allSession: MutableMap<String, Session> = mutableMapOf()
lateinit var authKey:String lateinit var authKey: String
fun createTempSession():TempSession = TempSession(EmptyCoroutineContext).also {newTempSession -> fun createTempSession(): TempSession = TempSession(EmptyCoroutineContext).also { newTempSession ->
allSession[newTempSession.key] = newTempSession allSession[newTempSession.key] = newTempSession
//设置180000ms后检测并回收 //设置180000ms后检测并回收
newTempSession.launch{ newTempSession.launch {
delay(180000) delay(180000)
allSession[newTempSession.key]?.run { allSession[newTempSession.key]?.run {
if(this is TempSession) if (this is TempSession)
closeSession(newTempSession.key) closeSession(newTempSession.key)
} }
} }
...@@ -50,15 +50,13 @@ object SessionManager { ...@@ -50,15 +50,13 @@ object SessionManager {
fun containSession(sessionKey: String): Boolean = allSession.containsKey(sessionKey) fun containSession(sessionKey: String): Boolean = allSession.containsKey(sessionKey)
fun closeSession(sessionKey: String) = allSession.remove(sessionKey)?.also {it.close() } fun closeSession(sessionKey: String) = allSession.remove(sessionKey)?.also { it.close() }
fun closeSession(session: Session) = closeSession(session.key) fun closeSession(session: Session) = closeSession(session.key)
} }
/** /**
* @author NaturalHG * @author NaturalHG
* 这个用于管理不同Client与Mirai HTTP的会话 * 这个用于管理不同Client与Mirai HTTP的会话
...@@ -68,20 +66,19 @@ object SessionManager { ...@@ -68,20 +66,19 @@ object SessionManager {
*/ */
abstract class Session internal constructor( abstract class Session internal constructor(
coroutineContext: CoroutineContext coroutineContext: CoroutineContext
): CoroutineScope { ) : CoroutineScope {
val supervisorJob = SupervisorJob(coroutineContext[Job]) val supervisorJob = SupervisorJob(coroutineContext[Job])
final override val coroutineContext: CoroutineContext = supervisorJob + coroutineContext final override val coroutineContext: CoroutineContext = supervisorJob + coroutineContext
val key:String = generateSessionKey() val key: String = generateSessionKey()
internal open fun close(){ internal open fun close() {
supervisorJob.complete() supervisorJob.complete()
} }
} }
/** /**
* 任何新链接建立后分配一个[TempSession] * 任何新链接建立后分配一个[TempSession]
* *
...@@ -93,10 +90,10 @@ class TempSession internal constructor(coroutineContext: CoroutineContext) : Ses ...@@ -93,10 +90,10 @@ class TempSession internal constructor(coroutineContext: CoroutineContext) : Ses
* 任何[TempSession]认证后转化为一个[AuthedSession] * 任何[TempSession]认证后转化为一个[AuthedSession]
* 在这一步[AuthedSession]应该已经有assigned的bot * 在这一步[AuthedSession]应该已经有assigned的bot
*/ */
class AuthedSession internal constructor(val bot: Bot, coroutineContext: CoroutineContext):Session(coroutineContext){ class AuthedSession internal constructor(val bot: Bot, coroutineContext: CoroutineContext) : Session(coroutineContext) {
val messageQueue = MessageQueue() val messageQueue = MessageQueue()
private val _listener : Listener<MessagePacket<*, *>> private val _listener: Listener<MessagePacket<*, *>>
init { init {
bot.subscribeMessages { bot.subscribeMessages {
......
...@@ -70,6 +70,7 @@ fun ByteReadPacket.debugPrintThis(name: String = ""): ByteReadPacket { ...@@ -70,6 +70,7 @@ fun ByteReadPacket.debugPrintThis(name: String = ""): ByteReadPacket {
@MiraiDebugAPI("Low efficiency") @MiraiDebugAPI("Low efficiency")
@UseExperimental(ExperimentalContracts::class) @UseExperimental(ExperimentalContracts::class)
inline fun <R> Input.debugIfFail(name: String = "", onFail: (ByteArray) -> ByteReadPacket = { it.toReadPacket() }, block: ByteReadPacket.() -> R): R { inline fun <R> Input.debugIfFail(name: String = "", onFail: (ByteArray) -> ByteReadPacket = { it.toReadPacket() }, block: ByteReadPacket.() -> R): R {
contract { contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE) callsInPlace(block, InvocationKind.EXACTLY_ONCE)
callsInPlace(onFail, InvocationKind.UNKNOWN) callsInPlace(onFail, InvocationKind.UNKNOWN)
......
package demo.gentleman package demo.gentleman
import com.alibaba.fastjson.JSON import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import kotlinx.coroutines.* import kotlinx.coroutines.*
import net.mamoe.mirai.contact.Contact import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.message.data.Image import net.mamoe.mirai.message.data.Image
import net.mamoe.mirai.message.uploadAsImage import net.mamoe.mirai.message.uploadAsImage
import org.jsoup.Jsoup import org.jsoup.Jsoup
import kotlin.random.Random
class GentleImage { class GentleImage(val contact: Contact, val keyword: String) {
lateinit var contact: Contact
// `Deferred<Image?>` causes a runtime ClassCastException
val image: Deferred<Image> by lazy { getImage(0) } val image: Deferred<Image> by lazy { getImage(0) }
...@@ -18,18 +17,21 @@ class GentleImage { ...@@ -18,18 +17,21 @@ class GentleImage {
fun getImage(r18: Int): Deferred<Image> { fun getImage(r18: Int): Deferred<Image> {
return GlobalScope.async { return GlobalScope.async {
withTimeoutOrNull(5 * 1000) { withTimeoutOrNull(10 * 1000) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val result = val result =
JSON.parseObject( JSON.parseObject(
Jsoup.connect("https://api.lolicon.app/setu/?r18=$r18").ignoreContentType(true).timeout( Jsoup.connect("https://api.lolicon.app/setu/?r18=$r18" + if (keyword.isNotBlank()) "&keyword=$keyword&num=100" else "").ignoreContentType(
true
).timeout(
10_0000 10_0000
).get().body().text() ).get().body().text()
) )
val url: String val url: String
val pid: String val pid: String
with(result.getJSONArray("data").getJSONObject(0)) { val data = result.getJSONArray("data")
with(JSONObject(data.getJSONObject(Random.nextInt(0, data.size)))) {
url = this.getString("url") url = this.getString("url")
pid = this.getString("pid") pid = this.getString("pid")
} }
......
...@@ -18,22 +18,20 @@ private const val IMAGE_BUFFER_CAPACITY: Int = 5 ...@@ -18,22 +18,20 @@ private const val IMAGE_BUFFER_CAPACITY: Int = 5
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
@ExperimentalCoroutinesApi @ExperimentalCoroutinesApi
object Gentlemen : MutableMap<Long, Gentleman> by mutableMapOf() { object Gentlemen : MutableMap<Long, Gentleman> by mutableMapOf() {
fun provide(key: Contact): Gentleman = this.getOrPut(key.id) { Gentleman(key) } fun provide(key: Contact, keyword: String = ""): Gentleman = this.getOrPut(key.id) { Gentleman(key, keyword) }
} }
/** /**
* 工作是缓存图片 * 工作是缓存图片
*/ */
@ExperimentalCoroutinesApi @ExperimentalCoroutinesApi
class Gentleman(private val contact: Contact) : Channel<GentleImage> by Channel(IMAGE_BUFFER_CAPACITY) { class Gentleman(private val contact: Contact, private val keyword: String) : Channel<GentleImage> by Channel(IMAGE_BUFFER_CAPACITY) {
init { init {
GlobalScope.launch { GlobalScope.launch {
while (!isClosedForSend) { while (!isClosedForSend) {
send(GentleImage().apply { send(GentleImage(contact, keyword).apply {
contact = this@Gentleman.contact seImage// start downloading
image// start downloading
}) })
} }
} }
......
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