[fix] Return value must be of type array, fixes #204

This commit is contained in:
Lkeme 2023-01-27 15:01:20 +08:00
parent 2782e2af42
commit 3fdf4c25de
9 changed files with 81 additions and 58 deletions

View File

@ -25,6 +25,7 @@ use Bhp\Log\Log;
use Bhp\Plugin\BasePlugin;
use Bhp\Plugin\Plugin;
use Bhp\TimeLock\TimeLock;
use Bhp\Util\ArrayR\ArrayR;
use Bhp\Util\Exceptions\NoLoginException;
class MainSite extends BasePlugin
@ -75,14 +76,14 @@ class MainSite extends BasePlugin
* @param int $num
* @return array
*/
protected function fetchCustomAids(int $num = 30): array
protected function fetchCustomArchives(int $num = 30): array
{
if (getConf('main_site.fetch_aids_mode') == 'random') {
// 随机热门稿件榜单
return $this->getTopArchives($num);
} else {
// 固定获取关注UP稿件榜单, 不足会随机补全
return $this->getFollowUpAids($num);
return $this->getFollowUpArchives($num);
}
}
@ -110,7 +111,7 @@ class MainSite extends BasePlugin
return true;
}
// 稿件列表
$aids = $this->fetchCustomAids($actual_num);
$aids = $this->fetchCustomArchives($actual_num);
//
Log::info("主站任务: 预投币稿件 " . implode(" ", $aids));
// 投币
@ -155,14 +156,9 @@ class MainSite extends BasePlugin
//
if ($response['code']) {
Log::warning("主站任务: 获取首页推荐失败 {$response['code']} -> {$response['message']}");
return $this->getDayRankingArchives($num);
}
//
if ($num == 1) {
return [array_rand($response['data']['archives'], $num)];
} else {
return array_rand($response['data']['archives'], $num);
return $this->getTopFeedRCMDArchives($num);
}
return ArrayR::toSlice($response['data']['archives'], $num);
}
/**
@ -170,27 +166,19 @@ class MainSite extends BasePlugin
* @param int $num
* @return array
*/
protected function getDayRankingArchives(int $num): array
protected function getTopFeedRCMDArchives(int $num): array
{
$archives = [];
$rand_nums = [];
$new_archives = [];
//
$response = ApiVideo::ranking();
$response = ApiVideo::topFeedRCMD();
$archives = ArrayR::toSlice($response['data']['item'], $num);
//
for ($i = 0; $i < $num; $i++) {
while (true) {
$rand_num = mt_rand(1, 99);
if (in_array($rand_num, $rand_nums)) {
continue;
} else {
$rand_nums[] = $rand_num;
break;
}
}
$archives[] = $response['data']['list'][$rand_nums[$i]];
foreach ($archives as $archive) {
$archive['aid'] = $archive['id'];
unset($archive['id']);
$new_archives[] = $archive;
}
//
return $archives;
return $new_archives;
}
/**
@ -198,28 +186,29 @@ class MainSite extends BasePlugin
* @param int $num
* @return array
*/
protected function getFollowUpAids(int $num): array
protected function getFollowUpArchives(int $num): array
{
$aids = [];
$archives = [];
//
$response = ApiDynamicSvr::followUpDynamic();
//
if ($response['code']) {
Log::warning("主站任务: 获取UP稿件失败 {$response['code']} -> {$response['message']}");
return $aids;
}
//
foreach ($response['data']['cards'] as $index => $card) {
if ($index >= $num) {
break;
} else {
foreach ($response['data']['cards'] as $i => $card) {
// if ($i >= $num) break;
// JSON_ERROR_CTRL_CHAR
$temp = preg_replace('/[\x00-\x1F]/', '', $card['card']);
$archives[] = json_decode($temp, true);
}
$aids[] = $card['desc']['rid'];
$archives = ArrayR::toSlice($archives, $num, false);
}
// 此处补全缺失
if (count($aids) < $num) {
$aids = array_merge($aids, $this->getTopArchives($num - count($aids)));
if (($t_num = count($archives)) < $num) {
Log::warning("主站任务: 获取UP稿件数量不足将自动补全随机稿件。");
$archives = array_merge($archives, $this->getTopArchives($num - $t_num));
}
return $aids;
return $archives;
}
/**
@ -288,7 +277,7 @@ class MainSite extends BasePlugin
{
if (!getConf('main_site.share', false, 'bool')) return true;
$archives = $this->fetchCustomAids(10);
$archives = $this->fetchCustomArchives(10);
$archive = array_pop($archives);
$aid = (string)$archive['aid'];
@ -314,10 +303,16 @@ class MainSite extends BasePlugin
{
if (!getConf('main_site.watch', false, 'bool')) return true;
$archives = $this->fetchCustomAids(10);
$archives = $this->fetchCustomArchives(10);
$archive = array_pop($archives);
// 额外处理信息
$info = $this->getArchiveInfo((string)$archive['aid']);
//
if (isset($archive['duration']) && is_int($archive['duration'])) {
$info = $archive;
} else {
// 额外处理信息
$info = $this->getArchiveInfo((string)$archive['aid']);
}
//
$aid = (string)$info['aid'];
$cid = (string)$info['cid'];
$duration = (int)$info['duration'];
@ -358,8 +353,10 @@ class MainSite extends BasePlugin
protected function getArchiveInfo(string $aid): array
{
$response = ApiPlayer::pageList($aid);
return $response['data'];
//
$archive_info = $response['data'][0];
$archive_info['aid'] = $aid;
return $archive_info;
}

View File

@ -3,8 +3,8 @@ device_version: 0.0.1
app:
bili_a: # Android
package: "tv.danmaku.bili"
version: "7.3.0"
build: "7030300"
version: "7.14.1"
build: "7141100"
channel: "bili"
device: "phone"
mobi_app: "android"
@ -15,7 +15,7 @@ app:
secret_key: "NTYwYzUyY2NkMjg4ZmVkMDQ1ODU5ZWQxOGJmZmQ5NzM"
app_key_n: "NzgzYmJiNzI2NDQ1MWQ4Mg=="
secret_key_n: "MjY1MzU4M2M4ODczZGVhMjY4YWI5Mzg2OTE4YjFkNjU="
statistics: '{"appId":1,"platform":3,"version":"7.3.0","abtest":""}'
statistics: '{"appId":1,"platform":3,"version":"7.14.1","abtest":""}'
bili_i: # IOS
app_key: "MjdlYjUzZmM5MDU4ZjhjMw=="
secret_key: "YzJlZDUzYTc0ZWVlZmUzY2Y5OWZiZDAxZDhjOWMzNzU="

View File

@ -34,7 +34,7 @@ class ApiTask
* @var array|string[]
*/
protected static array $payload = [
'statistics' => '{"appId":1,"platform":3,"version":"7.3.0","abtest":""}',
'statistics' => '{"appId":1,"platform":3,"version":"7.14.1","abtest":""}',
];
/**

View File

@ -34,7 +34,7 @@ class ApiTask
* @var array|string[]
*/
protected static array $payload = [
'statistics' => '{"appId":1,"platform":3,"version":"7.3.0","abtest":""}',
'statistics' => '{"appId":1,"platform":3,"version":"7.14.1","abtest":""}',
];
/**

View File

@ -43,5 +43,4 @@ class ApiDynamicSvr
}
}

View File

@ -60,7 +60,7 @@ class ApiLogin
// $payload = [
// 'cid' => $cid,
// 'tel' => $phone,
// 'statistics' => '{"appId":1,"platform":3,"version":"7.3.0","abtest":""}',
// 'statistics' => '{"appId":1,"platform":3,"version":"7.14.1","abtest":""}',
// ];
// {"code":0,"message":"0","ttl":1,"data":{"is_new":false,"captcha_key":"4e292933816755442c1568e2043b8e41","recaptcha_url":""}}
// {"code":0,"message":"0","ttl":1,"data":{"is_new":false,"captcha_key":"","recaptcha_url":"https://www.bilibili.com/h5/project-msg-auth/verify?ct=geetest\u0026recaptcha_token=ad520c3a4a3c46e29b1974d85efd2c4b\u0026gee_gt=1c0ea7c7d47d8126dda19ee3431a5f38\u0026gee_challenge=c772673050dce482b9f63ff45b681ceb\u0026hash=ea2850a43cc6b4f1f7b925d601098e5e"}}

View File

@ -35,7 +35,7 @@ class ApiEvent
* @var array|string[]
*/
protected static array $payload = [
'statistics' => '{"appId":1,"platform":3,"version":"7.3.0","abtest":""}',
'statistics' => '{"appId":1,"platform":3,"version":"7.14.1","abtest":""}',
];
/**

View File

@ -72,5 +72,18 @@ class ApiVideo
return Request::getJson(true, 'other', $url, $payload);
}
/**
* 首页填充物 max=30
* @param int $ps
* @return array
*/
public static function topFeedRCMD(int $ps = 30): array
{
$url = 'https://api.bilibili.com/x/web-interface/wbi/index/top/feed/rcmd';
$payload = [
'ps' => $ps,
];
return Request::getJson(true, 'other', $url, $payload);
}
}
}

View File

@ -36,7 +36,21 @@ class ArrayR
}
/**
* 随机部分切片
* @param array $arr
* @param int $num
* @param bool $random
* @return array
*/
public static function toSlice(array $arr, int $num, bool $random = true): array
{
if ($random) {
//shuffle 将数组顺序随即打乱
shuffle($arr);
}
//array_slice 取该数组中的某一段
return array_slice($arr, 0, $num);
}
}