[update] *Raffle

This commit is contained in:
Lkeme 2020-01-04 00:54:33 +08:00
parent 9244ae0d29
commit 2d3362196c
13 changed files with 423 additions and 182 deletions

View File

@ -1,6 +1,22 @@
# Release Notes # Release Notes
# 本项目Log # 本项目Log
## v0.1.0.200103 alpha (2020-01-03)
### Added
- 增加并发请求
-
### Changed
- 更新抽奖逻辑
- 更新部分API
- 减少重复请求
-
### Fixed
- 修复部分回显错误
-
## v0.1.0.200101 alpha (2020-01-01) ## v0.1.0.200101 alpha (2020-01-01)
### Added ### Added

12
DOC.md
View File

@ -32,11 +32,11 @@ B 站直播实用脚本
|MaterialObject |19.12.27 |实物抽奖 | |MaterialObject |19.12.27 |实物抽奖 |
|AloneTcpClient |19.12.27 |独立监控 | |AloneTcpClient |19.12.27 |独立监控 |
|ZoneTcpClient |19.12.27 |分区监控 | |ZoneTcpClient |19.12.27 |分区监控 |
|StormRaffle |19.12.27 |节奏风暴 | |StormRaffle |20.01.03 |节奏风暴 |
|GiftRaffle |19.12.27 |活动礼物 | |GiftRaffle |20.01.03 |活动礼物 |
|PkRaffle |19.12.27 |大乱斗 | |PkRaffle |20.01.03 |大乱斗 |
|GuardRaffle |19.12.27 |舰长总督 | |GuardRaffle |20.01.03 |舰长总督 |
|AnchorRaffle |19.12.27 |天选时刻 | |AnchorRaffle |20.01.03 |天选时刻 |
|AwardRecord |19.12.27 |获奖通知 | |AwardRecord |19.12.27 |获奖通知 |
|Statistics |19.12.27 |数据统计 | |Statistics |19.12.27 |数据统计 |
@ -90,7 +90,7 @@ composer config -g repos.packagist composer https://mirrors.cloud.tencent.com/co
1. 下载(克隆)项目代码,初始化项目 1. 下载(克隆)项目代码,初始化项目
``` ```
$ git clone https://github.com/lkeme/BiliHelper-personal.git $ git clone https://github.com/lkeme/BiliHelper-personal.git
$ cd BiliHelper/conf $ cd BiliHelper-personal/conf
$ cp user.conf.example user.conf $ cp user.conf.example user.conf
``` ```
2. 使用 [composer](https://getcomposer.org/download/) 工具进行安装 2. 使用 [composer](https://getcomposer.org/download/) 工具进行安装

View File

@ -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**
## 文档 ## 文档

View File

@ -14,7 +14,8 @@
"bramus/monolog-colored-line-formatter": "^2.0", "bramus/monolog-colored-line-formatter": "^2.0",
"clue/socket-raw": "^1.4.1", "clue/socket-raw": "^1.4.1",
"vlucas/phpdotenv": "^4.1", "vlucas/phpdotenv": "^4.1",
"amphp/amp": "^2.4" "amphp/amp": "^2.4",
"lkeme/curl-future": "dev-master"
}, },
"license": "MIT", "license": "MIT",
"authors": [ "authors": [

45
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "bc4f20d52292405305a8aa3bd6dcf4bd", "content-hash": "6e83be077b5af9863dab5c4affd54c0b",
"packages": [ "packages": [
{ {
"name": "amphp/amp", "name": "amphp/amp",
@ -244,6 +244,45 @@
], ],
"time": "2019-10-28T12:32:07+00:00" "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", "name": "monolog/monolog",
"version": "1.25.3", "version": "1.25.3",
@ -574,7 +613,9 @@
"packages-dev": [], "packages-dev": [],
"aliases": [], "aliases": [],
"minimum-stability": "stable", "minimum-stability": "stable",
"stability-flags": [], "stability-flags": {
"lkeme/curl-future": 20
},
"prefer-stable": false, "prefer-stable": false,
"prefer-lowest": false, "prefer-lowest": false,
"platform": { "platform": {

View File

@ -42,20 +42,19 @@ class App
/** /**
* @use 新任务 * @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) { while (true) {
call_user_func(array('BiliHelper\Plugin\\' . $classname, 'run'), []); call_user_func(array('BiliHelper\Plugin\\' . $taskName, 'run'), []);
yield new Delayed(1000); yield new Delayed(1000);
} }
}); });
} }
/** /**
* @use 核心运行 * @use 核心运行
*/ */

View File

@ -10,6 +10,8 @@
namespace BiliHelper\Core; namespace BiliHelper\Core;
use CurlFuture\HttpFuture;
class Curl class Curl
{ {
public static $headers = array( public static $headers = array(
@ -29,6 +31,45 @@ class Curl
}, array_keys($headers), $headers); }, 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) public static function post($url, $payload = null, $headers = null, $timeout = 30)
{ {
$url = self::http2https($url); $url = self::http2https($url);

View File

@ -34,7 +34,7 @@ class AnchorRaffle extends BaseRaffle
* @param array $data * @param array $data
* @return bool * @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'])) { if (!array_key_exists('anchor', $data['data'])) {
@ -71,31 +71,55 @@ class AnchorRaffle extends BaseRaffle
/** /**
* @use 请求抽奖 * @use 创建抽奖任务
* @param array $data * @param array $raffles
* @return bool * @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/v1/Anchor/Join';
$tasks = [];
$results = [];
$user_info = User::parseCookies(); $user_info = User::parseCookies();
foreach ($raffles as $raffle) {
$payload = [ $payload = [
'id' => $data['raffle_id'], 'id' => $raffle['raffle_id'],
'roomid' => $data['room_id'], 'roomid' => $raffle['room_id'],
'platform' => 'pc', 'platform' => 'pc',
'csrf_token' => $user_info['token'], 'csrf_token' => $user_info['token'],
'csrf' => $user_info['token'], 'csrf' => $user_info['token'],
'visit_id' => '', 'visit_id' => ''
]; ];
$url = 'https://api.live.bilibili.com/xlive/lottery-interface/v1/Anchor/Join'; array_push($tasks, [
$raw = Curl::post($url, Sign::api($payload)); 'payload' => Sign::api($payload),
$de_raw = json_decode($raw, true); '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) { if (isset($de_raw['code']) && $de_raw['code'] == 0) {
Statistics::addSuccessList(self::ACTIVE_TITLE); Statistics::addSuccessList(self::ACTIVE_TITLE);
Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$data['raffle_name']}"); Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": 参与抽奖成功~");
} else { } else {
Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['message']}"); Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['message']}");
} }
return true; }
} }
} }

View File

@ -22,6 +22,7 @@ abstract class BaseRaffle
protected static $wait_list; protected static $wait_list;
protected static $finish_list; protected static $finish_list;
protected static $all_list; protected static $all_list;
protected static $room_stats = ['room_id' => 0, 'status' => false];
public static function run() public static function run()
{ {
@ -40,16 +41,13 @@ abstract class BaseRaffle
*/ */
protected static function startLottery(): bool protected static function startLottery(): bool
{ {
if (count(static::$wait_list) == 0) { if (count(static::$wait_list) == 0) {
return false; return false;
} }
if (count(static::$wait_list) < 100) { $raffle_list = [];
$max_num = mt_rand(10, 20); $room_list = [];
} else {
$max_num = mt_rand(45, 90);
}
static::$wait_list = static::arrKeySort(static::$wait_list, 'wait'); static::$wait_list = static::arrKeySort(static::$wait_list, 'wait');
$max_num = count(static::$wait_list);
for ($i = 0; $i <= $max_num; $i++) { for ($i = 0; $i <= $max_num; $i++) {
$raffle = array_shift(static::$wait_list); $raffle = array_shift(static::$wait_list);
if (is_null($raffle)) { if (is_null($raffle)) {
@ -57,11 +55,22 @@ abstract class BaseRaffle
} }
if ($raffle['wait'] > time()) { if ($raffle['wait'] > time()) {
array_push(static::$wait_list, $raffle); 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); 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; return true;
} }
@ -88,20 +97,28 @@ abstract class BaseRaffle
/** /**
* @use 解析抽奖数据 * @use 解析抽奖信息
* @param int $room_id * @param int $room_id
* @param array $data * @param array $data
* @return bool * @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 请求抽奖 * @use 创建抽奖
* @param array $data * @param array $raffles
* @return bool * @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排序 * @use 二维数组按key排序
@ -128,9 +145,10 @@ abstract class BaseRaffle
/** /**
* @use 去重检测 * @use 去重检测
* @param $lid * @param $lid
* @param bool $filter
* @return bool * @return bool
*/ */
protected static function toRepeatLid($lid): bool protected static function toRepeatLid($lid, $filter = true): bool
{ {
$lid = (int)$lid; $lid = (int)$lid;
if (in_array($lid, static::$all_list)) { if (in_array($lid, static::$all_list)) {
@ -139,7 +157,9 @@ abstract class BaseRaffle
if (count(static::$all_list) > 2000) { if (count(static::$all_list) > 2000) {
static::$all_list = []; static::$all_list = [];
} }
if ($filter) {
array_push(static::$all_list, $lid); array_push(static::$all_list, $lid);
}
return false; return false;
} }
@ -150,19 +170,28 @@ abstract class BaseRaffle
*/ */
public static function pushToQueue(array $data): bool public static function pushToQueue(array $data): bool
{ {
// 开关
if (getenv(static::ACTIVE_SWITCH) == 'false') { if (getenv(static::ACTIVE_SWITCH) == 'false') {
return false; return false;
} }
// 去重
if (Live::fishingDetection($data['rid'])) { if (static::toRepeatLid($data['lid'], false)) {
return 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']); $raffles_info = static::check($data['rid']);
if (!empty($raffles_info)) { if (!empty($raffles_info)) {
static::parse($data['rid'], $raffles_info); static::parseLotteryInfo($data['rid'], $raffles_info);
} }
$wait_num = count(static::$wait_list); $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 . "待抽奖"); Log::info("当前队列中共有 {$wait_num}" . static::ACTIVE_TITLE . "待抽奖");
} }
return true; return true;

View File

@ -31,7 +31,7 @@ class GiftRaffle extends BaseRaffle
* @param array $data * @param array $data
* @return bool * @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'])) { if (!array_key_exists('gift_list', $data['data'])) {
@ -65,38 +65,63 @@ class GiftRaffle extends BaseRaffle
/** /**
* @use 请求抽奖 * @use 创建抽奖任务
* @param array $data * @param array $raffles
* @return bool * @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接口 暂做保留处理 // V3接口 暂做保留处理
// $url = 'https://api.live.bilibili.com/gift/v3/smalltv/join'; // $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';
$url = 'https://api.live.bilibili.com/gift/v4/smalltv/getAward'; $url = 'https://api.live.bilibili.com/xlive/lottery-interface/v5/smalltv/join';
$raw = Curl::post($url, Sign::api($payload)); $tasks = [];
$de_raw = json_decode($raw, true); $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 (isset($de_raw['code']) && !$de_raw['code']) {
// 推送中奖信息 // 推送中奖信息
if ($de_raw['data']['gift_name'] != '辣条' && $de_raw['data']['gift_name'] != '') { if ($de_raw['data']['award_name'] != '辣条' && $de_raw['data']['award_name'] != '') {
$info = $de_raw['data']['gift_name'] . 'x' . $de_raw['data']['gift_num']; $info = $de_raw['data']['gift_name'] . 'x' . $de_raw['data']['gift_num'];
Notice::push('gift', $info); 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']}"); 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); Statistics::addSuccessList(self::ACTIVE_TITLE);
} else { } else {
Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['msg']}"); Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['msg']}");
} }
return true; }
} }
} }

View File

@ -32,7 +32,7 @@ class GuardRaffle extends BaseRaffle
* @param array $data * @param array $data
* @return bool * @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'])) { if (!array_key_exists('guard', $data['data'])) {
@ -82,32 +82,56 @@ class GuardRaffle extends BaseRaffle
/** /**
* @use 请求抽奖 * @use 创建抽奖任务
* @param array $data * @param array $raffles
* @return bool * @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(); $user_info = User::parseCookies();
foreach ($raffles as $raffle) {
$payload = [ $payload = [
'id' => $data['raffle_id'], 'id' => $raffle['raffle_id'],
'roomid' => $data['room_id'], 'roomid' => $raffle['room_id'],
"type" => "guard", "type" => "guard",
'csrf_token' => $user_info['token'], 'csrf_token' => $user_info['token'],
'csrf' => $user_info['token'] 'csrf' => $user_info['token'],
'visit_id' => ''
]; ];
$url = 'https://api.live.bilibili.com/lottery/v2/lottery/join'; array_push($tasks, [
$raw = Curl::post($url, Sign::api($payload)); 'payload' => Sign::api($payload),
$de_raw = json_decode($raw, true); '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) { if (isset($de_raw['code']) && $de_raw['code'] == 0) {
Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['data']['message']}"); 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); Statistics::addSuccessList(self::ACTIVE_TITLE);
} else { } else {
Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['msg']}"); Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['msg']}");
} }
return true; }
} }
} }

View File

@ -33,7 +33,7 @@ class PkRaffle extends BaseRaffle
* @param array $data * @param array $data
* @return bool * @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'])) { if (!array_key_exists('pk', $data['data'])) {
@ -68,22 +68,47 @@ class PkRaffle extends BaseRaffle
/** /**
* @use 请求抽奖 * @use 创建抽奖任务
* @param array $data * @param array $raffles
* @return bool * @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/v1/pk/join';
$tasks = [];
$results = [];
$user_info = User::parseCookies(); $user_info = User::parseCookies();
foreach ($raffles as $raffle) {
$payload = [ $payload = [
'id' => $data['raffle_id'], 'id' => $raffle['raffle_id'],
'roomid' => $data['room_id'], 'roomid' => $raffle['room_id'],
'csrf_token' => $user_info['token'], 'csrf_token' => $user_info['token'],
"csrf" => $user_info['token'], "csrf" => $user_info['token'],
]; ];
$url = 'https://api.live.bilibili.com/xlive/lottery-interface/v1/pk/join'; array_push($tasks, [
$raw = Curl::post($url, Sign::api($payload)); 'payload' => Sign::api($payload),
$de_raw = json_decode($raw, true); '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': 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': -1, 'message': '抽奖已结束', 'ttl': 1}
@ -96,6 +121,6 @@ class PkRaffle extends BaseRaffle
} else { } else {
Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['message']}"); Log::notice("房间 {$data['room_id']} 编号 {$data['raffle_id']} " . self::ACTIVE_TITLE . ": {$de_raw['message']}");
} }
return true; }
} }
} }

View File

@ -35,7 +35,7 @@ class StormRaffle extends BaseRaffle
* @param array $data * @param array $data
* @return bool * @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'])) { if (!array_key_exists('storm', $data['data'])) {
@ -85,57 +85,73 @@ class StormRaffle extends BaseRaffle
/** /**
* @use 请求抽奖 * @use 创建抽奖任务
* @param array $data * @param array $raffles
* @return bool * @return array
* @throws \Exception * @throws \Exception
*/ */
protected static function lottery(array $data): bool protected static function createLottery(array $raffles): array
{ {
$url = 'https://api.live.bilibili.com/lottery/v1/Storm/join';
$user_info = User::parseCookies();
foreach ($raffles as $raffle) {
self::$attempt = getenv('STORM_ATTEMPT') !== "" ? explode(',', getenv('STORM_ATTEMPT')) : [30, 50]; self::$attempt = getenv('STORM_ATTEMPT') !== "" ? explode(',', getenv('STORM_ATTEMPT')) : [30, 50];
$num = random_int((int)self::$attempt[0], (int)self::$attempt[1]); $num = random_int((int)self::$attempt[0], (int)self::$attempt[1]);
$user_info = User::parseCookies();
$payload = [ $payload = [
'id' => $data['raffle_id'], 'id' => $raffle['raffle_id'],
'roomid' => $data['room_id'], 'roomid' => $raffle['room_id'],
"color" => "16772431", "color" => "16772431",
"captcha_token" => "", "captcha_token" => "",
"captcha_phrase" => "", "captcha_phrase" => "",
"token" => $user_info['token'], "token" => $user_info['token'],
"csrf_token" => $user_info['token'], "csrf_token" => $user_info['token'],
"visit_id" => "", "visit_id" => ""
]; ];
$url = 'https://api.live.bilibili.com/lottery/v1/Storm/join';
for ($i = 1; $i < $num; $i++) { for ($i = 1; $i < $num; $i++) {
$raw = Curl::post($url, Sign::api($payload)); $raw = Curl::post($url, Sign::api($payload));
$de_raw = json_decode($raw, true); $de_raw = json_decode($raw, true);
if ($de_raw['code'] == 429 || $de_raw['code'] == -429) { if ($de_raw['code'] == 429 || $de_raw['code'] == -429) {
Log::notice(self::formatInfo($data['raffle_id'], $num, '节奏风暴未实名或异常验证码')); Log::notice(self::formatInfo($raffle['raffle_id'], $num, '节奏风暴未实名或异常验证码'));
break; break;
} }
if (isset($de_raw['data']) && empty($de_raw['data'])) { if (isset($de_raw['data']) && empty($de_raw['data'])) {
Log::notice(self::formatInfo($data['raffle_id'], $num, '节奏风暴在小黑屋')); Log::notice(self::formatInfo($raffle['raffle_id'], $num, '节奏风暴在小黑屋'));
break; break;
} }
if ($de_raw['code'] == 0) { if ($de_raw['code'] == 0) {
Statistics::addSuccessList(self::ACTIVE_TITLE); Statistics::addSuccessList(self::ACTIVE_TITLE);
Log::notice(self::formatInfo($data['raffle_id'], $num, $de_raw['data']['mobile_content'])); Log::notice(self::formatInfo($raffle['raffle_id'], $num, $de_raw['data']['mobile_content']));
break; break;
} }
if ($de_raw['msg'] == '节奏风暴不存在') { if ($de_raw['msg'] == '节奏风暴不存在') {
Log::notice(self::formatInfo($data['raffle_id'], $num, '节奏风暴已结束')); Log::notice(self::formatInfo($raffle['raffle_id'], $num, '节奏风暴已结束'));
break; break;
} }
if ($de_raw['msg'] == '已经领取奖励') { if ($de_raw['msg'] == '已经领取奖励') {
Log::notice(self::formatInfo($data['raffle_id'], $num, '节奏风暴已经领取')); Log::notice(self::formatInfo($raffle['raffle_id'], $num, '节奏风暴已经领取'));
break; break;
} }
if ($de_raw['msg'] == '你错过了奖励,下次要更快一点哦~') { if ($de_raw['msg'] == '你错过了奖励,下次要更快一点哦~') {
continue; continue;
} }
Log::notice(self::formatInfo($data['raffle_id'], $num, $de_raw['msg'])); Log::notice(self::formatInfo($raffle['raffle_id'], $num, $de_raw['msg']));
continue; 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 '';
}
} }
} }