Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Colter23 committed Jun 7, 2023
1 parent f322383 commit 4b53379
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 65 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = "top.colter"
version = "3.2.8"
version = "3.2.10"

repositories {
mavenLocal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object BiliBiliDynamic : KotlinPlugin(
JvmPluginDescription(
id = "top.colter.bilibili-dynamic-mirai-plugin",
name = "BiliBili Dynamic",
version = "3.2.8",
version = "3.2.10",
) {
author("Colter")
dependsOn("xyz.cssxsh.mirai.plugin.mirai-skia-plugin", ">= 1.1.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const val SEARCH = "https://api.bilibili.com/x/web-interface/search/type"
const val USER_INFO = "https://api.bilibili.com/x/space/acc/info"
const val USER_INFO_WBI = "https://api.bilibili.com/x/space/wbi/acc/info"
const val USER_ID = "https://api.bilibili.com/x/web-interface/nav"
const val SPACE_SEARCH = "https://api.bilibili.com/x/space/arc/search"
const val SPACE_SEARCH = "https://api.bilibili.com/x/space/wbi/arc/search"

// Follow
const val IS_FOLLOW = "https://api.bilibili.com/x/relation"
Expand Down
43 changes: 24 additions & 19 deletions src/main/kotlin/top/colter/mirai/plugin/bilibili/api/General.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import io.ktor.http.*
import top.colter.mirai.plugin.bilibili.client.BiliClient
import top.colter.mirai.plugin.bilibili.data.BiliResult
import top.colter.mirai.plugin.bilibili.data.ShortLinkData
import top.colter.mirai.plugin.bilibili.utils.actionNotify
import top.colter.mirai.plugin.bilibili.utils.bodyParameter
import top.colter.mirai.plugin.bilibili.utils.decode
import top.colter.mirai.plugin.bilibili.utils.md5
import top.colter.mirai.plugin.bilibili.data.WbiImg
import top.colter.mirai.plugin.bilibili.utils.*
import java.time.LocalDate

fun twemoji(code: String) = "$TWEMOJI/$code.png"

Expand Down Expand Up @@ -89,22 +88,28 @@ suspend fun BiliClient.toShortLink(oid: String, shareId: String, shareOrigin: St
}catch (e: Exception) { null }
}

fun getVerifyString(): String {
// https://api.bilibili.com/x/web-interface/nav
//val imgUrl = "https://i0.hdslb.com/bfs/wbi/829e9923f2d9407c8932e9e492a345ff.png"
//val subUrl = "https://i0.hdslb.com/bfs/wbi/349e075a9ac443dfb7679db4f73c379f.png"
//val r = splitUrl(imgUrl) + splitUrl(subUrl)
//val r = "829e9923f2d9407c8932e9e492a345ff349e075a9ac443dfb7679db4f73c379f"
//val array = intArrayOf(46,47,18,2,53,8,23,32,15,50,10,31,58,3,45,35,27,43,5,49,33,9,42,19,29,28,14,39,12,38,41,13,37,48,7,16,24,55,40,61,26,17,0,1,60,51,30,4,22,25,54,21,56,59,6,63,57,62,11,36,20,34,44,52)
//return buildString {
// array.forEach { t ->
// if (t < r.length) {
// append(r[t])
// }
// }
//}.slice(IntRange(0, 31))
var lastWbiTime: LocalDate = LocalDate.now()
var wbiImg: WbiImg? = null
suspend fun getWbiImg(): WbiImg {
val now = LocalDate.now()
if (now.isAfter(lastWbiTime) || wbiImg == null) {
lastWbiTime = now
wbiImg = biliClient.userInfo()?.wbiImg
}
return wbiImg!!
}

return "df39df43c6df3e3e349742c2547a45a0"
suspend fun getVerifyString(): String {
val wi = getWbiImg()
val r = splitUrl(wi.imgUrl) + splitUrl(wi.subUrl)
val array = intArrayOf(46,47,18,2,53,8,23,32,15,50,10,31,58,3,45,35,27,43,5,49,33,9,42,19,29,28,14,39,12,38,41,13,37,48,7,16,24,55,40,61,26,17,0,1,60,51,30,4,22,25,54,21,56,59,6,63,57,62,11,36,20,34,44,52)
return buildString {
array.forEach { t ->
if (t < r.length) {
append(r[t])
}
}
}.slice(IntRange(0, 31))
}

fun splitUrl(url: String): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,24 @@ open class BiliClient : Closeable {
Json {
json
}
BrowserUserAgent()
//BrowserUserAgent()
install(UserAgent) {
agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36"
}
}

suspend inline fun <reified T> get(url: String, crossinline block: HttpRequestBuilder.() -> Unit = {}): T =
useHttpClient<String> {
it.get(url) {
header(HttpHeaders.Cookie, BiliBiliDynamic.cookie.toString())
header(HttpHeaders.Cookie, BiliBiliDynamic.cookie.toString() + "DedeUserID=" + BiliBiliDynamic.uid)
block()
}.body()
}.decode()

suspend inline fun <reified T> post(url: String, crossinline block: HttpRequestBuilder.() -> Unit = {}): T =
useHttpClient<String> {
it.post(url) {
header(HttpHeaders.Cookie, BiliBiliDynamic.cookie.toString())
header(HttpHeaders.Cookie, BiliBiliDynamic.cookie.toString() + "DedeUserID=" + BiliBiliDynamic.uid)
block()
}.body()
}.decode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import net.mamoe.mirai.utils.ExternalResource.Companion.sendAsImageTo
import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource
import top.colter.mirai.plugin.bilibili.*
import top.colter.mirai.plugin.bilibili.BiliBiliDynamic.crossContact
import top.colter.mirai.plugin.bilibili.BiliBiliDynamic.logger
import top.colter.mirai.plugin.bilibili.api.getDynamicDetail
import top.colter.mirai.plugin.bilibili.api.getLive
import top.colter.mirai.plugin.bilibili.api.getUserNewDynamic
Expand Down Expand Up @@ -58,13 +59,13 @@ object DynamicCommand : CompositeCommand(
}
}

@SubCommand("add", "添加", "订阅")
@SubCommand("add", "follow", "添加", "订阅")
suspend fun CommandSender.add(id: String, target: GroupOrContact = GroupOrContact(Contact())) {
if (checkPerm(target)) {
if (pgcRegex.matches(id)) {
sendMessage(PgcService.followPgc(id, target.subject))
} else try {
DynamicService.addSubscribe(id.toLong(), target.subject).let {
DynamicService.addSubscribe(id.toLong(), target.subject, this.subject?.delegate == target.subject).let {
sendMessage(it)
actionNotify(this.subject?.id, name, target.name, "订阅", it)
}
Expand All @@ -74,13 +75,13 @@ object DynamicCommand : CompositeCommand(
}
}

@SubCommand("del", "删除")
@SubCommand("del", "unfollow", "删除")
suspend fun CommandSender.del(id: String, target: GroupOrContact = GroupOrContact(Contact())) {
if (checkPerm(target)) {
if (pgcRegex.matches(id)) {
sendMessage(PgcService.delPgc(id, target.subject))
} else matchUser(id) {
DynamicService.removeSubscribe(it, target.subject)
DynamicService.removeSubscribe(it, target.subject, this.subject?.delegate == target.subject)
}?.let {
sendMessage(it)
actionNotify(this.subject?.id, name, target.name, "取消订阅", it)
Expand Down Expand Up @@ -262,14 +263,19 @@ object DynamicCommand : CompositeCommand(

@SubCommand("search", "s", "搜索")
suspend fun CommandSenderOnMessage<*>.search(did: String) {
val subject = Contact()
val detail = try {
biliClient.getDynamicDetail(did)
val msg = sendMessage("加载中...")
try {
val detail = biliClient.getDynamicDetail(did)
if (detail != null) {
detail.let { d -> BiliBiliDynamic.dynamicChannel.send(DynamicDetail(d, subject?.delegate)) }
} else {
sendMessage("未找到动态")
}
} catch (e: Exception) {
null
sendMessage("获取动态失败 ${e.message}")
logger.error("获取动态失败", e)
}
if (detail != null) subject.sendMessage("加载中...") else subject.sendMessage("未找到动态")
detail?.let { d -> BiliBiliDynamic.dynamicChannel.send(DynamicDetail(d, subject.delegate)) }
msg?.recall()
}

@SubCommand("live", "直播")
Expand All @@ -282,34 +288,48 @@ object DynamicCommand : CompositeCommand(

@SubCommand("new", "最新动态")
suspend fun CommandSenderOnMessage<*>.new(user: String, count: Int = 1) {
val msg = sendMessage("加载中...")
matchUser(user) {
val list = biliClient.getUserNewDynamic(it)?.items?.subList(0, count)
list?.forEach { di ->
BiliBiliDynamic.dynamicChannel.send(DynamicDetail(di, Contact().delegate))
try {
val list = biliClient.getUserNewDynamic(it)?.items?.subList(0, count)
list?.forEach { di ->
BiliBiliDynamic.dynamicChannel.send(DynamicDetail(di, Contact().delegate))
}
if (list.isNullOrEmpty()) "未找到动态" else null
}catch (e: Exception) {
logger.error("获取动态失败", e)
"获取动态失败 ${e.message}"
}
if (!list.isNullOrEmpty()) "加载中..." else "未找到动态"
}?.let { sendMessage(it) }
msg?.recall()
}

@SubCommand("video", "最新视频")
suspend fun CommandSenderOnMessage<*>.newVideo(user: String) {
val msg = sendMessage("加载中...")
matchUser(user) {
biliClient.searchUserVideo(it)?.list?.vlist?.run {
if (isNotEmpty()) {
val video = first()
val type = matchingRegular(video.bvid)
val img = type?.drawGeneral() ?: return
val imgMsg = Contact().uploadImage(img, CacheType.DRAW_SEARCH) ?: return
sendMessage(buildMessageChain {
+imgMsg
if (BiliConfig.linkResolveConfig.returnLink) +PlainText(type.getLink())
})
null
} else {
"未找到视频"
try {
biliClient.searchUserVideo(it)?.list?.vlist?.run {
if (isNotEmpty()) {
val video = first()
val type = matchingRegular(video.bvid)
val img = type?.drawGeneral() ?: return
val imgMsg = Contact().uploadImage(img, CacheType.DRAW_SEARCH) ?: return
sendMessage(buildMessageChain {
+imgMsg
if (BiliConfig.linkResolveConfig.returnLink) +PlainText(type.getLink())
})
null
} else {
"未找到视频"
}
}
}catch (e: Exception) {
logger.error("获取动态失败", e)
"获取动态失败 ${e.message}"
}
}?.let { sendMessage(it) }
msg?.recall()
}

@SubCommand("create", "创建分组")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ data class BiliCookie(
fun isEmpty(): Boolean = sessData == "" && biliJct == ""

override fun toString(): String {
return "SESSDATA=$sessData; bili_jct=$biliJct"
return "SESSDATA=$sessData; bili_jct=$biliJct; "
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/top/colter/mirai/plugin/bilibili/data/BiliUser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ data class BiliUser(
val vip: ModuleAuthor.Vip? = null,
@SerialName("pendant")
val pendant: ModuleAuthor.Pendant? = null,
@SerialName("wbi_img")
val wbiImg: WbiImg? = null,

//@SerialName("sex")
//val sex: String,
//@SerialName("face_nft")
Expand Down Expand Up @@ -120,6 +123,14 @@ data class BiliUser(

}

@Serializable
data class WbiImg(
@SerialName("img_url")
val imgUrl: String,
@SerialName("sub_url")
val subUrl: String,
)

@Serializable
data class FansMedal(
@SerialName("show")
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/top/colter/mirai/plugin/bilibili/data/Dynamic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ data class DynamicItem(
val idStr: String?,
@SerialName("visible")
val visible: Boolean = true,
@SerialName("basic")
val basic: DynamicBasic? = null,
//@SerialName("basic")
//val basic: DynamicBasic? = null,
@SerialName("modules")
val modules: Modules,
@SerialName("orig")
Expand Down Expand Up @@ -118,7 +118,7 @@ data class DynamicItem(
@Serializable
data class LikeIcon(
@SerialName("id")
val id: Int = 0,
val id: Long = 0,
@SerialName("action_url")
val actionUrl: String = "",
@SerialName("start_url")
Expand Down Expand Up @@ -243,7 +243,7 @@ data class ModuleAuthor(
@Serializable
data class Pendant(
@SerialName("pid")
val pid: Int,
val pid: Long,
@SerialName("name")
val name: String,
@SerialName("expire")
Expand Down Expand Up @@ -903,7 +903,7 @@ data class ModuleDynamic(
@SerialName("cover")
val cover: String,
@SerialName("desc")
val desc: String,
val desc: String? = null,
//@SerialName("disable_preview")
//val disablePreview: Boolean,
@SerialName("duration_text")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ suspend fun ModuleDynamic.Major.Archive.drawGeneral(showStat: Boolean = false):
}

val descParagraph =
ParagraphBuilder(paragraphStyle, FontUtils.fonts).addText(desc.replace("\r\n", " ").replace("\n", " ")).build()
ParagraphBuilder(paragraphStyle, FontUtils.fonts).addText((desc?:"").replace("\r\n", " ").replace("\n", " ")).build()
.layout(paragraphWidth)

val fallbackUrl = imgApi(cover, cardContentRect.width.toInt(), (cardContentRect.width * 0.625).toInt())
Expand Down Expand Up @@ -332,7 +332,7 @@ suspend fun ModuleDynamic.Major.Pgc.drawSmall(): Image {

suspend fun drawSmallCard(
title: String,
desc: String,
desc: String?,
cover: String,
lbadge: String,
rbadge: String,
Expand All @@ -355,7 +355,7 @@ suspend fun drawSmallCard(
textStyle = descTextStyle
}

val descParagraph = ParagraphBuilder(paragraphStyle, FontUtils.fonts).addText(desc).build().layout(paragraphWidth)
val descParagraph = ParagraphBuilder(paragraphStyle, FontUtils.fonts).addText(desc?:"").build().layout(paragraphWidth)

val videoCardRect = RRect.makeComplexXYWH(
quality.cardPadding.toFloat(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ object DynamicService {
"设置完成"
}

suspend fun addSubscribe(uid: Long, subject: String) = mutex.withLock {
suspend fun addSubscribe(uid: Long, subject: String, isSelf: Boolean = true) = mutex.withLock {
if (isFollow(uid, subject)) return@withLock "之前订阅过这个人哦"
if (dynamic[0]?.contacts?.contains(subject) == true) dynamic[0]?.contacts?.remove(subject)

Expand All @@ -80,10 +80,13 @@ object DynamicService {
// add(subject)
//}
dynamic[uid]?.contacts?.add(subject)
"订阅 ${dynamic[uid]?.name} 成功!"
val contact = findContact(subject)
if (isSelf) "订阅 ${dynamic[uid]?.name} 成功!"
else "${if (contact is Group) "" else "好友"} ${contact?.id} 订阅 ${dynamic[uid]?.name} 成功!"

}

suspend fun removeSubscribe(uid: Long, subject: String) = mutex.withLock {
suspend fun removeSubscribe(uid: Long, subject: String, isSelf: Boolean = true) = mutex.withLock {
if (!isFollow(uid, subject)) return@withLock "还未订阅此人哦"
val user = dynamic[uid]!!
if (user.contacts.remove(subject)) {
Expand All @@ -96,7 +99,9 @@ object DynamicService {
remove(uid)
isEmpty()
} == true) atAll.remove(subject)
"${user.name} 取消订阅成功"
val contact = findContact(subject)
if (isSelf) "取消订阅 ${user.name} 成功"
else "${if (contact is Group) "" else "好友"} ${contact?.id} 取消订阅 ${user.name} 成功"
}else "取消订阅失败"
}

Expand Down

0 comments on commit 4b53379

Please sign in to comment.