From a4eaa63b6deb41d8f7459f7d39c5e51fad331411 Mon Sep 17 00:00:00 2001 From: lkeme <1462364497@qq.com> Date: Sun, 5 Dec 2021 17:48:31 +0800 Subject: [PATCH] [fix] Backports PHP 8.1 fixes #148 --- .gitignore | 2 - ABOUT.md | 1 + README.md | 43 ++++--- composer.json | 15 ++- conf/bili.yaml | 4 +- src/core/App.php | 1 + src/core/Env.php | 12 +- src/plugin/ActivityLottery.php | 13 +- src/plugin/AloneTcpClient.php | 2 +- src/plugin/AwardRecord.php | 3 + src/plugin/Barrage.php | 3 + src/plugin/BpConsumption.php | 3 + src/plugin/CapsuleLottery.php | 16 +-- src/plugin/CheckUpdate.php | 11 +- src/plugin/Competition.php | 3 +- src/plugin/DailyBag.php | 3 + src/plugin/DailyTask.php | 4 + src/plugin/DoubleHeart.php | 3 + src/plugin/Forward.php | 11 +- src/plugin/GiftHeart.php | 3 + src/plugin/GiftRaffle.php | 2 +- src/plugin/GiftSend.php | 4 +- src/plugin/GroupSignIn.php | 3 + src/plugin/GuardRaffle.php | 2 +- src/plugin/Judge.php | 3 + src/plugin/LiveReservation.php | 10 +- src/plugin/Login.php | 1 + src/plugin/MainSite.php | 6 + src/plugin/ManGa.php | 3 + src/plugin/MaterialObject.php | 3 + src/plugin/PkRaffle.php | 2 +- src/plugin/PolishTheMedal.php | 1 + src/plugin/Schedule.php | 4 +- src/plugin/Silver.php | 3 + src/plugin/Silver2Coin.php | 7 +- src/plugin/SmallHeart.php | 3 + src/plugin/Statistics.php | 7 +- src/plugin/StormRaffle.php | 2 +- src/plugin/VipPrivilege.php | 3 + src/plugin/ZoneTcpClient.php | 2 +- src/script/DelDynamic.php | 3 + src/script/UnFollow.php | 1 + src/tool/ArrayToTextTable.php | 222 +++++++++++++++++++++++++++++++++ src/tool/Path.php | 37 ++++++ src/util/AllotTasks.php | 9 +- src/util/BaseRaffle.php | 3 + tests/ArrayToTextTableTest.php | 48 +++++++ 47 files changed, 465 insertions(+), 85 deletions(-) create mode 100644 ABOUT.md create mode 100644 src/tool/ArrayToTextTable.php create mode 100644 src/tool/Path.php create mode 100644 tests/ArrayToTextTableTest.php diff --git a/.gitignore b/.gitignore index c862858..d302d7c 100644 --- a/.gitignore +++ b/.gitignore @@ -16,7 +16,6 @@ gen /vendor/ composer.lock /configs/ -/tests/ config /conf/user*.ini *.log @@ -29,7 +28,6 @@ conf/user1.ini /conf/test1.ini /src/backup/ script.php -ABOUT.md task/* cache/* diff --git a/ABOUT.md b/ABOUT.md new file mode 100644 index 0000000..50fdb24 --- /dev/null +++ b/ABOUT.md @@ -0,0 +1 @@ +哔哩哔哩(Bilibili)B 站主站助手、直播助手、直播抽奖、挂机升级、贴心小棉袄脚本、Lv6 离你仅有一步之遥~ - PHP 版(Personal) \ No newline at end of file diff --git a/README.md b/README.md index b15bea8..9556590 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@

-## 公告通知 +## 📌公告通知 代码开源,本地化99.9%,项目不收集或使用任何敏感信息,兴趣所致,一切只为学习。 @@ -32,7 +32,13 @@ ---- 只有花大价钱去买到的东西,才会令人信任。 ``` -## 打赏支持 +## 👤游客访问 + +

+ +

