diff --git a/CHANGELOG.md b/CHANGELOG.md index 669ac6f..0979a58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,22 @@ # Release Notes # 本项目Log +## v0.1.0.200103 alpha (2020-01-03) + +### Added +- 增加并发请求 +- + +### Changed +- 更新抽奖逻辑 +- 更新部分API +- 减少重复请求 +- + +### Fixed +- 修复部分回显错误 +- + ## v0.1.0.200101 alpha (2020-01-01) ### Added diff --git a/DOC.md b/DOC.md index 2c02486..0d1c774 100644 --- a/DOC.md +++ b/DOC.md @@ -32,11 +32,11 @@ B 站直播实用脚本 |MaterialObject |19.12.27 |实物抽奖 | |AloneTcpClient |19.12.27 |独立监控 | |ZoneTcpClient |19.12.27 |分区监控 | -|StormRaffle |19.12.27 |节奏风暴 | -|GiftRaffle |19.12.27 |活动礼物 | -|PkRaffle |19.12.27 |大乱斗 | -|GuardRaffle |19.12.27 |舰长总督 | -|AnchorRaffle |19.12.27 |天选时刻 | +|StormRaffle |20.01.03 |节奏风暴 | +|GiftRaffle |20.01.03 |活动礼物 | +|PkRaffle |20.01.03 |大乱斗 | +|GuardRaffle |20.01.03 |舰长总督 | +|AnchorRaffle |20.01.03 |天选时刻 | |AwardRecord |19.12.27 |获奖通知 | |Statistics |19.12.27 |数据统计 | @@ -90,7 +90,7 @@ composer config -g repos.packagist composer https://mirrors.cloud.tencent.com/co 1. 下载(克隆)项目代码,初始化项目 ``` $ git clone https://github.com/lkeme/BiliHelper-personal.git -$ cd BiliHelper/conf +$ cd BiliHelper-personal/conf $ cp user.conf.example user.conf ``` 2. 使用 [composer](https://getcomposer.org/download/) 工具进行安装 diff --git a/README.md b/README.md index f84a1e6..3575047 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Group: [55308141](https://jq.qq.com/?_wv=1027&k=5AIDaJg) ## 公告 -Currently for Personal Edition **0.1.0.200101 alpha** +Currently for Personal Edition **0.1.0.200103 alpha** ## 文档 diff --git a/composer.json b/composer.json index 5faa992..84e1a49 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ "bramus/monolog-colored-line-formatter": "^2.0", "clue/socket-raw": "^1.4.1", "vlucas/phpdotenv": "^4.1", - "amphp/amp": "^2.4" + "amphp/amp": "^2.4", + "lkeme/curl-future": "dev-master" }, "license": "MIT", "authors": [ diff --git a/composer.lock b/composer.lock index 31d1e93..ce7aa83 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "bc4f20d52292405305a8aa3bd6dcf4bd", + "content-hash": "6e83be077b5af9863dab5c4affd54c0b", "packages": [ { "name": "amphp/amp", @@ -244,6 +244,45 @@ ], "time": "2019-10-28T12:32:07+00:00" }, + { + "name": "lkeme/curl-future", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/lkeme/CurlFuture.git", + "reference": "084f1004ba722b72755c7519932117d7fb72c214" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/lkeme/CurlFuture/zipball/084f1004ba722b72755c7519932117d7fb72c214", + "reference": "084f1004ba722b72755c7519932117d7fb72c214", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "type": "library", + "autoload": { + "psr-4": { + "CurlFuture\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "leo", + "email": "263068280@qq.com" + } + ], + "description": "Parallel CURL Requests with PHP", + "time": "2020-01-03T16:39:17+00:00" + }, { "name": "monolog/monolog", "version": "1.25.3", @@ -574,7 +613,9 @@ "packages-dev": [], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": { + "lkeme/curl-future": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": { diff --git a/src/core/App.php b/src/core/App.php index 372c8f1..bdcf48a 100644 --- a/src/core/App.php +++ b/src/core/App.php @@ -42,20 +42,19 @@ class App /** * @use 新任务 - * @param string $classname + * @param string $taskName */ - public function newTask(string $classname) + public function newTask(string $taskName) { - asyncCall(function () use ($classname) { + asyncCall(function () use ($taskName) { while (true) { - call_user_func(array('BiliHelper\Plugin\\' . $classname, 'run'), []); + call_user_func(array('BiliHelper\Plugin\\' . $taskName, 'run'), []); yield new Delayed(1000); } }); } - /** * @use 核心运行 */ diff --git a/src/core/Curl.php b/src/core/Curl.php index 86e9b4d..74bf276 100644 --- a/src/core/Curl.php +++ b/src/core/Curl.php @@ -10,6 +10,8 @@ namespace BiliHelper\Core; +use CurlFuture\HttpFuture; + class Curl { public static $headers = array( @@ -29,6 +31,45 @@ class Curl }, array_keys($headers), $headers); } + public static function asyncPost($url, $tasks = null, $headers = null, $timeout = 30) + { + $new_tasks = []; + $results = []; + $url = self::http2https($url); + foreach ($tasks as $task) { + $payload = $task['payload']; + $header = is_null($headers) ? self::getHeaders(self::$headers) : self::getHeaders($headers); + $options = [ + 'header' => $header, + 'timeout' => $timeout, + 'post_data' => is_array($payload) ? http_build_query($payload) : $payload + ]; +// $options['proxy_url'] = "127.0.0.1:8888"; + if (getenv('USE_PROXY') == 'true') { + $options['proxy_url'] = "http://" . getenv('PROXY_IP') . ":" . getenv('PROXY_PORT'); + } + if (($cookie = getenv('COOKIE')) != "") { + $options['header']['Cookie'] = $cookie; + } + $new_task = new HttpFuture($url, $options); + array_push($new_tasks, [ + 'task' => $new_task, + 'source' => $task['source'] + ]); + } + foreach ($new_tasks as $new_task) { + Log::debug($url); + $result = $new_task['task']->fetch(); + // var_dump($result); + array_push($results, [ + 'content' => $result, + 'source' => $new_task['source'] + ]); + Log::debug($result); + } + return $results; + } + public static function post($url, $payload = null, $headers = null, $timeout = 30) { $url = self::http2https($url); diff --git a/src/plugin/AnchorRaffle.php b/src/plugin/AnchorRaffle.php index 7b792d9..4a070be 100644 --- a/src/plugin/AnchorRaffle.php +++ b/src/plugin/AnchorRaffle.php @@ -34,7 +34,7 @@ class AnchorRaffle extends BaseRaffle * @param array $data * @return bool */ - protected static function parse(int $room_id, array $data): bool + protected static function parseLotteryInfo(int $room_id, array $data): bool { // 防止异常 if (!array_key_exists('anchor', $data['data'])) { @@ -71,31 +71,55 @@ class AnchorRaffle extends BaseRaffle /** - * @use 请求抽奖 - * @param array $data - * @return bool + * @use 创建抽奖任务 + * @param array $raffles + * @return array */ - protected static function lottery(array $data): bool + protected static function createLottery(array $raffles): array { - $user_info = User::parseCookies(); - $payload = [ - 'id' => $data['raffle_id'], - 'roomid' => $data['room_id'], - 'platform' => 'pc', - 'csrf_token' => $user_info['token'], - 'csrf' => $user_info['token'], - 'visit_id' => '', - ]; $url = 'https://api.live.bilibili.com/xlive/lottery-interface/v1/Anchor/Join'; - $raw = Curl::post($url, Sign::api($payload)); - $de_raw = json_decode($raw, true); - - if (isset($de_raw['code']) && $de_raw['code'] == 0) { - Statistics::addSuccessList(self::ACTIVE_TITLE); - Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$data['raffle_name']}"); - } else { - Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['message']}"); + $tasks = []; + $results = []; + $user_info = User::parseCookies(); + foreach ($raffles as $raffle) { + $payload = [ + 'id' => $raffle['raffle_id'], + 'roomid' => $raffle['room_id'], + 'platform' => 'pc', + 'csrf_token' => $user_info['token'], + 'csrf' => $user_info['token'], + 'visit_id' => '' + ]; + array_push($tasks, [ + 'payload' => Sign::api($payload), + 'source' => [ + 'room_id' => $raffle['room_id'], + 'raffle_id' => $raffle['raffle_id'] + ] + ]); + } + $results = Curl::asyncPost($url, $tasks); + # print_r($results); + return $results; + } + + /** + * @use 解析抽奖信息 + * @param array $results + * @return mixed|void + */ + protected static function parseLottery(array $results) + { + foreach ($results as $result) { + $data = $result['source']; + $content = $result['content']; + $de_raw = json_decode($content, true); + if (isset($de_raw['code']) && $de_raw['code'] == 0) { + Statistics::addSuccessList(self::ACTIVE_TITLE); + Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": 参与抽奖成功~"); + } else { + Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['message']}"); + } } - return true; } } diff --git a/src/plugin/BaseRaffle.php b/src/plugin/BaseRaffle.php index 5867548..d5f53fa 100644 --- a/src/plugin/BaseRaffle.php +++ b/src/plugin/BaseRaffle.php @@ -22,6 +22,7 @@ abstract class BaseRaffle protected static $wait_list; protected static $finish_list; protected static $all_list; + protected static $room_stats = ['room_id' => 0, 'status' => false]; public static function run() { @@ -40,16 +41,13 @@ abstract class BaseRaffle */ protected static function startLottery(): bool { - if (count(static::$wait_list) == 0) { return false; } - if (count(static::$wait_list) < 100) { - $max_num = mt_rand(10, 20); - } else { - $max_num = mt_rand(45, 90); - } + $raffle_list = []; + $room_list = []; static::$wait_list = static::arrKeySort(static::$wait_list, 'wait'); + $max_num = count(static::$wait_list); for ($i = 0; $i <= $max_num; $i++) { $raffle = array_shift(static::$wait_list); if (is_null($raffle)) { @@ -57,11 +55,22 @@ abstract class BaseRaffle } if ($raffle['wait'] > time()) { array_push(static::$wait_list, $raffle); - continue; + break; } - Live::goToRoom($raffle['room_id']); + if (count($raffle_list) > 200) { + break; + } + array_push($room_list, $raffle['room_id']); + array_push($raffle_list, $raffle); Statistics::addJoinList(static::ACTIVE_TITLE); - static::lottery($raffle); + } + if (count($raffle_list) && count($room_list)) { + $room_list = array_unique($room_list); + foreach ($room_list as $room_id) { + Live::goToRoom($room_id); + } + $results = static::createLottery($raffle_list); + static::parseLottery($results); } return true; } @@ -88,20 +97,28 @@ abstract class BaseRaffle /** - * @use 解析抽奖数据 + * @use 解析抽奖信息 * @param int $room_id * @param array $data * @return bool */ - abstract protected static function parse(int $room_id, array $data): bool; + abstract protected static function parseLotteryInfo(int $room_id, array $data): bool; /** - * @use 请求抽奖 - * @param array $data - * @return bool + * @use 创建抽奖 + * @param array $raffles + * @return array */ - abstract protected static function lottery(array $data): bool; + abstract protected static function createLottery(array $raffles): array; + + + /** + * @use 解析抽奖返回 + * @param array $results + * @return mixed + */ + abstract protected static function parseLottery(array $results); /** * @use 二维数组按key排序 @@ -128,9 +145,10 @@ abstract class BaseRaffle /** * @use 去重检测 * @param $lid + * @param bool $filter * @return bool */ - protected static function toRepeatLid($lid): bool + protected static function toRepeatLid($lid, $filter = true): bool { $lid = (int)$lid; if (in_array($lid, static::$all_list)) { @@ -139,7 +157,9 @@ abstract class BaseRaffle if (count(static::$all_list) > 2000) { static::$all_list = []; } - array_push(static::$all_list, $lid); + if ($filter) { + array_push(static::$all_list, $lid); + } return false; } @@ -150,19 +170,28 @@ abstract class BaseRaffle */ public static function pushToQueue(array $data): bool { + // 开关 if (getenv(static::ACTIVE_SWITCH) == 'false') { return false; } - - if (Live::fishingDetection($data['rid'])) { + // 去重 + if (static::toRepeatLid($data['lid'], false)) { return false; } + // 钓鱼&&防止重复请求 + if ($data['rid'] != static::$room_stats['room_id']) { + static::$room_stats = ['room_id' => $data['rid'], 'status' => Live::fishingDetection($data['rid'])]; + } + if (static::$room_stats['status']) { + return false; + } + // 实际检测 $raffles_info = static::check($data['rid']); if (!empty($raffles_info)) { - static::parse($data['rid'], $raffles_info); + static::parseLotteryInfo($data['rid'], $raffles_info); } $wait_num = count(static::$wait_list); - if ($wait_num > 4 && ($wait_num % 2)) { + if ($wait_num > 10 && ($wait_num % 2)) { Log::info("当前队列中共有 {$wait_num} 个" . static::ACTIVE_TITLE . "待抽奖"); } return true; diff --git a/src/plugin/GiftRaffle.php b/src/plugin/GiftRaffle.php index 284f196..55ece16 100644 --- a/src/plugin/GiftRaffle.php +++ b/src/plugin/GiftRaffle.php @@ -31,7 +31,7 @@ class GiftRaffle extends BaseRaffle * @param array $data * @return bool */ - protected static function parse(int $room_id, array $data): bool + protected static function parseLotteryInfo(int $room_id, array $data): bool { // 防止异常 if (!array_key_exists('gift_list', $data['data'])) { @@ -65,38 +65,63 @@ class GiftRaffle extends BaseRaffle /** - * @use 请求抽奖 - * @param array $data - * @return bool + * @use 创建抽奖任务 + * @param array $raffles + * @return array */ - protected static function lottery(array $data): bool + protected static function createLottery(array $raffles): array { - $user_info = User::parseCookies(); - $payload = [ - 'raffleId' => $data['raffle_id'], - 'roomid' => $data['room_id'], - 'type' => $data['type'], - 'csrf_token' => $user_info['token'], - 'csrf' => $user_info['token'], - 'visit_id' => null, - ]; // V3接口 暂做保留处理 // $url = 'https://api.live.bilibili.com/gift/v3/smalltv/join'; - // $url = 'https://api.live.bilibili.com/xlive/lottery-interface/v5/smalltv/Join'; - $url = 'https://api.live.bilibili.com/gift/v4/smalltv/getAward'; - $raw = Curl::post($url, Sign::api($payload)); - $de_raw = json_decode($raw, true); - if (isset($de_raw['code']) && !$de_raw['code']) { - // 推送中奖信息 - if ($de_raw['data']['gift_name'] != '辣条' && $de_raw['data']['gift_name'] != '') { - $info = $de_raw['data']['gift_name'] . 'x' . $de_raw['data']['gift_num']; - Notice::push('gift', $info); - } - Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['data']['gift_name']}x{$de_raw['data']['gift_num']}"); - Statistics::addSuccessList(self::ACTIVE_TITLE); - } else { - Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['msg']}"); + // $url = 'https://api.live.bilibili.com/gift/v4/smalltv/getAward'; + $url = 'https://api.live.bilibili.com/xlive/lottery-interface/v5/smalltv/join'; + $tasks = []; + $results = []; + $user_info = User::parseCookies(); + foreach ($raffles as $raffle) { + $payload = [ + 'id' => $raffle['raffle_id'], + 'roomid' => $raffle['room_id'], + 'type' => $raffle['type'], + 'csrf_token' => $user_info['token'], + 'csrf' => $user_info['token'], + 'visit_id' => '' + ]; + array_push($tasks, [ + 'payload' => Sign::api($payload), + 'source' => [ + 'room_id' => $raffle['room_id'], + 'raffle_id' => $raffle['raffle_id'] + ] + ]); + } + $results = Curl::asyncPost($url, $tasks); + # print_r($results); + return $results; + } + + /** + * @use 解析抽奖信息 + * @param array $results + * @return mixed|void + */ + protected static function parseLottery(array $results) + { + foreach ($results as $result) { + $data = $result['source']; + $content = $result['content']; + $de_raw = json_decode($content, true); + if (isset($de_raw['code']) && !$de_raw['code']) { + // 推送中奖信息 + if ($de_raw['data']['award_name'] != '辣条' && $de_raw['data']['award_name'] != '') { + $info = $de_raw['data']['gift_name'] . 'x' . $de_raw['data']['gift_num']; + Notice::push('gift', $info); + } + Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['data']['award_name']}x{$de_raw['data']['award_num']}"); + Statistics::addSuccessList(self::ACTIVE_TITLE); + } else { + Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['msg']}"); + } } - return true; } } diff --git a/src/plugin/GuardRaffle.php b/src/plugin/GuardRaffle.php index ca084d1..2810a8c 100644 --- a/src/plugin/GuardRaffle.php +++ b/src/plugin/GuardRaffle.php @@ -32,7 +32,7 @@ class GuardRaffle extends BaseRaffle * @param array $data * @return bool */ - protected static function parse(int $room_id, array $data): bool + protected static function parseLotteryInfo(int $room_id, array $data): bool { // 防止异常 if (!array_key_exists('guard', $data['data'])) { @@ -82,32 +82,56 @@ class GuardRaffle extends BaseRaffle /** - * @use 请求抽奖 - * @param array $data - * @return bool + * @use 创建抽奖任务 + * @param array $raffles + * @return array */ - protected static function lottery(array $data): bool + protected static function createLottery(array $raffles): array { + $url = 'https://api.live.bilibili.com/xlive/lottery-interface/v3/guard/join'; + $tasks = []; + $results = []; $user_info = User::parseCookies(); - $payload = [ - 'id' => $data['raffle_id'], - 'roomid' => $data['room_id'], - "type" => "guard", - 'csrf_token' => $user_info['token'], - 'csrf' => $user_info['token'] - ]; - $url = 'https://api.live.bilibili.com/lottery/v2/lottery/join'; - $raw = Curl::post($url, Sign::api($payload)); - $de_raw = json_decode($raw, true); - - if (isset($de_raw['code']) && $de_raw['code'] == 0) { - Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['data']['message']}"); - Statistics::addSuccessList(self::ACTIVE_TITLE); - } else { - Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['msg']}"); + foreach ($raffles as $raffle) { + $payload = [ + 'id' => $raffle['raffle_id'], + 'roomid' => $raffle['room_id'], + "type" => "guard", + 'csrf_token' => $user_info['token'], + 'csrf' => $user_info['token'], + 'visit_id' => '' + ]; + array_push($tasks, [ + 'payload' => Sign::api($payload), + 'source' => [ + 'room_id' => $raffle['room_id'], + 'raffle_id' => $raffle['raffle_id'] + ] + ]); } - return true; + $results = Curl::asyncPost($url, $tasks); + # print_r($results); + return $results; } + /** + * @use 解析抽奖信息 + * @param array $results + * @return mixed|void + */ + protected static function parseLottery(array $results) + { + foreach ($results as $result) { + $data = $result['source']; + $content = $result['content']; + $de_raw = json_decode($content, true); + if (isset($de_raw['code']) && $de_raw['code'] == 0) { + Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": " . $de_raw['data']['award_name'] . "x" . $de_raw['data']['award_num']); + Statistics::addSuccessList(self::ACTIVE_TITLE); + } else { + Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['msg']}"); + } + } + } } \ No newline at end of file diff --git a/src/plugin/PkRaffle.php b/src/plugin/PkRaffle.php index 44d87e2..b9ecdf8 100644 --- a/src/plugin/PkRaffle.php +++ b/src/plugin/PkRaffle.php @@ -33,7 +33,7 @@ class PkRaffle extends BaseRaffle * @param array $data * @return bool */ - protected static function parse(int $room_id, array $data): bool + protected static function parseLotteryInfo(int $room_id, array $data): bool { // 防止异常 if (!array_key_exists('pk', $data['data'])) { @@ -68,34 +68,59 @@ class PkRaffle extends BaseRaffle /** - * @use 请求抽奖 - * @param array $data - * @return bool + * @use 创建抽奖任务 + * @param array $raffles + * @return array */ - protected static function lottery(array $data): bool + protected static function createLottery(array $raffles): array { - $user_info = User::parseCookies(); - $payload = [ - 'id' => $data['raffle_id'], - 'roomid' => $data['room_id'], - 'csrf_token' => $user_info['token'], - "csrf" => $user_info['token'], - ]; $url = 'https://api.live.bilibili.com/xlive/lottery-interface/v1/pk/join'; - $raw = Curl::post($url, Sign::api($payload)); - $de_raw = json_decode($raw, true); - /* - * {'code': 0, 'message': '0', 'ttl': 1, 'data': {'id': 343560, 'gift_type': 0, 'award_id': '1', 'award_text': '辣条X1', 'award_image': 'https://i0.hdslb.com/bfs/live/da6656add2b14a93ed9eb55de55d0fd19f0fc7f6.png', 'award_num': 0, 'title': '大乱斗获胜抽奖'}} - * {'code': -1, 'message': '抽奖已结束', 'ttl': 1} - * {'code': -2, 'message': '您已参加过抽奖', 'ttl': 1} - * {"code":-403,"data":null,"message":"访问被拒绝","msg":"访问被拒绝"} - */ - if (isset($de_raw['code']) && $de_raw['code'] == 0) { - Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['data']['award_text']}"); - Statistics::addSuccessList(self::ACTIVE_TITLE); - } else { - Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['message']}"); + $tasks = []; + $results = []; + $user_info = User::parseCookies(); + foreach ($raffles as $raffle) { + $payload = [ + 'id' => $raffle['raffle_id'], + 'roomid' => $raffle['room_id'], + 'csrf_token' => $user_info['token'], + "csrf" => $user_info['token'], + ]; + array_push($tasks, [ + 'payload' => Sign::api($payload), + 'source' => [ + 'room_id' => $raffle['room_id'], + 'raffle_id' => $raffle['raffle_id'] + ] + ]); + } + $results = Curl::asyncPost($url, $tasks); + # print_r($results); + return $results; + } + + /** + * @use 解析抽奖信息 + * @param array $results + * @return mixed|void + */ + protected static function parseLottery(array $results) + { + foreach ($results as $result) { + $data = $result['source']; + $content = $result['content']; + $de_raw = json_decode($content, true); + /* + * {'code': 0, 'message': '0', 'ttl': 1, 'data': {'id': 343560, 'gift_type': 0, 'award_id': '1', 'award_text': '辣条X1', 'award_image': 'https://i0.hdslb.com/bfs/live/da6656add2b14a93ed9eb55de55d0fd19f0fc7f6.png', 'award_num': 0, 'title': '大乱斗获胜抽奖'}} + * {'code': -1, 'message': '抽奖已结束', 'ttl': 1} + * {'code': -2, 'message': '您已参加过抽奖', 'ttl': 1} + * {"code":-403,"data":null,"message":"访问被拒绝","msg":"访问被拒绝"} + */ + if (isset($de_raw['code']) && $de_raw['code'] == 0) { + Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['data']['award_text']}"); + Statistics::addSuccessList(self::ACTIVE_TITLE); + } else { + Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['message']}"); + } } - return true; } } diff --git a/src/plugin/StormRaffle.php b/src/plugin/StormRaffle.php index 9c25b4f..104fcbe 100644 --- a/src/plugin/StormRaffle.php +++ b/src/plugin/StormRaffle.php @@ -35,7 +35,7 @@ class StormRaffle extends BaseRaffle * @param array $data * @return bool */ - protected static function parse(int $room_id, array $data): bool + protected static function parseLotteryInfo(int $room_id, array $data): bool { // 防止异常 if (!array_key_exists('storm', $data['data'])) { @@ -85,57 +85,73 @@ class StormRaffle extends BaseRaffle /** - * @use 请求抽奖 - * @param array $data - * @return bool + * @use 创建抽奖任务 + * @param array $raffles + * @return array * @throws \Exception */ - protected static function lottery(array $data): bool + protected static function createLottery(array $raffles): array { - self::$attempt = getenv('STORM_ATTEMPT') !== "" ? explode(',', getenv('STORM_ATTEMPT')) : [30, 50]; - $num = random_int((int)self::$attempt[0], (int)self::$attempt[1]); - $user_info = User::parseCookies(); - $payload = [ - 'id' => $data['raffle_id'], - 'roomid' => $data['room_id'], - "color" => "16772431", - "captcha_token" => "", - "captcha_phrase" => "", - "token" => $user_info['token'], - "csrf_token" => $user_info['token'], - "visit_id" => "", - ]; $url = 'https://api.live.bilibili.com/lottery/v1/Storm/join'; - for ($i = 1; $i < $num; $i++) { - $raw = Curl::post($url, Sign::api($payload)); - $de_raw = json_decode($raw, true); - if ($de_raw['code'] == 429 || $de_raw['code'] == -429) { - Log::notice(self::formatInfo($data['raffle_id'], $num, '节奏风暴未实名或异常验证码')); - break; - } - if (isset($de_raw['data']) && empty($de_raw['data'])) { - Log::notice(self::formatInfo($data['raffle_id'], $num, '节奏风暴在小黑屋')); - break; - } - if ($de_raw['code'] == 0) { - Statistics::addSuccessList(self::ACTIVE_TITLE); - Log::notice(self::formatInfo($data['raffle_id'], $num, $de_raw['data']['mobile_content'])); - break; - } - if ($de_raw['msg'] == '节奏风暴不存在') { - Log::notice(self::formatInfo($data['raffle_id'], $num, '节奏风暴已结束')); - break; - } - if ($de_raw['msg'] == '已经领取奖励') { - Log::notice(self::formatInfo($data['raffle_id'], $num, '节奏风暴已经领取')); - break; - } - if ($de_raw['msg'] == '你错过了奖励,下次要更快一点哦~') { + $user_info = User::parseCookies(); + foreach ($raffles as $raffle) { + self::$attempt = getenv('STORM_ATTEMPT') !== "" ? explode(',', getenv('STORM_ATTEMPT')) : [30, 50]; + $num = random_int((int)self::$attempt[0], (int)self::$attempt[1]); + $payload = [ + 'id' => $raffle['raffle_id'], + 'roomid' => $raffle['room_id'], + "color" => "16772431", + "captcha_token" => "", + "captcha_phrase" => "", + "token" => $user_info['token'], + "csrf_token" => $user_info['token'], + "visit_id" => "" + ]; + for ($i = 1; $i < $num; $i++) { + $raw = Curl::post($url, Sign::api($payload)); + $de_raw = json_decode($raw, true); + if ($de_raw['code'] == 429 || $de_raw['code'] == -429) { + Log::notice(self::formatInfo($raffle['raffle_id'], $num, '节奏风暴未实名或异常验证码')); + break; + } + if (isset($de_raw['data']) && empty($de_raw['data'])) { + Log::notice(self::formatInfo($raffle['raffle_id'], $num, '节奏风暴在小黑屋')); + break; + } + if ($de_raw['code'] == 0) { + Statistics::addSuccessList(self::ACTIVE_TITLE); + Log::notice(self::formatInfo($raffle['raffle_id'], $num, $de_raw['data']['mobile_content'])); + break; + } + if ($de_raw['msg'] == '节奏风暴不存在') { + Log::notice(self::formatInfo($raffle['raffle_id'], $num, '节奏风暴已结束')); + break; + } + if ($de_raw['msg'] == '已经领取奖励') { + Log::notice(self::formatInfo($raffle['raffle_id'], $num, '节奏风暴已经领取')); + break; + } + if ($de_raw['msg'] == '你错过了奖励,下次要更快一点哦~') { + continue; + } + Log::notice(self::formatInfo($raffle['raffle_id'], $num, $de_raw['msg'])); continue; } - Log::notice(self::formatInfo($data['raffle_id'], $num, $de_raw['msg'])); - continue; } - return true; + return []; + } + + /** + * @use 解析抽奖信息 + * @param array $results + * @return mixed|void + */ + protected static function parseLottery(array $results) + { + foreach ($results as $result) { + $data = $result['source']; + $content = $result['content']; + echo ''; + } } }