Skip to content

Commit

Permalink
弃用cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
Colter23 committed Jun 6, 2021
1 parent 057cc43 commit 0a41874
Show file tree
Hide file tree
Showing 19 changed files with 121 additions and 143 deletions.
23 changes: 1 addition & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
下面是一个配置文件模板
配置文件位于 根目录/config/BilibiliDynamic/config.yml
第一次运行插件会自动生成,也可自己创建
注:**第一行的`admin`和最后一行的`COOKIE`为必填**
注:**第一行的`admin`为必填**
```yml
# 管理 一个控制台 必填!!!!!!!!!!
# 可以是群号或QQ号
Expand Down Expand Up @@ -82,12 +82,6 @@ dynamic:
live:
# 播检测总开关
enable: true
# 直播检测独立api
# 如果你要直播检测效果比较好的话 改为 true
# 不开的话可能有些up的直播检测不到
# 不过打开这个 每次的访问周期会变高
# 可以先不开试一试 如果效果不好再打开
indeApi: false

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

Expand All @@ -113,25 +107,10 @@ BPI:
# 大航海数(暂时没用)
# 参数: 用户id:ruid 直播间id:roomid eg: ruid=487550002&roomid=21811136
guard: 'https://api.live.bilibili.com/xlive/app-room/v2/guardTab/topList?page=1&page_size=1&'
# cookie 必填!!!!!!!!!!!!!!!!
# 放在双引号里,cookie里可能带有单引号
COOKIE: ""
```
以上的 好友、群 的开关不建议开启,避免群友过多添加订阅
可以通过管理群统一设置
<details>
<summary>获取 Cookie</summary>
浏览器打开B站 [BiliBili](https://www.bilibili.com/) 并登陆
注:登陆后最好不要退出登陆
`F12`,打开`开发者工具`,找到`Network 网络`并点击
按`F5`刷新页面,按下图复制Cookie
<img src="docs/img/cookie.png" width="500">

</details>

### 使用
在管理群内发送 `#?` `#help` `#帮助` `#功能` `#菜单` 中的任意一个即可查看命令
```
Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ plugins {
kotlin("jvm") version kotlinVersion
kotlin("plugin.serialization") version kotlinVersion

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

group = "top.colter"
version = "1.0.4"
version = "1.0.6"

repositories {
mavenLocal()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package top.colter.mirai.plugin
package top.colter.miraiplugin

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.utils.info
import top.colter.mirai.plugin.PluginConfig.BPI
import top.colter.mirai.plugin.bean.Dynamic
import top.colter.mirai.plugin.utils.httpGet
import top.colter.miraiplugin.bean.Dynamic
import top.colter.miraiplugin.PluginConfig.BPI
import top.colter.miraiplugin.utils.httpGet
import java.text.SimpleDateFormat

suspend fun check(bot: Bot){
while (true){
if (!(PluginConfig.botState&&(PluginConfig.dynamic["enable"]=="true"||PluginConfig.live["enable"]=="true"))){
if (!(PluginConfig.botState &&(PluginConfig.dynamic["enable"]=="true"|| PluginConfig.live["enable"]=="true"))){
delay(600000)
continue
}
try {
PluginMain.logger.info {"Start testing...开始检测..."}
PluginMain.logger.info {"开始检测..."}

val timestamp = System.currentTimeMillis()
val time = SimpleDateFormat("HHmm").format(timestamp)
Expand All @@ -37,7 +38,7 @@ suspend fun check(bot: Bot){
PluginData.userData.forEach { user ->
//获取动态
delay(delay.random())
val rawDynamicList = httpGet(BPI["dynamic"]+user.uid ,BPI["COOKIE"]!!).getJSONObject("data").getJSONArray("cards")
val rawDynamicList = httpGet(BPI["dynamic"]+user.uid).getJSONObject("data").getJSONArray("cards")
val rawDynamicOne = rawDynamicList.getJSONObject(0)

//动态检测
Expand All @@ -60,26 +61,21 @@ suspend fun check(bot: Bot){

//直播检测
if (user.liveRoom!="0" && PluginConfig.live["enable"]=="true") {
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
}
}
var liveStatus = 0

if (liveStatus == 1 && (user.liveStatus==0||user.liveStatus==2)){
if (PluginConfig.live["indeApi"]!="true"){
delay(shortDelay.random())
delay(shortDelay.random())
try {
if (user.liveRoom!="0"){
roomInfo = httpGet(BPI["liveStatus"] + user.liveRoom).getJSONObject("data").getJSONObject("room_info")
liveStatus = roomInfo.getInteger("live_status")
}
}catch (e:Exception){
PluginMain.logger.error("检测动态失败")
}

if (liveStatus == 1 && (user.liveStatus==0||user.liveStatus==2)){
val dynamic = Dynamic()
dynamic.did = user.liveRoom
dynamic.timestamp = roomInfo.getBigInteger("live_start_time").toLong()
Expand All @@ -96,7 +92,7 @@ suspend fun check(bot: Bot){
}else if(keyframe!=""){
dynamic.pictures?.add(keyframe)
}
sendMessage(bot,user.uid,buildResMessage(dynamic, user))
sendMessage(bot,user.uid, buildResMessage(dynamic, user))
}
user.liveStatus = liveStatus
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
package top.colter.mirai. plugin
package top.colter.miraiplugin

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONArray
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
import top.colter.mirai.plugin.utils.getImageIdByBi
import java.io.File
import top.colter.miraiplugin.bean.Dynamic
import top.colter.miraiplugin.bean.User
import top.colter.miraiplugin.utils.buildImageMessage
import java.lang.Exception
import java.lang.StringBuilder
import java.net.URL
Expand All @@ -40,7 +36,7 @@ suspend fun sendDynamic(bot: Bot, rawDynamic: JSONObject, user: User){
dynamic.type = type
dynamic.contentJson = JSON.parseObject(rawDynamic.getString("card"))

if (PluginConfig.pushMode==0){
if (PluginConfig.pushMode ==0){
try{
dynamic.display = rawDynamic.getJSONObject("display")
}catch (e:Exception){ }
Expand All @@ -54,7 +50,8 @@ suspend fun sendDynamic(bot: Bot, rawDynamic: JSONObject, user: User){
sendMessage(bot, user.uid, resMag)

}catch (e : Exception){
PluginMain.logger.error("发送 " + user.name + "的动态失败!")
PluginMain.logger.error("发送 " + user.name + " 的动态失败!\n"+e.message)
throw Exception("发送 " + user.name + " 的动态失败!\n"+e.message)
}
}

Expand Down Expand Up @@ -97,14 +94,14 @@ fun dynamicFormat(dynamic: Dynamic){
dynamic.pictures?.add(origin.getString("pic"))
}
}
if (PluginConfig.pushMode==0){
if (PluginConfig.pushMode ==0){
try {
val emojiJson = dynamic.display.getJSONObject("emoji_info").getJSONArray("emoji_details")
putEmoji(emojiJson)
putEmoji(dynamic,emojiJson)
} catch (e: Exception) { }
try {
val emojiJson = dynamic.display.getJSONObject("origin").getJSONObject("emoji_info").getJSONArray("emoji_details")
putEmoji(emojiJson)
putEmoji(dynamic,emojiJson)
} catch (e: Exception) { }
}
}
Expand All @@ -116,21 +113,21 @@ fun dynamicFormat(dynamic: Dynamic){
for (pic in card.getJSONObject("item").getJSONArray("pictures")) {
dynamic.pictures?.add((pic as JSONObject).getString("img_src"))
}
if (PluginConfig.pushMode==0){
if (PluginConfig.pushMode ==0){
try {
val emojiJson = dynamic.display.getJSONObject("emoji_info").getJSONArray("emoji_details")
putEmoji(emojiJson)
putEmoji(dynamic,emojiJson)
} catch (e: Exception) { }
}
}
//带表情的文字动态
4 -> {
val card = dynamic.contentJson
content = card.getJSONObject("item").getString("content")
if (PluginConfig.pushMode==0){
if (PluginConfig.pushMode ==0){
try {
val emojiJson = dynamic.display.getJSONObject("emoji_info").getJSONArray("emoji_details")
putEmoji(emojiJson)
putEmoji(dynamic,emojiJson)
} catch (e: Exception) { }
}
}
Expand Down Expand Up @@ -197,20 +194,22 @@ fun dynamicFormat(dynamic: Dynamic){
/**
* 解析emojiJson 获取图片封装进map
*/
fun putEmoji(emojiJson: JSONArray){
fun putEmoji(dynamic:Dynamic, emojiJson: JSONArray){
for (emojiItem in emojiJson){
val em = emojiItem as JSONObject
val emojiName = em.getString("emoji_name")
if (PluginMain.emojiMap[emojiName] == null)
PluginMain.emojiMap[emojiName] = ImageIO.read(URL(em.getString("url")))
if (dynamic.emoji == null){
dynamic.emoji = mutableMapOf()
}
dynamic.emoji?.set(emojiName, ImageIO.read(URL(em.getString("url"))))
}
}

/**
* 构建发送消息链
*/
suspend fun buildResMessage(dynamic: Dynamic, user: User): MessageChain {
return if (PluginConfig.pushMode==0){
return if (PluginConfig.pushMode ==0){
buildImageMassageChain(dynamic,user)
}else{
buildTextMassageChain(dynamic,user)
Expand Down Expand Up @@ -254,7 +253,10 @@ suspend fun buildTextMassageChain(dynamic: Dynamic, user: User):MessageChain{
+sb.toString()
for (img in dynamic.pictures!!){
// +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)
+Image(URL(img).openConnection().getInputStream().uploadAsImage(
PluginMain.bot.getFriendOrGroup(
PluginConfig.admin
)).imageId)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package top.colter.mirai.plugin
package top.colter.miraiplugin

import com.alibaba.fastjson.JSON
import kotlinx.coroutines.delay
import top.colter.mirai.plugin.utils.httpGet
import top.colter.mirai.plugin.PluginConfig.BPI
import top.colter.mirai.plugin.bean.User
import top.colter.mirai.plugin.utils.generateImg
import top.colter.miraiplugin.bean.User
import top.colter.miraiplugin.utils.httpGet
import top.colter.miraiplugin.PluginConfig.BPI
import top.colter.miraiplugin.utils.generateImg
import java.awt.Font
import java.io.BufferedInputStream
import java.io.File
Expand All @@ -15,18 +15,21 @@ suspend fun init(){
PluginMain.logger.info("初始化数据中...")

PluginData.userData.forEach { user ->
delay((1000L..2000L).random())
val rawDynamic = httpGet(BPI["dynamic"]+user.uid ,BPI["COOKIE"]!!).getJSONObject("data").getJSONArray("cards")
delay((500..2000L).random())
val rawDynamic = httpGet(BPI["dynamic"]+user.uid).getJSONObject("data").getJSONArray("cards")

val raw0 = rawDynamic.getJSONObject(0)
val desc = raw0.getJSONObject("desc")
user.dynamicId = desc.getBigInteger("dynamic_id").toString()
user.liveStatus =
try {
raw0.getJSONObject("display").getJSONObject("live_info").getInteger("live_status")
}catch (e:Exception){
0

try {
if (user.liveRoom!="0"){
delay(500)
user.liveStatus = httpGet(BPI["liveStatus"] + user.liveRoom).getJSONObject("data").getJSONObject("room_info").getInteger("live_status")
}
}catch (e:Exception){
user.liveStatus = 0
}

var lastId = "0"
rawDynamic.forEach { item ->
Expand All @@ -36,8 +39,8 @@ suspend fun init(){
}

// 查找记录上一页动态
delay((500L..1500L).random())
val rawDynamic2 = httpGet(BPI["dynamic"]+user.uid+"&offset_dynamic_id="+lastId ,BPI["COOKIE"]!!).getJSONObject("data").getJSONArray("cards")
delay((500L..700L).random())
val rawDynamic2 = httpGet(BPI["dynamic"]+user.uid+"&offset_dynamic_id="+lastId ).getJSONObject("data").getJSONArray("cards")

rawDynamic2.forEach { item ->
val desc = JSON.parseObject(item.toString()).getJSONObject("desc")
Expand All @@ -62,8 +65,7 @@ suspend fun init(){
}

suspend fun initFollowInfo(uid:String, user: User, hex: String): String? {
delay(1000)
val rawDynamic = httpGet(BPI["dynamic"]+uid,BPI["COOKIE"]!!).getJSONObject("data").getJSONArray("cards")
val rawDynamic = httpGet(BPI["dynamic"]+uid).getJSONObject("data").getJSONArray("cards")
rawDynamic.forEach { item ->
val desc = JSON.parseObject(item.toString()).getJSONObject("desc")
PluginMain.historyDynamic.add(desc.getBigInteger("dynamic_id").toString())
Expand All @@ -75,21 +77,25 @@ suspend fun initFollowInfo(uid:String, user: User, hex: String): String? {
user.uid = uid
user.name = name
user.dynamicId = res.getJSONObject("desc").getBigInteger("dynamic_id").toString()
try {
user.liveStatus = res.getJSONObject("display").getJSONObject("live_info").getInteger("live_status")
}catch (e:Exception){
user.liveStatus = 0
}

val face = userProfile.getJSONObject("info").getString("face")
val pendant = userProfile.getJSONObject("pendant").getString("image")

delay(1000)
val liveRoom = httpGet(BPI["liveRoom"]+uid,BPI["COOKIE"]!!).getJSONObject("data").getBigInteger("roomid").toString()
delay(500)
val liveRoom = httpGet(BPI["liveRoom"]+uid,"aaa").getJSONObject("data").getBigInteger("roomid").toString()
user.liveRoom = liveRoom

try {
if (user.liveRoom!="0"){
delay(500)
user.liveStatus = httpGet(BPI["liveStatus"] + user.liveRoom).getJSONObject("data").getJSONObject("room_info").getInteger("live_status")
}
}catch (e:Exception){
user.liveStatus = 0
}

var r :String? = null
if (PluginConfig.pushMode==0){
if (PluginConfig.pushMode ==0){
r = generateImg(uid,name,face,pendant,hex)
}

Expand Down
Loading

0 comments on commit 0a41874

Please sign in to comment.