+ +## 🎁打赏支持 如果觉得本项目好用,对你有所帮助,欢迎打赏支持本项目哦。 @@ -40,7 +46,23 @@ [comment]: <> (![Image](https://i.loli.net/2019/07/13/5d2963e5cc1eb22973.png)) -## 功能组件 +## 🖨️相关文档 + +有疑问一定要先看看文档或Issue里是否存在相同的问题,再考虑其他渠道咨询。 + +[comment]: <> (:cherry_blossom: :gift: :gift_heart: :confetti_ball:) + +* [使用文档 / DOC.md](./DOC.md) +* [推送文档 / DOC.md](./NOTIFY.md) +* [更新日志 / CHANGELOG.md](./CHANGELOG.md) +* [配置文档 / WIKI.md](https://github.com/lkeme/BiliHelper-personal/wiki/%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E8%AF%A6%E8%A7%A3) +* [常见问题 / WIKI.md](https://github.com/lkeme/BiliHelper-personal/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98) + +## 💬交流反馈 + +Group: [163955623](https://jq.qq.com/?_wv=1027&k=1QMFTUEO) | **请不要来问如何使用, 文档齐全, 仅用于BUG提交反馈** + +## 🧑‍🏭功能组件 以下任务都是按设定周期自动执行,`true`为正常使用,`false`为暂停使用或抛弃。 @@ -83,21 +105,6 @@ | LiveReservation | true | 21.11.15 | 待整理 | 直播预约有奖 | | Silver | false | 21.03.27 | 待整理 | 直播银瓜子自动开启宝箱 | -## 交流反馈 - -Group: [163955623](https://jq.qq.com/?_wv=1027&k=1QMFTUEO) | **请不要来问如何使用, 文档齐全, 仅用于BUG提交反馈** - -## 相关文档 - -有疑问一定要先看看文档或Issue里是否存在相同的问题,再考虑其他渠道咨询。 - -[comment]: <> (:cherry_blossom: :gift: :gift_heart: :confetti_ball:) - -* [使用文档 / DOC.md](./DOC.md) -* [推送文档 / DOC.md](./NOTIFY.md) -* [更新日志 / CHANGELOG.md](./CHANGELOG.md) -* [配置文档 / WIKI.md](https://github.com/lkeme/BiliHelper-personal/wiki/%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E8%AF%A6%E8%A7%A3) -* [常见问题 / WIKI.md](https://github.com/lkeme/BiliHelper-personal/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98) ## 🖥️星图 diff --git a/composer.json b/composer.json index 32fe0a6..9bd6e37 100644 --- a/composer.json +++ b/composer.json @@ -14,17 +14,16 @@ "bramus/monolog-colored-line-formatter": "^2.0.3", "clue/socket-raw": "^1.5.0", "amphp/amp": "^2.5.2", - "guzzlehttp/guzzle": "^6.5.5", - "mathieuviossat/arraytotexttable": "^1.0.8", - "klkvsk/json-decode-stream": "^1.0", - "sven/file-config": "^3.1", - "hassankhan/config": "^2.2", - "lkeme/inifile": "^3.4", + "guzzlehttp/guzzle": "^7.4.0", + "overtrue/pinyin": "~4.0", "adhocore/cli": "^0.9.0", + "lkeme/inifile": "^3.4", + "symfony/yaml": "^6.0", + "sven/file-config": "^3.1", "fire015/flintstone": "^2.3", - "symfony/yaml": "^5.3", "consolidation/config": "^2.0", - "overtrue/pinyin": "~4.0" + "lkeme/data": "dev-master", + "phppkg/config": "^1.1" }, "license": "MIT", "authors": [ diff --git a/conf/bili.yaml b/conf/bili.yaml index c0e924a..8d74b8a 100644 --- a/conf/bili.yaml +++ b/conf/bili.yaml @@ -3,8 +3,8 @@ bili_version: 0.0.1 # Android bili_a: package: "tv.danmaku.bili" - version: "6.48.0" - build: "6480300" + version: "6.51.0" + build: "6510400" channel: "bili" device: "phone" mobi_app: "android" diff --git a/src/core/App.php b/src/core/App.php index c4bdf31..8297a09 100644 --- a/src/core/App.php +++ b/src/core/App.php @@ -27,6 +27,7 @@ class App */ public function __construct(string $app_path) { + define('APP_MICROSECOND', 1000000); define('APP_CONF_PATH', $app_path . '/conf/'); define('APP_DATA_PATH', $app_path . '/data/'); define('APP_LOG_PATH', $app_path . '/log/'); diff --git a/src/core/Env.php b/src/core/Env.php index 3c8651b..e6bf433 100644 --- a/src/core/Env.php +++ b/src/core/Env.php @@ -10,7 +10,7 @@ namespace BiliHelper\Core; -use Noodlehaus\Config; +use function JBZoo\Data\json; class Env { @@ -78,10 +78,10 @@ class Env */ private function loadJsonData() { - $conf = new Config($this->repository); - $this->app_name = $conf->get('project'); - $this->app_version = $conf->get('version'); - $this->app_branch = $conf->get('branch'); - $this->app_source = $conf->get('source'); + $conf = json($this->repository); + $this->app_name = $conf->get('project', 'BiliHelper-personal'); + $this->app_version = $conf->get('version', '0.0.0.000000'); + $this->app_branch = $conf->get('branch', 'master'); + $this->app_source = $conf->get('source', 'https://github.com/lkeme/BiliHelper-personal'); } } diff --git a/src/plugin/ActivityLottery.php b/src/plugin/ActivityLottery.php index 89b8195..6145099 100644 --- a/src/plugin/ActivityLottery.php +++ b/src/plugin/ActivityLottery.php @@ -23,10 +23,7 @@ class ActivityLottery private static string $repository = APP_DATA_PATH . 'activity_infos.json'; /** - * @throws \JsonDecodeStream\Exception\TokenizerException - * @throws \JsonDecodeStream\Exception\SelectorException - * @throws \JsonDecodeStream\Exception\CollectorException - * @throws \JsonDecodeStream\Exception\ParserException + * @use run */ public static function run() { @@ -44,10 +41,6 @@ class ActivityLottery /** * @use 分配任务 * @return bool - * @throws \JsonDecodeStream\Exception\CollectorException - * @throws \JsonDecodeStream\Exception\ParserException - * @throws \JsonDecodeStream\Exception\SelectorException - * @throws \JsonDecodeStream\Exception\TokenizerException */ private static function allotTasks(): bool { @@ -55,7 +48,9 @@ class ActivityLottery return false; } $parser = self::loadJsonData(); - foreach ($parser->items('data[]') as $act) { + foreach ($parser->get('data', []) as $act) { + // todo 优化处理 + $act = json_decode(json_encode($act)); // 活动无效 if (is_null($act->sid)) { continue; diff --git a/src/plugin/AloneTcpClient.php b/src/plugin/AloneTcpClient.php index 094d814..0035f5b 100644 --- a/src/plugin/AloneTcpClient.php +++ b/src/plugin/AloneTcpClient.php @@ -28,7 +28,7 @@ class AloneTcpClient private static int $max_errors_num = 0; // 最大连续错误5次 /** - * @use 入口 + * @use run */ public static function run() { diff --git a/src/plugin/AwardRecord.php b/src/plugin/AwardRecord.php index 6fc89bd..436899f 100644 --- a/src/plugin/AwardRecord.php +++ b/src/plugin/AwardRecord.php @@ -25,6 +25,9 @@ class AwardRecord private static int $gift_lock = 0; private static array $gift_list = []; + /** + * @use run + */ public static function run() { if (self::getLock() > time() || !getEnable('award_record')) { diff --git a/src/plugin/Barrage.php b/src/plugin/Barrage.php index bb692a9..9572e12 100644 --- a/src/plugin/Barrage.php +++ b/src/plugin/Barrage.php @@ -19,6 +19,9 @@ class Barrage { use TimeLock; + /** + * @use run + */ public static function run() { if (self::getLock() > time() || !getEnable('barrage')) { diff --git a/src/plugin/BpConsumption.php b/src/plugin/BpConsumption.php index 34d5a37..2945ef0 100644 --- a/src/plugin/BpConsumption.php +++ b/src/plugin/BpConsumption.php @@ -20,6 +20,9 @@ class BpConsumption use TimeLock; + /** + * @use run + */ public static function run() { if (self::getLock() > time() || !getEnable('bp_consumption')) { diff --git a/src/plugin/CapsuleLottery.php b/src/plugin/CapsuleLottery.php index 7b2888e..4202ad5 100644 --- a/src/plugin/CapsuleLottery.php +++ b/src/plugin/CapsuleLottery.php @@ -29,10 +29,7 @@ class CapsuleLottery /** - * @throws \JsonDecodeStream\Exception\TokenizerException - * @throws \JsonDecodeStream\Exception\SelectorException - * @throws \JsonDecodeStream\Exception\ParserException - * @throws \JsonDecodeStream\Exception\CollectorException + * @use run */ public static function run() { @@ -50,10 +47,6 @@ class CapsuleLottery /** * @use 分配任务 * @return bool - * @throws \JsonDecodeStream\Exception\CollectorException - * @throws \JsonDecodeStream\Exception\ParserException - * @throws \JsonDecodeStream\Exception\SelectorException - * @throws \JsonDecodeStream\Exception\TokenizerException */ private static function allotTasks(): bool { @@ -61,7 +54,10 @@ class CapsuleLottery return false; } $parser = self::loadJsonData(); - foreach ($parser->items('data[]') as $act) { + // Converting to array + foreach ($parser->get('data', []) as $act) { + // todo 优化处理 + $act = json_decode(json_encode($act)); // 活动无效 if (is_null($act->coin_id)) { continue; @@ -248,7 +244,7 @@ class CapsuleLottery Log::info("获取剩余抽奖次数成功 {$capsule_info['data']['coin']}"); return $capsule_info['data']['coin']; } - Log::warning("获取剩余抽奖次数失败 $capsule_info"); + Log::warning("获取剩余抽奖次数失败 " . json_encode($capsule_info, true)); return 0; } diff --git a/src/plugin/CheckUpdate.php b/src/plugin/CheckUpdate.php index fe2ba23..a13a3b5 100644 --- a/src/plugin/CheckUpdate.php +++ b/src/plugin/CheckUpdate.php @@ -10,11 +10,10 @@ namespace BiliHelper\Plugin; -use Noodlehaus\Config; -use Noodlehaus\Parser\Json; use BiliHelper\Core\Log; use BiliHelper\Core\Curl; use BiliHelper\Util\TimeLock; +use function JBZoo\Data\json; class CheckUpdate { @@ -24,7 +23,9 @@ class CheckUpdate private static object $latest_conf; private static string $repository = APP_DATA_PATH . 'latest_version.json'; - + /** + * @use run + */ public static function run() { if (self::getLock() > time()) { @@ -77,7 +78,7 @@ class CheckUpdate $url = self::$current_conf->get('raw_url'); $payload = []; $raw = Curl::get('other', $url, $payload); - self::$latest_conf = Config::load($raw, new Json, true); + self::$latest_conf = json(json_decode($raw, true)); } /** @@ -85,7 +86,7 @@ class CheckUpdate */ private static function loadJsonData() { - self::$current_conf = Config::load(self::$repository); + self::$current_conf = json(self::$repository); } /** diff --git a/src/plugin/Competition.php b/src/plugin/Competition.php index 788f5f1..57fe45d 100644 --- a/src/plugin/Competition.php +++ b/src/plugin/Competition.php @@ -19,7 +19,8 @@ class Competition use TimeLock; /** - * @use 赛事入口 https://www.bilibili.com/v/game/match/competition + * @use run + * @doc 赛事入口 https://www.bilibili.com/v/game/match/competition */ public static function run() { diff --git a/src/plugin/DailyBag.php b/src/plugin/DailyBag.php index 9113d90..f8975cf 100644 --- a/src/plugin/DailyBag.php +++ b/src/plugin/DailyBag.php @@ -18,6 +18,9 @@ class DailyBag { use TimeLock; + /** + * @use run + */ public static function run() { if (self::getLock() > time() || !getEnable('daily_bag')) { diff --git a/src/plugin/DailyTask.php b/src/plugin/DailyTask.php index d655547..65b01bc 100644 --- a/src/plugin/DailyTask.php +++ b/src/plugin/DailyTask.php @@ -18,6 +18,9 @@ class DailyTask { use TimeLock; + /** + * @use run + */ public static function run() { if (self::getLock() > time() || !getEnable('daily_task')) { @@ -72,6 +75,7 @@ class DailyTask $payload = []; $data = Curl::get('app', $url, Sign::common($payload)); $data = json_decode($data, true); + // 您被封禁了,无法进行操作 // {"code":1011040,"message":"今日已签到过,无法重复签到","ttl":1,"data":null} // {"code":0,"message":"0","ttl":1,"data":{"text":"3000点用户经验,2根辣条","specialText":"再签到3天可以获得666银瓜子","allDays":31,"hadSignDays":2,"isBonusDay":0}} // {"code":0,"message":"0","ttl":1,"data":{"text":"3000点用户经验,2根辣条,50根辣条","specialText":"","allDays":31,"hadSignDays":20,"isBonusDay":1}} diff --git a/src/plugin/DoubleHeart.php b/src/plugin/DoubleHeart.php index 5979ca6..73f4f66 100644 --- a/src/plugin/DoubleHeart.php +++ b/src/plugin/DoubleHeart.php @@ -18,6 +18,9 @@ class DoubleHeart { use TimeLock; + /** + * @use run + */ public static function run() { if (self::getLock() > time() || !getEnable('double_heart')) { diff --git a/src/plugin/Forward.php b/src/plugin/Forward.php index 4e2a7be..d65a89a 100644 --- a/src/plugin/Forward.php +++ b/src/plugin/Forward.php @@ -21,7 +21,7 @@ namespace BiliHelper\Plugin; use BiliHelper\Core\Log; use BiliHelper\Util\TimeLock; -use Noodlehaus\Config; +use function JBZoo\Data\json; class Forward { @@ -42,7 +42,9 @@ class Forward private static string $repository = APP_DATA_PATH . 'reply_words.json'; - + /** + * @use run + */ public static function run() { @@ -270,8 +272,9 @@ class Forward */ private static function getReplyMsgList(): array { - $data = Config::load(self::$repository); - $data = $data->get("DynamicForward.default"); + $data = json(self::$repository); + // 给个默认值 + $data = $data->find("DynamicForward.default", []); array_push($data, getConf('auto_reply_text', 'dynamic')); return $data; } diff --git a/src/plugin/GiftHeart.php b/src/plugin/GiftHeart.php index 91e2c48..0863285 100644 --- a/src/plugin/GiftHeart.php +++ b/src/plugin/GiftHeart.php @@ -18,6 +18,9 @@ class GiftHeart { use TimeLock; + /** + * @use run + */ public static function run() { if (self::getLock() > time() || !getEnable('gift_heart')) { diff --git a/src/plugin/GiftRaffle.php b/src/plugin/GiftRaffle.php index 1651f86..e5efd3f 100644 --- a/src/plugin/GiftRaffle.php +++ b/src/plugin/GiftRaffle.php @@ -100,7 +100,7 @@ class GiftRaffle extends BaseRaffle * @param array $results * @return mixed */ - protected static function parseLottery(array $results):mixed + protected static function parseLottery(array $results): mixed { foreach ($results as $result) { $data = $result['source']; diff --git a/src/plugin/GiftSend.php b/src/plugin/GiftSend.php index e730c8c..da368ca 100644 --- a/src/plugin/GiftSend.php +++ b/src/plugin/GiftSend.php @@ -26,7 +26,9 @@ class GiftSend protected static array $room_list = []; protected static array $medal_list = []; - + /** + * @use run + */ public static function run() { if (self::getLock() > time() || !self::inTime('23:50:00', '23:59:50')) { diff --git a/src/plugin/GroupSignIn.php b/src/plugin/GroupSignIn.php index a27fa96..c102d14 100644 --- a/src/plugin/GroupSignIn.php +++ b/src/plugin/GroupSignIn.php @@ -18,6 +18,9 @@ class GroupSignIn { use TimeLock; + /** + * @use run + */ public static function run() { if (self::getLock() > time() || !getEnable('love_club')) { diff --git a/src/plugin/GuardRaffle.php b/src/plugin/GuardRaffle.php index a52b3f9..2d20813 100644 --- a/src/plugin/GuardRaffle.php +++ b/src/plugin/GuardRaffle.php @@ -114,7 +114,7 @@ class GuardRaffle extends BaseRaffle * @param array $results * @return mixed */ - protected static function parseLottery(array $results):mixed + protected static function parseLottery(array $results): mixed { foreach ($results as $result) { $data = $result['source']; diff --git a/src/plugin/Judge.php b/src/plugin/Judge.php index 2ff6d94..0da46d0 100644 --- a/src/plugin/Judge.php +++ b/src/plugin/Judge.php @@ -36,6 +36,9 @@ class Judge private static array $wait_case = []; + /** + * @use run + */ public static function run() { // 基础判断 diff --git a/src/plugin/LiveReservation.php b/src/plugin/LiveReservation.php index e5da383..126e95d 100644 --- a/src/plugin/LiveReservation.php +++ b/src/plugin/LiveReservation.php @@ -18,7 +18,9 @@ class LiveReservation { use TimeLock; - + /** + * @use run + */ public static function run() { if (self::getLock() > time() || !getEnable('live_reservation')) { @@ -93,10 +95,12 @@ class LiveReservation $de_raw = json_decode($raw, true); // {"code":0,"message":"0","ttl":1,"data":[{"sid":253672,"name":"直播预约:创世之音-虚拟偶像演唱会","total":6382,"stime":1636716437,"etime":1637408100,"is_follow":1,"state":100,"oid":"","type":2,"up_mid":9617619,"reserve_record_ctime":1636731801,"live_plan_start_time":1637406000,"lottery_type":1,"lottery_prize_info":{"text":"预约有奖:小电视年糕抱枕、哔哩哔哩小电视樱花毛绒抱枕大号、哔哩哔哩小夜灯","lottery_icon":"https://i0.hdslb.com/bfs/activity-plat/static/ce06d65bc0a8d8aa2a463747ce2a4752/rgHplMQyiX.png","jump_url":"https://www.bilibili.com/h5/lottery/result?business_id=253672\u0026business_type=10\u0026lottery_id=76240"},"show_total":true,"subtitle":""},{"sid":246469,"name":"直播预约:创世之音-YuNi个人演唱会","total":3555,"stime":1636367836,"etime":1637494500,"is_follow":0,"state":100,"oid":"","type":2,"up_mid":9617619,"reserve_record_ctime":0,"live_plan_start_time":1637492400,"show_total":true,"subtitle":""}]} if (!$de_raw['code']) { - foreach ($de_raw['data'] as $data) { + // data == NULL + $de_data = $de_raw['data'] ?: []; + foreach ($de_data as $data) { $result = self::checkLottery($data); if (!$result) continue; - array_push($reservation_list, $result); + $reservation_list[] = $result; } } else { Log::warning("获取预约列表失败: $raw"); diff --git a/src/plugin/Login.php b/src/plugin/Login.php index 656e8d4..f7d6151 100644 --- a/src/plugin/Login.php +++ b/src/plugin/Login.php @@ -25,6 +25,7 @@ class Login private static string $password; /** + * @use run * @throws \Jelix\IniFile\IniException */ public static function run() diff --git a/src/plugin/MainSite.php b/src/plugin/MainSite.php index 33d5087..49ce2de 100644 --- a/src/plugin/MainSite.php +++ b/src/plugin/MainSite.php @@ -19,6 +19,9 @@ class MainSite { use TimeLock; + /** + * @use run + */ public static function run() { if (self::getLock() > time() || !getEnable('main_site')) { @@ -53,6 +56,9 @@ class MainSite ]; // {"code":34005,"message":"超过投币上限啦~","ttl":1,"data":{"like":false}} // {"code":0,"message":"0","ttl":1,"data":{"like":false}} + // CODE -> 137001 MSG -> 账号封禁中,无法完成操作 + // CODE -> -650 MSG -> 用户等级太低 + $raw = Curl::post('app', $url, Sign::common($payload), $headers); $de_raw = json_decode($raw, true); if ($de_raw['code'] == 0) { diff --git a/src/plugin/ManGa.php b/src/plugin/ManGa.php index 1592f30..79c1cda 100644 --- a/src/plugin/ManGa.php +++ b/src/plugin/ManGa.php @@ -18,6 +18,9 @@ class ManGa { use TimeLock; + /** + * @use run + */ public static function run() { if (self::getLock() > time() || !getEnable('manga')) { diff --git a/src/plugin/MaterialObject.php b/src/plugin/MaterialObject.php index 5bd343d..381025d 100644 --- a/src/plugin/MaterialObject.php +++ b/src/plugin/MaterialObject.php @@ -24,6 +24,9 @@ class MaterialObject private static int $start_aid = 0; private static int $end_aid = 0; + /** + * @use run + */ public static function run() { if (self::getLock() > time() || !getEnable('live_box')) { diff --git a/src/plugin/PkRaffle.php b/src/plugin/PkRaffle.php index 1ba47f8..e06664e 100644 --- a/src/plugin/PkRaffle.php +++ b/src/plugin/PkRaffle.php @@ -97,7 +97,7 @@ class PkRaffle extends BaseRaffle * @param array $results * @return mixed */ - protected static function parseLottery(array $results):mixed + protected static function parseLottery(array $results): mixed { foreach ($results as $result) { $data = $result['source']; diff --git a/src/plugin/PolishTheMedal.php b/src/plugin/PolishTheMedal.php index ad1d55d..310496b 100644 --- a/src/plugin/PolishTheMedal.php +++ b/src/plugin/PolishTheMedal.php @@ -23,6 +23,7 @@ class PolishTheMedal private static array $grey_fans_medals = []; // 灰色勋章 /** + * @use run */ public static function run() { diff --git a/src/plugin/Schedule.php b/src/plugin/Schedule.php index 6e472bb..b3f8ee1 100644 --- a/src/plugin/Schedule.php +++ b/src/plugin/Schedule.php @@ -36,7 +36,9 @@ class Schedule // 暂定不做处理 大会员类 private static array $guarded_fifth = ['VipPrivilege', 'BpConsumption']; - + /** + * @use run + */ public static function run() { if (self::getLock() > time()) { diff --git a/src/plugin/Silver.php b/src/plugin/Silver.php index 6b82617..c9738f4 100644 --- a/src/plugin/Silver.php +++ b/src/plugin/Silver.php @@ -20,6 +20,9 @@ class Silver protected static array $task = []; + /** + * @use run + */ public static function run() { if (self::getLock() > time()) { diff --git a/src/plugin/Silver2Coin.php b/src/plugin/Silver2Coin.php index 5bb8b0f..03fd37b 100644 --- a/src/plugin/Silver2Coin.php +++ b/src/plugin/Silver2Coin.php @@ -18,6 +18,9 @@ class Silver2Coin { use TimeLock; + /** + * @use run + */ public static function run() { if (self::getLock() > time() || !getEnable('silver2coin')) { @@ -37,7 +40,7 @@ class Silver2Coin */ protected static function appSilver2coin(): bool { - sleep(0.5); + usleep(0.5 * APP_MICROSECOND); $url = 'https://api.live.bilibili.com/AppExchange/silver2coin'; $payload = []; $raw = Curl::post('app', $url, Sign::common($payload)); @@ -53,7 +56,7 @@ class Silver2Coin */ protected static function pcSilver2coin(): bool { - sleep(0.5); + usleep(0.5 * APP_MICROSECOND); $payload = [ 'csrf_token' => getCsrf(), 'csrf' => getCsrf(), diff --git a/src/plugin/SmallHeart.php b/src/plugin/SmallHeart.php index da8e64b..126df00 100644 --- a/src/plugin/SmallHeart.php +++ b/src/plugin/SmallHeart.php @@ -24,6 +24,9 @@ class SmallHeart private static int $total_time = 0; private static array|null $metal = null; + /** + * @use run + */ public static function run() { if (!getEnable('small_heart')) { diff --git a/src/plugin/Statistics.php b/src/plugin/Statistics.php index 5691b9e..a83f9cd 100644 --- a/src/plugin/Statistics.php +++ b/src/plugin/Statistics.php @@ -12,7 +12,7 @@ namespace BiliHelper\Plugin; use BiliHelper\Core\Log; use BiliHelper\Util\TimeLock; -use MathieuViossat\Util\ArrayToTextTable; +use BiliHelper\Tool\ArrayToTextTable; class Statistics { @@ -23,7 +23,10 @@ class Statistics private static array $success_list = []; private static array $profit_list = []; - // Todo 统计开关 统计时间间隔 统计类型 + /** + * @use run + * @todo 统计开关 统计时间间隔 统计类型 + */ public static function run() { if (self::getLock() > time()) { diff --git a/src/plugin/StormRaffle.php b/src/plugin/StormRaffle.php index 99b0d10..d883dfd 100644 --- a/src/plugin/StormRaffle.php +++ b/src/plugin/StormRaffle.php @@ -154,7 +154,7 @@ class StormRaffle extends BaseRaffle * @param array $results * @return mixed */ - protected static function parseLottery(array $results):mixed + protected static function parseLottery(array $results): mixed { foreach ($results as $result) { $data = $result['source']; diff --git a/src/plugin/VipPrivilege.php b/src/plugin/VipPrivilege.php index 09e8a48..890bc58 100644 --- a/src/plugin/VipPrivilege.php +++ b/src/plugin/VipPrivilege.php @@ -24,6 +24,9 @@ class VipPrivilege 2 => '会员购优惠券' ]; + /** + * @use run + */ public static function run() { if (self::getLock() > time() || !getEnable('vip_privilege')) { diff --git a/src/plugin/ZoneTcpClient.php b/src/plugin/ZoneTcpClient.php index 23b41ad..f64b293 100644 --- a/src/plugin/ZoneTcpClient.php +++ b/src/plugin/ZoneTcpClient.php @@ -37,7 +37,7 @@ class ZoneTcpClient /** - * @use 入口 + * @use run */ public static function run() { diff --git a/src/script/DelDynamic.php b/src/script/DelDynamic.php index fd840fa..e167ce7 100644 --- a/src/script/DelDynamic.php +++ b/src/script/DelDynamic.php @@ -15,6 +15,9 @@ class DelDynamic extends BaseTask { public static string $description = '批量删除动态,默认单次最大清理300个动态.'; + /** + * @use run + */ public static function run() { Log::error('当前功能未完成'); diff --git a/src/script/UnFollow.php b/src/script/UnFollow.php index df673fc..2ea2a5b 100644 --- a/src/script/UnFollow.php +++ b/src/script/UnFollow.php @@ -17,6 +17,7 @@ class UnFollow extends BaseTask public static string $description = '批量清理选定分组关注,默认单次最大清理600个关注.'; /** + * @use run * @throws \Exception */ public static function run() diff --git a/src/tool/ArrayToTextTable.php b/src/tool/ArrayToTextTable.php new file mode 100644 index 0000000..9120df3 --- /dev/null +++ b/src/tool/ArrayToTextTable.php @@ -0,0 +1,222 @@ +setData($data) + ->setIndentation('') + ->setKeysAlignment(self::AlignCenter) + ->setValuesAlignment(self::AlignLeft) + ->setFormatter(null); + } + + public function __toString() + { + return $this->getTable(); + } + + public function getTable($data = null): string + { + if (!is_null($data)) + $this->setData($data); + + $data = $this->prepare(); + + $i = $this->indentation; + + $table = $i . $this->line('┌', '─', '┬', '┐') . PHP_EOL; + + if ($this->displayHeader) { + //绘制table header + $headerRows = array_combine($this->keys, $this->keys); + $table .= $i . $this->row($headerRows, $this->keysAlignment) . PHP_EOL; + $table .= $i . $this->line('├', '─', '┼', '┤') . PHP_EOL; + } + + foreach ($data as $row) { + $table .= $i . $this->row($row, $this->valuesAlignment) . PHP_EOL; + } + $table .= $i . $this->line('└', '─', '┴', '┘') . PHP_EOL; + + return $table; + } + + public function setIndentation($indentation): static + { + $this->indentation = $indentation; + return $this; + } + + public function isDisplayHeader(bool $displayHeader): static + { + $this->displayHeader = $displayHeader; + return $this; + } + + public function setKeysAlignment($keysAlignment): static + { + $this->keysAlignment = $keysAlignment; + return $this; + } + + public function setValuesAlignment($valuesAlignment): static + { + $this->valuesAlignment = $valuesAlignment; + return $this; + } + + public function setFormatter($formatter): static + { + $this->formatter = $formatter; + return $this; + } + + private function line($left, $horizontal, $link, $right): string + { + $line = $left; + foreach ($this->keys as $key) { + $line .= str_repeat($horizontal, $this->widths[$key] + 2) . $link; + } + + if (mb_strlen($line) > mb_strlen($left)) { + $line = mb_substr($line, 0, -mb_strlen($horizontal)); + } + return $line . $right; + } + + private function row($row, $alignment): string + { + $line = '│'; + foreach ($this->keys as $key) { + $value = $row[$key] ?? ''; + $line .= ' ' . static::mb_str_pad($value, $this->widths[$key], ' ', $alignment) . ' ' . '│'; + } + if (empty($row)) { + $line .= '│'; + } + return $line; + } + + private function prepare(): array + { + $this->keys = []; + $this->widths = []; + $data = $this->data; + + //合并全部数组的key + foreach ($data as $row) { + $this->keys = array_merge($this->keys, array_keys($row)); + } + $this->keys = array_unique($this->keys); + + //补充缺陷数组 + foreach ($data as $index => $row) { + foreach ($this->keys as $key) { + if (!array_key_exists($key, $row)) { + $data[$index][$key] = null; + } + } + } + + //执行formatter + if ($this->formatter instanceof \Closure) { + foreach ($data as &$row) { + array_walk($row, $this->formatter); + } + unset($row); + } + + foreach ($this->keys as $key) { + $this->setWidth($key, $key); + } + foreach ($data as $row) { + foreach ($row as $columnKey => $columnValue) { + $this->setWidth($columnKey, $columnValue); + } + } + return $data; + } + + private function setWidth($key, $value) + { + if (!isset($this->widths[$key])) { + $this->widths[$key] = 0; + } + // Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated + // Deprecated: mb_strlen(): Passing null to parameter #1 ($string) of type string is deprecated + $value = $value ?: ''; + $width = (strlen($value) + mb_strlen($value, 'UTF8')) / 2; + if ($width > $this->widths[$key]) { + $this->widths[$key] = $width; + } + } + + private static function countCJK($string): bool|int|null + { + return preg_match_all('/[\p{Han}\p{Katakana}\p{Hiragana}\p{Hangul}]/u', $string); + } + + private function mb_str_pad($input, $pad_length, $pad_string = ' ', $pad_type = STR_PAD_RIGHT, $encoding = null): string + { + // $encoding = $encoding === null ? mb_internal_encoding() : $encoding; + // $diff = strlen($input) - (strlen($input) + mb_strlen($input, $encoding)) / 2; + // return str_pad($input, $pad_length + $diff, $pad_string, $pad_type); + + // https://github.com/viossat/arraytotexttable/blob/6b1af924478cb9c3a903269e304fff006fe0dbf4/src/ArrayToTextTable.php#L255 + $encoding = $encoding === null ? mb_internal_encoding() : $encoding; + $pad_before = $pad_type === STR_PAD_BOTH || $pad_type === STR_PAD_LEFT; + $pad_after = $pad_type === STR_PAD_BOTH || $pad_type === STR_PAD_RIGHT; + $pad_length -= mb_strlen($input, $encoding) + static::countCJK($input); + $target_length = $pad_before && $pad_after ? $pad_length / 2 : $pad_length; + + $repeat_times = ceil($target_length / mb_strlen($pad_string, $encoding)); + $repeated_string = str_repeat($pad_string, max(0, $repeat_times)); + $before = $pad_before ? mb_substr($repeated_string, 0, floor($target_length), $encoding) : ''; + $after = $pad_after ? mb_substr($repeated_string, 0, ceil($target_length), $encoding) : ''; + + return $before . $input . $after; + + } + + private function setData($data): static + { + if (!is_array($data)) { + $data = []; + } + $arrayData = []; + foreach ($data as $row) { + if (is_array($row)) { + $arrayData[] = $row; + } else if (is_object($row)) { + $arrayData[] = get_object_vars($row); + } + } + $this->data = $arrayData; + return $this; + } +} \ No newline at end of file diff --git a/src/tool/Path.php b/src/tool/Path.php new file mode 100644 index 0000000..7d78c97 --- /dev/null +++ b/src/tool/Path.php @@ -0,0 +1,37 @@ + $act, 'time' => $time ]; - array_push(static::$tasks, $task); + static::$tasks[] = $task; return true; } diff --git a/src/util/BaseRaffle.php b/src/util/BaseRaffle.php index 321c75b..584c7d7 100644 --- a/src/util/BaseRaffle.php +++ b/src/util/BaseRaffle.php @@ -30,6 +30,9 @@ abstract class BaseRaffle protected static array $all_list; protected static array $banned_rids = []; + /** + * @use run + */ public static function run() { if (!getEnable(static::ACTIVE_SWITCH)) { diff --git a/tests/ArrayToTextTableTest.php b/tests/ArrayToTextTableTest.php new file mode 100644 index 0000000..4f964ab --- /dev/null +++ b/tests/ArrayToTextTableTest.php @@ -0,0 +1,48 @@ + 'Mo 啊大苏打allie', + 'surname' => 'Alv萨达速度asarez', + 'email' => 'molliealvarez@example.com', + ], + [ + 'firstname' => 'Dianna', + 'surname' => 'Mcbride', + 'age' => 1111, + 'email' => 'diannamcbride@example.com', + ], + [ + 'firstname' => '撒旦撒旦asra', + 'surname' => 'Muel大大是打算的ler', + 'age' => 50, + 'email' => 'elviramueller@example.com', + ], + [ + 'firstname' => 'Corine', + 'surname' => 'Morton', + 'age' => 0, + ], + [ + 'firstname' => 'James', + 'surname' => 'Allison', + ], + [ + 'firstname' => 'Bowen这是哥', + 'surname' => 'Kelley', + 'age' => 50, + 'email' => 'bowenkelley@example.com', + ], + ]; + $renderer = new ArrayToTextTable($data); + echo $renderer->getTable(); + } +}