From 2525c2680976cefc9287ce42411e07cbdcf0905e Mon Sep 17 00:00:00 2001 From: Lkeme <19500576+lkeme@users.noreply.github.com> Date: Sat, 28 Jan 2023 23:30:31 +0800 Subject: [PATCH] [perf] MainSite support for cache --- plugin/MainSite/MainSite.php | 67 +++++++++++++++++++++++++++--- src/Api/Api/X/Player/ApiPlayer.php | 3 +- 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/plugin/MainSite/MainSite.php b/plugin/MainSite/MainSite.php index dcf4cea..f51f0dd 100644 --- a/plugin/MainSite/MainSite.php +++ b/plugin/MainSite/MainSite.php @@ -27,9 +27,15 @@ use Bhp\Plugin\Plugin; use Bhp\TimeLock\TimeLock; use Bhp\Util\ArrayR\ArrayR; use Bhp\Util\Exceptions\NoLoginException; +use Bhp\Cache\Cache; class MainSite extends BasePlugin { + /** + * @var array|array[] + */ + protected array $records = []; + /** * 插件信息 * @var array|string[] @@ -51,6 +57,8 @@ class MainSite extends BasePlugin { // TimeLock::initTimeLock(); + // + Cache::initCache(); // $this::class $plugin->register($this, 'execute'); } @@ -63,14 +71,32 @@ class MainSite extends BasePlugin public function execute(): void { if (TimeLock::getTimes() > time() || !getEnable('main_site')) return; + // 缓存开始 + $this->records = ($tmp = Cache::get('records')) ? $tmp : $this->initRecords(); // if ($this->watchTask() && $this->shareTask() && $this->coinTask()) { TimeLock::setTimes(TimeLock::timing(10)); } else { TimeLock::setTimes(3600); } + // 缓存结束 + Cache::set('records', $this->records); } + /** + * 初始化 + * @return array[] + */ + protected function initRecords(): array + { + return [ + 'watch' => [], + 'share' => [], + 'coin' => [], + ]; + } + + /** * 获取稿件列表 * @param int $num @@ -90,12 +116,15 @@ class MainSite extends BasePlugin /** * 投币任务 + * @param string $key * @return bool * @throws NoLoginException */ - protected function coinTask(): bool + protected function coinTask(string $key = 'coin'): bool { if (!getConf('main_site.add_coin', false, 'bool')) return true; + // + if (in_array($this->getKey(), $this->records[$key])) return true; // 预计数量 失败默认0 避免损失 $estimate_num = getConf('main_site.add_coin_num', 0, 'int'); // 库存数量 @@ -108,6 +137,8 @@ class MainSite extends BasePlugin // 上限 if ($actual_num <= 0) { Log::notice('主站任务: 今日投币上限已满'); + // 插入 + $this->records[$key][] = $this->getKey(); return true; } // 稿件列表 @@ -120,6 +151,8 @@ class MainSite extends BasePlugin // sleep(1); } + // 插入 + $this->records[$key][] = $this->getKey(); return true; } @@ -270,13 +303,16 @@ class MainSite extends BasePlugin /** * 分享任务 + * @param string $key * @return bool * @throws NoLoginException */ - protected function shareTask(): bool + protected function shareTask(string $key = 'share'): bool { if (!getConf('main_site.share', false, 'bool')) return true; - + // + if (in_array($this->getKey(), $this->records[$key])) return true; + // $archives = $this->fetchCustomArchives(10); $archive = array_pop($archives); $aid = (string)$archive['aid']; @@ -288,6 +324,8 @@ class MainSite extends BasePlugin throw new NoLoginException($response['message']); case 0: Log::notice("主站任务: $aid 分享成功"); + // 插入 + $this->records[$key][] = $this->getKey(); return true; default: Log::warning("主站任务: $aid 分享失败 {$response['code']} -> {$response['message']}"); @@ -297,12 +335,15 @@ class MainSite extends BasePlugin /** * 观看任务 + * @param string $key * @return bool */ - protected function watchTask(): bool + protected function watchTask(string $key = 'watch'): bool { if (!getConf('main_site.watch', false, 'bool')) return true; - + // + if (in_array($this->getKey(), $this->records[$key])) return true; + // $archives = $this->fetchCustomArchives(10); $archive = array_pop($archives); // @@ -311,6 +352,7 @@ class MainSite extends BasePlugin } else { // 额外处理信息 $info = $this->getArchiveInfo((string)$archive['aid']); + if (empty($info)) return false; } // $aid = (string)$info['aid']; @@ -342,6 +384,8 @@ class MainSite extends BasePlugin } // Log::notice("主站任务: $aid 观看成功"); + // 插入 + $this->records[$key][] = $this->getKey(); return true; } @@ -354,6 +398,10 @@ class MainSite extends BasePlugin { $response = ApiPlayer::pageList($aid); // + if ($response['code'] == -404 || !isset($response['data'])) { + Log::warning("主站任务: $aid 获取稿件信息失败 {$response['code']} -> {$response['message']}"); + return []; + } $archive_info = $response['data'][0]; $archive_info['aid'] = $aid; return $archive_info; @@ -376,4 +424,13 @@ class MainSite extends BasePlugin return $info; } + + /** + * @return string + */ + protected function getKey(): string + { + return substr(md5(md5(date("Y-m-d", time()))), 8, 8); + } + } diff --git a/src/Api/Api/X/Player/ApiPlayer.php b/src/Api/Api/X/Player/ApiPlayer.php index d2628cd..9d29e65 100644 --- a/src/Api/Api/X/Player/ApiPlayer.php +++ b/src/Api/Api/X/Player/ApiPlayer.php @@ -23,11 +23,12 @@ class ApiPlayer { public static function pageList(string $aid): array { - // day: 日榜1 三榜3 周榜7 月榜30 $url = 'https://api.bilibili.com/x/player/pagelist'; $payload = [ 'aid' => $aid, ]; + // {"code":-404,"message":"啥都木有","ttl":1} + // {"code":0,"message":"0","ttl":1,"data":[{"cid":123,"page":1,"from":"vupload","part":"","duration":2055,"vid":"","weblink":"","dimension":{"width":480,"height":360,"rotate":0}}]} return Request::getJson(true, 'other', $url, $payload); }