From b81e60287efaa6f84327f44cb02d1af154358fba Mon Sep 17 00:00:00 2001 From: Possible <794329728@qq.com> Date: Fri, 28 Oct 2022 16:56:51 +0800 Subject: [PATCH 1/6] polish --- plugin/PolishMedal/PolishMedal.php | 192 +++++++++++++++++++++++++++++ src/Api/Msg/ApiMsg.php | 73 +++++++++++ src/Api/Room/V1/ApiInfo.php | 53 ++++++++ 3 files changed, 318 insertions(+) create mode 100644 plugin/PolishMedal/PolishMedal.php create mode 100644 src/Api/Msg/ApiMsg.php create mode 100644 src/Api/Room/V1/ApiInfo.php diff --git a/plugin/PolishMedal/PolishMedal.php b/plugin/PolishMedal/PolishMedal.php new file mode 100644 index 0000000..50a490e --- /dev/null +++ b/plugin/PolishMedal/PolishMedal.php @@ -0,0 +1,192 @@ + __CLASS__, // hook + 'name' => 'PolishMedal', // 插件名称 + 'version' => '0.0.1', // 插件版本 + 'desc' => '点亮徽章', // 插件描述 + 'author' => 'Lkeme',// 作者 + 'priority' => 9999, // 插件优先级 + 'cycle' => '1(小时)', // 运行周期 + ]; + + private static array $grey_fans_medals = []; // 灰色勋章 + private static int $metal_lock = 0; // 勋章时间锁 + + /** + * @param Plugin $plugin + */ + public function __construct(Plugin &$plugin) + { + // 时间锁 + TimeLock::initTimeLock(); + // 缓存 + // Cache::initCache(); + // $this::class + $plugin->register($this, 'execute'); + } + + /** + * 执行 + * @return void + */ + public function execute(): void + { + if (TimeLock::getTimes() > time()) return; + + if (self::$metal_lock < time()) { + // 如果勋章过多导致未处理完,就1小时一次,否则10小时一次。 + if (empty(self::$grey_fans_medals)) { + // 处理每日 + if (getConf('everyday', 'polish_the_medal')) { + // 如果是 直接定时到第二天7点 + self::fetchGreyMedalList(true); + self::$metal_lock = time() + TimeLock::timing(7, 0, 0, true); + } else { + // 否则按正常逻辑 + self::fetchGreyMedalList(); + self::$metal_lock = time() + 10 * 60 * 60; + } + } else { + self::$metal_lock = time() + 60 * 60; + } + } + // 点亮灰色勋章 + if (TimeLock::getTimes() < time()) { + // 随机4-10分钟处理一次点亮操作 + self::polishTheMedal(); + TimeLock::setTimes(mt_rand(4, 10) * 60); + } + TimeLock::setTimes(10 * 60 * 60); + } + + /** + * 获取徽章列表 + * @return array + */ + private static function fetchMedalList(): array + { + $medalList = []; + for ($i = 1; $i <= 100; $i++) { + $de_raw = ApiFansMedal::panel($i, 50); + if (isset($de_raw['code']) && $de_raw['code']) { + Log::warning("获取徽章列表失败 => {$de_raw['message']}"); + } + $keys = ['list', 'special_list']; + foreach ($keys as $key) { + if (isset($de_raw['data'][$key])) { + foreach ($de_raw['data'][$key] as $vo) { + // 部分主站勋章没有直播间 + if (isset($vo['room_info']['room_id'])) { + $vo['medal']['roomid'] = $vo['room_info']['room_id']; + } else { + $vo['medal']['roomid'] = 0; + } + $medalList[] = $vo['medal']; + } + } + } + // total_number || count == 0 + if (count($medalList) >= $de_raw['data']['total_number'] || empty($medalList)) { + break; + } + } + // count == 0 + if (!empty($medalList)) { + $num = count($medalList); + Log::info("勋章列表获取成功, 共获取到 $num 个!"); + } + return $medalList; + } + + + /** + * 获取熄灭徽章 + * @param bool $all + */ + private static function fetchGreyMedalList(bool $all = false) + { + $data = self::fetchMedalList(); + foreach ($data as $vo) { + // 过滤主站勋章 + if (!isset($vo['roomid']) || $vo['roomid'] == 0) continue; + + // 如果是每天擦亮 ,就不过滤|否则过滤掉,只点亮灰色 + if ($all) { + self::$grey_fans_medals[] = [ + 'uid' => $vo['target_id'], + 'roomid' => $vo['roomid'], + ]; + } else { + // 灰色 + if ($vo['medal_color_start'] == 12632256 && $vo['medal_color_end'] == 12632256 && $vo['medal_color_border'] == 12632256) { + self::$grey_fans_medals[] = [ + 'uid' => $vo['target_id'], + 'roomid' => $vo['roomid'], + ]; + } + } + } + // 乱序 + shuffle(self::$grey_fans_medals); + } + + + /** + * 点亮徽章 + * @return void + */ + private static function polishTheMedal(): void + { + $medal = array_pop(self::$grey_fans_medals); + // 为空 + if (is_null($medal)) return; + // 特殊房间处理|央视未开播|CODE -> 11000 MSG -> '' + if (in_array($medal['roomid'], [21686237, 0])) return; + + Log::info("开始点亮直播间@{$medal['roomid']}的勋章"); + // 擦亮 +// $room_id = self::getRealRoomID($room_id); +// if (!$room_id) { +// return ['code' => 404, 'message' => '直播间数据异常']; +// } + + $res = ApiMsg::sendBarrageAPP($medal['roomid'], Fake::emoji()); + if (isset($res['code']) && $res['code'] == 0) { + Log::notice("在直播间@{$medal['roomid']}发送点亮弹幕成功"); + } else { + Log::warning("在直播间@{$medal['roomid']}发送点亮弹幕失败, CODE -> {$res['code']} MSG -> {$res['message']} "); + } + } + +} diff --git a/src/Api/Msg/ApiMsg.php b/src/Api/Msg/ApiMsg.php new file mode 100644 index 0000000..d79c6d2 --- /dev/null +++ b/src/Api/Msg/ApiMsg.php @@ -0,0 +1,73 @@ + '16777215', + 'fontsize' => 25, + 'mode' => 1, + 'msg' => $content, + 'rnd' => 0, + 'bubble' => 0, + 'roomid' => $room_id, + 'csrf' => $user['csrf'], + 'csrf_token' => $user['csrf'], + ]; + + + $headers = [ + 'origin' => 'https://live.bilibili.com', + 'referer' => "https://live.bilibili.com/$room_id" + ]; + return Request::postJson(true, 'pc', $url, $payload, $headers); + } + + public static function sendBarrageAPP(int $room_id, string $content): array + { + $user = User::parseCookie(); + $url = 'https://api.live.bilibili.com/msg/send'; + $payload = [ + 'color' => '16777215', + 'fontsize' => 25, + 'mode' => 1, + 'msg' => $content, + 'rnd' => 0, + 'roomid' => $room_id, + 'csrf' => $user['csrf'], + 'csrf_token' => $user['csrf'], + ]; + return Request::postJson(true, 'other', $url, $payload); + } +} + diff --git a/src/Api/Room/V1/ApiInfo.php b/src/Api/Room/V1/ApiInfo.php new file mode 100644 index 0000000..0767dcb --- /dev/null +++ b/src/Api/Room/V1/ApiInfo.php @@ -0,0 +1,53 @@ + $room_id + ]; + return Request::getJson(true, 'other', $url, $payload); + } + + /** + * 获取直播间信息 + * @param $room_id + * @return array + */ + public static function getRoomInfoV2($room_id): array + { + $url = ' https://api.live.bilibili.com/room/v1/Room/get_info_by_id'; + $payload = [ + 'ids[]' => $room_id + ]; + return Request::getJson(true, 'other', $url, $payload); + } + +} From 08e4618f8e0d551eef6e25844a5ed8313192460c Mon Sep 17 00:00:00 2001 From: Possible <794329728@qq.com> Date: Wed, 21 Dec 2022 16:04:04 +0800 Subject: [PATCH 2/6] =?UTF-8?q?:sparkles:=20=E8=BF=81=E7=A7=BB=E5=BE=BD?= =?UTF-8?q?=E7=AB=A0=E7=82=B9=E4=BA=AE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/PolishMedal/PolishMedal.php | 17 +++++------------ src/Api/Msg/ApiMsg.php | 2 +- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/plugin/PolishMedal/PolishMedal.php b/plugin/PolishMedal/PolishMedal.php index 50a490e..b66f316 100644 --- a/plugin/PolishMedal/PolishMedal.php +++ b/plugin/PolishMedal/PolishMedal.php @@ -36,7 +36,7 @@ class PolishMedal extends BasePlugin 'version' => '0.0.1', // 插件版本 'desc' => '点亮徽章', // 插件描述 'author' => 'Lkeme',// 作者 - 'priority' => 9999, // 插件优先级 + 'priority' => 1115, // 插件优先级 'cycle' => '1(小时)', // 运行周期 ]; @@ -51,8 +51,6 @@ class PolishMedal extends BasePlugin // 时间锁 TimeLock::initTimeLock(); // 缓存 - // Cache::initCache(); - // $this::class $plugin->register($this, 'execute'); } @@ -62,23 +60,23 @@ class PolishMedal extends BasePlugin */ public function execute(): void { - if (TimeLock::getTimes() > time()) return; + if (TimeLock::getTimes() > time() || !getEnable('polishMedal')) return; if (self::$metal_lock < time()) { // 如果勋章过多导致未处理完,就1小时一次,否则10小时一次。 if (empty(self::$grey_fans_medals)) { // 处理每日 - if (getConf('everyday', 'polish_the_medal')) { + if (getConf('polishMedal.everyday', false, 'bool')) { // 如果是 直接定时到第二天7点 self::fetchGreyMedalList(true); self::$metal_lock = time() + TimeLock::timing(7, 0, 0, true); } else { // 否则按正常逻辑 self::fetchGreyMedalList(); - self::$metal_lock = time() + 10 * 60 * 60; + self::$metal_lock = time() + 10 * 60 * 60; // 10小时 } } else { - self::$metal_lock = time() + 60 * 60; + self::$metal_lock = time() + 60 * 60; // 1小时一次 } } // 点亮灰色勋章 @@ -176,11 +174,6 @@ class PolishMedal extends BasePlugin Log::info("开始点亮直播间@{$medal['roomid']}的勋章"); // 擦亮 -// $room_id = self::getRealRoomID($room_id); -// if (!$room_id) { -// return ['code' => 404, 'message' => '直播间数据异常']; -// } - $res = ApiMsg::sendBarrageAPP($medal['roomid'], Fake::emoji()); if (isset($res['code']) && $res['code'] == 0) { Log::notice("在直播间@{$medal['roomid']}发送点亮弹幕成功"); diff --git a/src/Api/Msg/ApiMsg.php b/src/Api/Msg/ApiMsg.php index d79c6d2..ac014df 100644 --- a/src/Api/Msg/ApiMsg.php +++ b/src/Api/Msg/ApiMsg.php @@ -67,7 +67,7 @@ class ApiMsg 'csrf' => $user['csrf'], 'csrf_token' => $user['csrf'], ]; - return Request::postJson(true, 'other', $url, $payload); + return Request::postJson(true, 'app', $url, Sign::common($payload)); } } From be91ae740841b0bb4792a9686bd90506b3d8d105 Mon Sep 17 00:00:00 2001 From: Possible <794329728@qq.com> Date: Wed, 21 Dec 2022 16:09:55 +0800 Subject: [PATCH 3/6] =?UTF-8?q?:sparkles:=20=E6=B7=BB=E5=8A=A0=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- profile/example/config/user.ini | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/profile/example/config/user.ini b/profile/example/config/user.ini index 4a75df2..acb40aa 100644 --- a/profile/example/config/user.ini +++ b/profile/example/config/user.ini @@ -117,6 +117,12 @@ enable = false target_up_id = 11153765 target_room_id = 23058 + +; 擦亮徽章 +[polishMedal] +enable = true +everyday = false + ####################### # 通知设置 # ####################### From 3d4afe0904963536cdd60265cfb635668f3adced Mon Sep 17 00:00:00 2001 From: Possible <794329728@qq.com> Date: Wed, 21 Dec 2022 16:10:50 +0800 Subject: [PATCH 4/6] =?UTF-8?q?:sparkles:=20=E4=BF=AE=E6=94=B9=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=90=8D=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/PolishMedal/PolishMedal.php | 4 ++-- profile/example/config/user.ini | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin/PolishMedal/PolishMedal.php b/plugin/PolishMedal/PolishMedal.php index b66f316..678bed9 100644 --- a/plugin/PolishMedal/PolishMedal.php +++ b/plugin/PolishMedal/PolishMedal.php @@ -60,13 +60,13 @@ class PolishMedal extends BasePlugin */ public function execute(): void { - if (TimeLock::getTimes() > time() || !getEnable('polishMedal')) return; + if (TimeLock::getTimes() > time() || !getEnable('polish_medal')) return; if (self::$metal_lock < time()) { // 如果勋章过多导致未处理完,就1小时一次,否则10小时一次。 if (empty(self::$grey_fans_medals)) { // 处理每日 - if (getConf('polishMedal.everyday', false, 'bool')) { + if (getConf('polish_medal.everyday', false, 'bool')) { // 如果是 直接定时到第二天7点 self::fetchGreyMedalList(true); self::$metal_lock = time() + TimeLock::timing(7, 0, 0, true); diff --git a/profile/example/config/user.ini b/profile/example/config/user.ini index acb40aa..6d762ce 100644 --- a/profile/example/config/user.ini +++ b/profile/example/config/user.ini @@ -119,7 +119,7 @@ target_room_id = 23058 ; 擦亮徽章 -[polishMedal] +[polish_medal] enable = true everyday = false From 978be415876a6607413a90acd6bd0a1b8b2f1804 Mon Sep 17 00:00:00 2001 From: Possible <794329728@qq.com> Date: Wed, 21 Dec 2022 16:14:13 +0800 Subject: [PATCH 5/6] =?UTF-8?q?:sparkles:=20=E6=9A=82=E5=81=9C=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/PolishMedal/PolishMedal.php | 1 - 1 file changed, 1 deletion(-) diff --git a/plugin/PolishMedal/PolishMedal.php b/plugin/PolishMedal/PolishMedal.php index 678bed9..7530e78 100644 --- a/plugin/PolishMedal/PolishMedal.php +++ b/plugin/PolishMedal/PolishMedal.php @@ -85,7 +85,6 @@ class PolishMedal extends BasePlugin self::polishTheMedal(); TimeLock::setTimes(mt_rand(4, 10) * 60); } - TimeLock::setTimes(10 * 60 * 60); } /** From 285a5c9349b192e691fe4e589b3b4767d755fb65 Mon Sep 17 00:00:00 2001 From: Possible <794329728@qq.com> Date: Thu, 22 Dec 2022 09:49:42 +0800 Subject: [PATCH 6/6] =?UTF-8?q?:sparkles:=20=E9=BB=98=E8=AE=A4=E5=85=B3?= =?UTF-8?q?=E9=97=AD.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- profile/example/config/user.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/profile/example/config/user.ini b/profile/example/config/user.ini index 6d762ce..aacf34b 100644 --- a/profile/example/config/user.ini +++ b/profile/example/config/user.ini @@ -118,9 +118,9 @@ target_up_id = 11153765 target_room_id = 23058 -; 擦亮徽章 +; 擦亮徽章/ 每日擦亮 默认擦亮变灰徽章 [polish_medal] -enable = true +enable = false everyday = false #######################