From 3fdf4c25de24b0c3ffb432911319088b385d8aef Mon Sep 17 00:00:00 2001 From: Lkeme <19500576+lkeme@users.noreply.github.com> Date: Fri, 27 Jan 2023 15:01:20 +0800 Subject: [PATCH] [fix] Return value must be of type array, fixes #204 --- plugin/MainSite/MainSite.php | 87 +++++++++---------- profile/example/device/device.yaml | 6 +- src/Api/Api/Pgc/Activity/Deliver/ApiTask.php | 2 +- src/Api/Api/Pgc/Activity/Score/ApiTask.php | 2 +- src/Api/DynamicSvr/ApiDynamicSvr.php | 3 +- src/Api/Passport/ApiLogin.php | 2 +- .../Api/Activity/Fire/Common/ApiEvent.php | 2 +- src/Api/Video/ApiVideo.php | 15 +++- src/Util/ArrayR/ArrayR.php | 20 ++++- 9 files changed, 81 insertions(+), 58 deletions(-) diff --git a/plugin/MainSite/MainSite.php b/plugin/MainSite/MainSite.php index f440b64..dcf4cea 100644 --- a/plugin/MainSite/MainSite.php +++ b/plugin/MainSite/MainSite.php @@ -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; } diff --git a/profile/example/device/device.yaml b/profile/example/device/device.yaml index 4227f1e..cee49c3 100644 --- a/profile/example/device/device.yaml +++ b/profile/example/device/device.yaml @@ -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=" diff --git a/src/Api/Api/Pgc/Activity/Deliver/ApiTask.php b/src/Api/Api/Pgc/Activity/Deliver/ApiTask.php index d956633..56bd814 100644 --- a/src/Api/Api/Pgc/Activity/Deliver/ApiTask.php +++ b/src/Api/Api/Pgc/Activity/Deliver/ApiTask.php @@ -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":""}', ]; /** diff --git a/src/Api/Api/Pgc/Activity/Score/ApiTask.php b/src/Api/Api/Pgc/Activity/Score/ApiTask.php index 8050922..7c2f2cd 100644 --- a/src/Api/Api/Pgc/Activity/Score/ApiTask.php +++ b/src/Api/Api/Pgc/Activity/Score/ApiTask.php @@ -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":""}', ]; /** diff --git a/src/Api/DynamicSvr/ApiDynamicSvr.php b/src/Api/DynamicSvr/ApiDynamicSvr.php index 6acc7ee..5beaf81 100644 --- a/src/Api/DynamicSvr/ApiDynamicSvr.php +++ b/src/Api/DynamicSvr/ApiDynamicSvr.php @@ -43,5 +43,4 @@ class ApiDynamicSvr } } - - \ No newline at end of file + diff --git a/src/Api/Passport/ApiLogin.php b/src/Api/Passport/ApiLogin.php index ddd7a24..1efa683 100644 --- a/src/Api/Passport/ApiLogin.php +++ b/src/Api/Passport/ApiLogin.php @@ -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"}} diff --git a/src/Api/Show/Api/Activity/Fire/Common/ApiEvent.php b/src/Api/Show/Api/Activity/Fire/Common/ApiEvent.php index 0641ce3..3f8633d 100644 --- a/src/Api/Show/Api/Activity/Fire/Common/ApiEvent.php +++ b/src/Api/Show/Api/Activity/Fire/Common/ApiEvent.php @@ -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":""}', ]; /** diff --git a/src/Api/Video/ApiVideo.php b/src/Api/Video/ApiVideo.php index ddc8d56..a1ff6eb 100644 --- a/src/Api/Video/ApiVideo.php +++ b/src/Api/Video/ApiVideo.php @@ -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); + } -} \ No newline at end of file +} diff --git a/src/Util/ArrayR/ArrayR.php b/src/Util/ArrayR/ArrayR.php index 1127967..d1c3add 100644 --- a/src/Util/ArrayR/ArrayR.php +++ b/src/Util/ArrayR/ArrayR.php @@ -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); + } } - - \ No newline at end of file +