Skip to content

Commit

Permalink
修直播检测 加视频模式 支持专栏动态
Browse files Browse the repository at this point in the history
  • Loading branch information
Colter23 committed May 1, 2021
1 parent 20c12f4 commit 1a0e519
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 34 deletions.
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
本人菜鸡一个,代码写的不好(
可能不是很稳定
### 样式
### 图片模式样式
<img src="docs/img/demo1.jpg" width="400" alt="样式">

### 下载
Expand All @@ -14,18 +14,19 @@
下面是一个配置文件模板
配置文件位于 根目录/config/BilibiliDynamic/config.yml
第一次运行插件会自动生成,也可自己创建
注:**第一行的`adminGroup`和最后一行的`COOKIE`为必填**
注:**第一行的`admin`和最后一行的`COOKIE`为必填**
```yml
# 管理 一个控制台 必填!!!!!!!!!!
# 可以是群 也可以是好友,好友的话只能通过私聊控制
# 可以是群号或QQ号
admin: 11111111111

# 是否开启报错推送
exception: true

# 推送模式
# 0 :图片推送(默认) 1 :文字推送
pushMode: 0
# 0 :图片推送 (不稳定)
# 1 :文字推送 (默认)
pushMode: 1

# bot状态
botState: true
Expand Down Expand Up @@ -71,17 +72,26 @@ dynamic:
# 慢速模式访问api的间隔会再上面的间隔加上10s
# 例:200..800就是凌晨2点到8点
lowSpeed: '200-800'
# 视频模式 此模式仅会推送视频 要开启改为 true
videoMode: false
# 是否保存动态图片 保存路径为上面的 basePath
# 仅图片推送模式
saveDynamicImage: true

## 直播相关
live:
# 播检测总开关
enable: true
# 直播检测独立api
# 如果你要直播检测效果比较好的话 改为 true
# 不开的话可能有些up的直播检测不到
# 不过打开这个 每次的访问周期会变高
# 可以先不开试一试 如果效果不好再打开
indeApi: false

### 以上的 friend group dynamic live 的开关均可通过管理群回复 #管理 进行设置

## 百度翻译 (一些VTB的动态可能为日语或英语)
## 百度翻译
baiduTranslate:
# 是否开启百度翻译
enable: false
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
kotlin("jvm") version kotlinVersion
kotlin("plugin.serialization") version kotlinVersion

id("net.mamoe.mirai-console") version "2.6.1"
id("net.mamoe.mirai-console") version "2.6.2"
}

group = "top.colter"
Expand Down
21 changes: 15 additions & 6 deletions src/main/kotlin/Check.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package top.colter.mirai.plugin
import com.alibaba.fastjson.JSONObject
import kotlinx.coroutines.delay
import net.mamoe.mirai.Bot
import net.mamoe.mirai.console.plugin.info
import net.mamoe.mirai.console.util.ContactUtils.getFriendOrGroup
import net.mamoe.mirai.utils.info
import top.colter.mirai.plugin.PluginConfig.BPI
Expand All @@ -23,7 +22,7 @@ suspend fun check(bot: Bot){
val time = SimpleDateFormat("HHmm").format(timestamp)

val interval = PluginConfig.dynamic["interval"]!!.toLong()
val shortDelay = 1000L..4000L
val shortDelay = 1000L..3000L
val middleDelay = interval*1000..(interval+5)*1000
val longDelay = (interval+10)*1000..(interval+15)*1000
var delay = middleDelay
Expand Down Expand Up @@ -61,15 +60,25 @@ suspend fun check(bot: Bot){

//直播检测
if (PluginConfig.live["enable"]=="true") {
val liveStatus =
try {
var liveStatus = 0
var roomInfo = JSONObject()
if (PluginConfig.live["indeApi"]=="true"){
delay(shortDelay.random())
roomInfo = httpGet(BPI["liveStatus"] + user.liveRoom).getJSONObject("data").getJSONObject("room_info")
liveStatus = roomInfo.getInteger("live_status")
}else{
liveStatus = try {
rawDynamicOne.getJSONObject("display").getJSONObject("live_info").getInteger("live_status")
}catch (e:Exception){
0
}
}

if (liveStatus == 1 && (user.liveStatus==0||user.liveStatus==2)){
delay(shortDelay.random())
val roomInfo = httpGet(BPI["liveStatus"] + user.liveRoom).getJSONObject("data").getJSONObject("room_info")
if (PluginConfig.live["indeApi"]!="true"){
delay(shortDelay.random())
roomInfo = httpGet(BPI["liveStatus"] + user.liveRoom).getJSONObject("data").getJSONObject("room_info")
}

val dynamic = Dynamic()
dynamic.did = user.liveRoom
Expand Down
35 changes: 29 additions & 6 deletions src/main/kotlin/DynamicHandle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import com.alibaba.fastjson.JSONObject
import kotlinx.coroutines.delay
import net.mamoe.mirai.Bot
import net.mamoe.mirai.console.util.ContactUtils.getFriendOrGroup
import net.mamoe.mirai.message.data.FileMessage
import net.mamoe.mirai.message.data.Image
import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.message.data.buildMessageChain
import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource
import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsImage
import top.colter.mirai.plugin.bean.Dynamic
import top.colter.mirai.plugin.bean.User
import top.colter.mirai.plugin.utils.buildImageMessage
Expand All @@ -26,13 +28,16 @@ import javax.imageio.ImageIO
suspend fun sendDynamic(bot: Bot, rawDynamic: JSONObject, user: User){
try {
val desc = rawDynamic.getJSONObject("desc")
val dynamicId = desc.getBigInteger("dynamic_id").toString()
val type = desc.getInteger("type")
if (PluginConfig.dynamic["videoMode"]=="true" && type != 8){
return
}

// 封装动态
val dynamic = Dynamic()
dynamic.did = dynamicId
dynamic.did = desc.getBigInteger("dynamic_id").toString()
dynamic.timestamp = desc.getBigInteger("timestamp").toLong()
dynamic.type = desc.getInteger("type")
dynamic.type = type
dynamic.contentJson = JSON.parseObject(rawDynamic.getString("card"))

if (PluginConfig.pushMode==0){
Expand Down Expand Up @@ -133,17 +138,33 @@ fun dynamicFormat(dynamic: Dynamic){
//视频更新动态
8 -> {
val card = dynamic.contentJson
content += "视频: ${card.getString("title")}"
val dt = card.getString("dynamic")
val av = card.getString("aid")
if (dt!=""){
content += dt+"\n"
content += "\n\n"+dt+"\n"
}
content += "视频: ${card.getString("title")}"
dynamic.pictures = mutableListOf()
dynamic.pictures?.add(card.getString("pic"))
dynamic.info = "视频ID:av${av}"
dynamic.link = "https://www.bilibili.com/video/av${av}"
}

//专栏
64 -> {
val card = dynamic.contentJson
content += "专栏: ${card.getString("title")}\n"
content += card.getString("summary")
val cv = card.getString("id")
val banner = card.getString("banner_url")
if (banner != ""){
dynamic.pictures = mutableListOf()
dynamic.pictures?.add(banner)
}
dynamic.info = "专栏ID:${cv}"
dynamic.link = "https://www.bilibili.com/read/cv${cv}"
}

//音频
256 -> {
val card = dynamic.contentJson
Expand Down Expand Up @@ -232,10 +253,12 @@ suspend fun buildTextMassageChain(dynamic: Dynamic, user: User):MessageChain{
return buildMessageChain{
+sb.toString()
for (img in dynamic.pictures!!){
+Image(PluginMain.bot.getFriendOrGroup(PluginConfig.admin).uploadImage(URL(img).openConnection().getInputStream().toExternalResource()).imageId)
// +Image(PluginMain.bot.getFriendOrGroup(PluginConfig.admin).uploadImage(URL(img).openConnection().getInputStream().toExternalResource()).imageId)
+Image(URL(img).openConnection().getInputStream().uploadAsImage(PluginMain.bot.getFriendOrGroup(PluginConfig.admin)).imageId)
}
}
}

return buildMessageChain{
+sb.toString()
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/Listener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import net.mamoe.mirai.event.EventHandler
import net.mamoe.mirai.event.ListenerHost
import net.mamoe.mirai.event.events.*
import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.utils.ExternalResource.Companion.sendAsImageTo
import net.mamoe.mirai.utils.ExternalResource.Companion.sendTo
import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsImage
import top.colter.mirai.plugin.bean.Command
import top.colter.mirai.plugin.bean.User
import java.io.File

val emoji = listOf<String>("( •̀ ω •́ )✧","(o゚v゚)ノ","(o゜▽゜)o☆","( ̄▽ ̄)\"","(。・ω・)ノ",
"(੭ˊ꒳ˋ)੭✧","⸜(* ॑꒳ ॑* )⸝✩°。⋆","( •̀ ω •́ )y","( ̄3 ̄)","(ง •_•)ง","o(〃^▽^〃)o",
Expand Down
16 changes: 12 additions & 4 deletions src/main/kotlin/PluginConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import net.mamoe.mirai.console.data.value

object PluginConfig : AutoSavePluginConfig("config") {

// 管理,报错都会发送此 必填!!!!!!!!!!
// 管理 必填!!!!!!!!!!
var admin : Long by value()
// 是否开启报错推送
var exception by value(true)
// 推送模式
// 0 :图片推送(默认) 1 : 文字推送
var pushMode by value(0)
// 0 :图片推送 1 : 文字推送 (默认)
var pushMode by value(1)

// bot状态
var botState by value(true)
Expand Down Expand Up @@ -51,14 +51,22 @@ object PluginConfig : AutoSavePluginConfig("config") {
//慢速模式开启时间段 不开启则填000-000
//例:200..800就是凌晨2点到8点
"lowSpeed" to "200-800",
//视频模式 此模式仅会推送视频
"videoMode" to "false",
//是否保存动态图片
"saveDynamicImage" to "true"
))

//---------------直播检测----------------//
var live : MutableMap<String,String> by value(mutableMapOf(
//直播检测总开关
"enable" to "true"
"enable" to "true",
//直播检测独立的api
//如果你要直播检测效果比较好的话 改为 true
//不开的话可能有些up的直播检测不到
//不过打开这个 每次的访问周期会变高
//可以先不开试一试 如果效果不好再打开
"indeApi" to "false"
))

//---------------百度翻译----------------//
Expand Down
15 changes: 11 additions & 4 deletions src/main/kotlin/PluginMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,18 @@ object PluginMain : KotlinPlugin(
logger.info("forward......")

init()
delay(2000)

Bot.instances.forEach { b: Bot ->
bot = b
while (true){
try {
bot = Bot.instances[0]
bot
break
}catch (e:Exception){
delay(1000)
}
}
delay(1000)
logger.info("工作Bot:"+bot.id)

bot.eventChannel.registerListenerHost(NewFriendRequestListener)
bot.eventChannel.registerListenerHost(MemberJoinListener)
bot.eventChannel.registerListenerHost(MessageListener)
Expand Down
14 changes: 7 additions & 7 deletions src/main/kotlin/bean/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package top.colter.mirai.plugin.bean
import kotlinx.serialization.Serializable

@Serializable
class User {
data class User (
// 名用户名
var name = ""
var name: String="",
// 用户ID
var uid = ""
var uid: String="",
// 动态ID
var dynamicId = ""
var dynamicId: String="",
// 直播间号
var liveRoom = ""
var liveRoom: String="",
// 直播间状态
var liveStatus = 0
}
var liveStatus: Int=0
)

0 comments on commit 1a0e519

Please sign in to comment.