From d401865d638412168cabe48d4a6f2646a7560033 Mon Sep 17 00:00:00 2001 From: nicocatxzc Date: Mon, 23 Dec 2024 15:51:15 +0800 Subject: [PATCH 01/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0bangumi=E9=80=89?= =?UTF-8?q?=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- opt/options/theme-options.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/opt/options/theme-options.php b/opt/options/theme-options.php index 9b9a12e2..9d425a2c 100644 --- a/opt/options/theme-options.php +++ b/opt/options/theme-options.php @@ -2770,6 +2770,7 @@ function iro_validate_optional_url( $value ) { 'options' => array( 'bilibili' => 'https://s.nmxc.ltd/sakurairo_vision/@2.7/options/bangumi_tep_bili.webp', 'myanimelist' => 'https://s.nmxc.ltd/sakurairo_vision/@2.7/options/bangumi_tep_mal.webp', + 'bangumi' => 'https://s.nmxc.ltd/sakurairo_vision/@2.7/options/bangumi_tep_bangumi.webp' ), 'default' => 'bilibili' ), @@ -2814,6 +2815,15 @@ function iro_validate_optional_url( $value ) { 'default' => 'LIVE_BUVID=' ), + array( + 'id' => 'bangumi_id', + 'type' => 'text', + 'title' => __('Bangumi Account UID','sakurairo_csf'), + 'desc' => __('Fill in your Bangumi account ID, e.g. https://bangumi.tv/user/944883, just the number part "944883"','sakurairo_csf'), + 'dependency' => array( 'bangumi_source', '==', 'bangumi', '', 'true' ), + 'default' => '13972644' + ), + array( 'type' => 'subheading', 'content' => __('Friend Link Template Settings','sakurairo_csf'), From b27723dee5ca9717e6a68b3d6c1aa7351d9f6c37 Mon Sep 17 00:00:00 2001 From: nicocatxzc Date: Mon, 23 Dec 2024 15:51:42 +0800 Subject: [PATCH 02/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0bangumi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/api.php | 1 + 1 file changed, 1 insertion(+) diff --git a/inc/api.php b/inc/api.php index 81358698..28ca4579 100644 --- a/inc/api.php +++ b/inc/api.php @@ -18,6 +18,7 @@ include_once('classes/Captcha.php'); include_once('classes/MyAnimeList.php'); include_once('classes/BilibiliFavList.php'); +include_once('classes/bangumi.php'); use Sakura\API\Images; use Sakura\API\QQ; use Sakura\API\Cache; From e9ab527136ef20a9c3af46d2724585d0457fe54c Mon Sep 17 00:00:00 2001 From: nicocatxzc Date: Mon, 23 Dec 2024 15:52:07 +0800 Subject: [PATCH 03/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8E=B7=E5=8F=96bangu?= =?UTF-8?q?mi=E4=BF=A1=E6=81=AF=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/classes/bangumi.php | 131 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 inc/classes/bangumi.php diff --git a/inc/classes/bangumi.php b/inc/classes/bangumi.php new file mode 100644 index 00000000..88445b11 --- /dev/null +++ b/inc/classes/bangumi.php @@ -0,0 +1,131 @@ +userID = $userID; + $this->collectionApi = $this->apiUrl . '/v0/users/' . $this->userID . '/collections'; + } + + public function getCollections($isWatching = true, $isWatched = true) + { + $collDataArr = []; + + if ($isWatching) { + $collDataArr = array_merge($collDataArr, $this->fetchCollections(3)); + } + + if ($isWatched) { + $collDataArr = array_merge($collDataArr, $this->fetchCollections(2)); + } + + $collArr = []; + foreach ($collDataArr as $value) { + $collArr[] = [ + 'name' => $value['subject']['name'], + 'name_cn' => $value['subject']['name_cn'], + 'date' => $value['subject']['date'], + 'summary' => $value['subject']['short_summary'], + 'url' => 'https://bgm.tv/subject/' . $value['subject']['id'], + 'images' => $value['subject']['images']['large'] ?? '', + 'eps' => $value['subject']['eps'] ?? 0, + 'ep_status' => $value['ep_status'] ?? 0, + ]; + } + + return $collArr; + } + + private function fetchCollections($type) + { + $collOffset = 0; + $collDataArr = []; + + do { + $response = $this->http_get_contents($this->collectionApi . '?subject_type=2&type=' . $type . '&limit=50&offset=' . $collOffset); + $collData = json_decode($response, true); + + if (isset($collData['data'])) { + $collDataArr = array_merge($collDataArr, $collData['data']); + } + + $collOffset += 50; + } while (!empty($collData['data']) && $collOffset < ($collData['total'] ?? 0)); + + return $collDataArr; + } + + private function http_get_contents($url) + { + $response = wp_remote_get($url, ['user-agent' => 'fhyuncai/BangumiList/' . BGMLIST_VER . ' (WordPressPlugin)']); + if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) { + return wp_remote_retrieve_body($response); + } + + return json_encode(['error' => is_wp_error($response) ? $response->get_error_message() : 'An unknown error occurred.']); + } +} + +class BangumiList +{ + public function get_bgm_items($userID) + { + if (empty($userID)) { + return '

未配置 Bangumi ID

'; + } + + try { + $bgmAPI = new BangumiAPI($userID); + $collections = $bgmAPI->getCollections(); + + if (empty($collections)) { + return '

没有追番数据

'; + } + + $html = ''; +foreach ($collections as $item) { + //.column + $html .= ''; // .column +} + +return $html; + + + return $html; + } catch (\Exception $e) { + return '

发生错误: ' . esc_html($e->getMessage()) . '

'; + } + } +} From a13e41f4397f6600290d873347911d6e2c75d56a Mon Sep 17 00:00:00 2001 From: nicocatxzc Date: Mon, 23 Dec 2024 15:55:03 +0800 Subject: [PATCH 04/10] =?UTF-8?q?=E6=A2=B3=E7=90=86=E8=BF=BD=E7=95=AA?= =?UTF-8?q?=E9=A1=B5=E5=88=A4=E6=96=AD=EF=BC=8C=E6=B7=BB=E5=8A=A0bangumi?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- user/page-bangumi.php | 64 +++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/user/page-bangumi.php b/user/page-bangumi.php index 8d1d758e..3db961dd 100644 --- a/user/page-bangumi.php +++ b/user/page-bangumi.php @@ -1,7 +1,7 @@ @@ -12,41 +12,51 @@ - - - - -
> - -
- - -
+ + + + + +
> + +
+ + +
+ + get_bgm_items(); ?> - -
-

-
+ +

- - -
+ + get_all_items(); + $bgmList = new \Sakura\API\BangumiList(); + echo $bgmList->get_bgm_items($bangumi_id); ?> -
- -
-

-
+ +

+ + get_all_items(); + ?> + +

-
-
+
+
+
Date: Mon, 23 Dec 2024 16:14:49 +0800 Subject: [PATCH 05/10] =?UTF-8?q?=E6=94=B9=E7=94=A8{}=E5=88=A4=E6=96=AD?= =?UTF-8?q?=EF=BC=8C=E6=8F=90=E5=8D=87=E5=8F=AF=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- user/page-bangumi.php | 59 ++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/user/page-bangumi.php b/user/page-bangumi.php index 3db961dd..af079b90 100644 --- a/user/page-bangumi.php +++ b/user/page-bangumi.php @@ -13,9 +13,13 @@ - + - +
> @@ -28,36 +32,33 @@ ?>
- - - get_bgm_items(); - ?> - -

- - - - get_bgm_items($bangumi_id); - ?> - -

- - - get_all_items(); - ?> - -

- + get_bgm_items(); + } else { + echo '

' . __("Please fill in the Bilibili UID in Sakura Options.", "sakurairo") . '

'; + } + } elseif ($bangumi_source === 'bangumi') { + if (!empty($bangumi_id)) { + $bgmList = new \Sakura\API\BangumiList(); + echo $bgmList->get_bgm_items($bangumi_id); + } else { + echo '

' . __("Please fill in the Bangumi UID in Sakura Options.", "sakurairo") . '

'; + } + } elseif ($mal_username) { + $bgm = new \Sakura\API\MyAnimeList(); + echo $bgm->get_all_items(); + } else { + echo '

' . __("Please fill in the My Anime List Username in Sakura Options.", "sakurairo") . '

'; + } + ?>
From a147df905fbde1de00e7e67c1524352c41b353ab Mon Sep 17 00:00:00 2001 From: nicocatxzc Date: Mon, 23 Dec 2024 16:37:46 +0800 Subject: [PATCH 06/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0bangumi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/classes/bangumi.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/classes/bangumi.php b/inc/classes/bangumi.php index 88445b11..681acb20 100644 --- a/inc/classes/bangumi.php +++ b/inc/classes/bangumi.php @@ -70,7 +70,7 @@ private function fetchCollections($type) private function http_get_contents($url) { - $response = wp_remote_get($url, ['user-agent' => 'fhyuncai/BangumiList/' . BGMLIST_VER . ' (WordPressPlugin)']); + $response = wp_remote_get($url, ['user-agent' => 'mirai-mamori/Sakurairo/' . BGMLIST_VER . ' (WordPressTheme)']); if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) { return wp_remote_retrieve_body($response); } From 62784266fae90a87716c0b62145b088036913c2f Mon Sep 17 00:00:00 2001 From: nicocatxzc Date: Mon, 23 Dec 2024 16:45:54 +0800 Subject: [PATCH 07/10] =?UTF-8?q?=E6=8C=89bangumi=5Fapi=E8=A6=81=E6=B1=82?= =?UTF-8?q?=E7=BA=A0=E6=AD=A3UA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/bangumi/api/blob/master/docs-raw/user%20agent.md --- inc/classes/bangumi.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/inc/classes/bangumi.php b/inc/classes/bangumi.php index 681acb20..db17ca48 100644 --- a/inc/classes/bangumi.php +++ b/inc/classes/bangumi.php @@ -2,8 +2,6 @@ namespace Sakura\API; -define('BGMLIST_VER', '1.1.4'); - class BangumiAPI { private $apiUrl = 'https://api.bgm.tv'; @@ -70,7 +68,7 @@ private function fetchCollections($type) private function http_get_contents($url) { - $response = wp_remote_get($url, ['user-agent' => 'mirai-mamori/Sakurairo/' . BGMLIST_VER . ' (WordPressTheme)']); + $response = wp_remote_get($url, ['user-agent' => 'mirai-mamori/Sakurairo(https://github.com/mirai-mamori/Sakurairo):WordPressTheme']); if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) { return wp_remote_retrieve_body($response); } From c2ba77d1fa1bb15796de435893e931e59e7df5b5 Mon Sep 17 00:00:00 2001 From: nicocatxzc Date: Mon, 23 Dec 2024 17:01:24 +0800 Subject: [PATCH 08/10] =?UTF-8?q?=E7=BA=A0=E6=AD=A3=E4=B8=8A=E4=B8=8B?= =?UTF-8?q?=E6=96=87=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- opt/options/theme-options.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opt/options/theme-options.php b/opt/options/theme-options.php index 9d425a2c..4a4f4e58 100644 --- a/opt/options/theme-options.php +++ b/opt/options/theme-options.php @@ -2821,7 +2821,7 @@ function iro_validate_optional_url( $value ) { 'title' => __('Bangumi Account UID','sakurairo_csf'), 'desc' => __('Fill in your Bangumi account ID, e.g. https://bangumi.tv/user/944883, just the number part "944883"','sakurairo_csf'), 'dependency' => array( 'bangumi_source', '==', 'bangumi', '', 'true' ), - 'default' => '13972644' + 'default' => '944883' ), array( From 2a9396abbccda9e0eb249838661ab49e98d4d54c Mon Sep 17 00:00:00 2001 From: nicocatxzc Date: Mon, 23 Dec 2024 20:15:31 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E6=B3=A8=E5=86=8Cbangumi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/api.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/inc/api.php b/inc/api.php index 28ca4579..3c9a5f1c 100644 --- a/inc/api.php +++ b/inc/api.php @@ -75,6 +75,18 @@ 'permission_callback' => '__return_true' ) ); + register_rest_route('sakura/v1', '/bangumi', array( + 'methods' => 'POST', + 'callback' => function ($request) { + $userID = $request->get_param('userID'); + $page = $request->get_param('page') ?: 1; + + $bgmList = new \Sakura\API\BangumiList(); + return $bgmList->get_bgm_items($userID, (int)$page); + }, + 'permission_callback' => '__return_true' + ) + ); register_rest_route('sakura/v1', '/movies/bilibili', array( 'methods' => 'POST', 'callback' => 'bfv_bilibili', From 1c7cfa4b0e6dd54c39a13346cbc67198d395bdb4 Mon Sep 17 00:00:00 2001 From: nicocatxzc Date: Mon, 23 Dec 2024 20:18:31 +0800 Subject: [PATCH 10/10] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=88=86=E9=A1=B5?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/classes/bangumi.php | 67 ++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/inc/classes/bangumi.php b/inc/classes/bangumi.php index db17ca48..0897c8f3 100644 --- a/inc/classes/bangumi.php +++ b/inc/classes/bangumi.php @@ -79,51 +79,56 @@ private function http_get_contents($url) class BangumiList { - public function get_bgm_items($userID) + public function get_bgm_items($userID, $page = 1) { if (empty($userID)) { - return '

未配置 Bangumi ID

'; + return '

' . __('Bangumi ID not set.', 'sakurairo') . '

'; } try { $bgmAPI = new BangumiAPI($userID); - $collections = $bgmAPI->getCollections(); + $collections = $bgmAPI->getCollections(true, true); if (empty($collections)) { - return '

没有追番数据

'; + return '

' . __('No data', 'sakurairo') . '

'; } - $html = ''; -foreach ($collections as $item) { - //.column - $html .= ''; // .column -} + $total = count($collections); // 总条目数 + $perPage = 12; // 每页条目数 + $totalPages = ceil($total / $perPage); // 总页数 + $offset = ($page - 1) * $perPage; + $collections = array_slice($collections, $offset, $perPage); // 当前页数据 -return $html; + $html = ''; + foreach ($collections as $item) { + $html .= ''; + } + // 分页 + if ($page < $totalPages) { + $nextPageUrl = rest_url('sakura/v1/bangumi') . '?userID=' . urlencode($userID) . '&page=' . ($page + 1); + $html .= '
' . self::anchor_pagination_next($nextPageUrl) . '
'; + } return $html; } catch (\Exception $e) { - return '

发生错误: ' . esc_html($e->getMessage()) . '

'; + return '

' . __('An error occured: ', 'sakurairo') . esc_html($e->getMessage()) . '

'; } } -} + + private static function anchor_pagination_next(string $href) + { + return ' ' . __('Load more...', 'sakurairo') . ''; + } +} \ No newline at end of file