Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

追番页添加bangumi支持 #1031

Merged
merged 10 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions inc/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,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',
Expand Down
134 changes: 134 additions & 0 deletions inc/classes/bangumi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

namespace Sakura\API;

class BangumiAPI
{
private $apiUrl = 'https://api.bgm.tv';
private $userID;
private $collectionApi;

public function __construct($userID)
{
if (empty($userID)) {
throw new \InvalidArgumentException('User ID is required.');
}

$this->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' => '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);
}

return json_encode(['error' => is_wp_error($response) ? $response->get_error_message() : 'An unknown error occurred.']);
}
}

class BangumiList
{
public function get_bgm_items($userID, $page = 1)
{
if (empty($userID)) {
return '<p>' . __('Bangumi ID not set.', 'sakurairo') . '</p>';
}

try {
$bgmAPI = new BangumiAPI($userID);
$collections = $bgmAPI->getCollections(true, true);

if (empty($collections)) {
return '<p>' . __('No data', 'sakurairo') . '</p>';
}

$total = count($collections); // 总条目数
$perPage = 12; // 每页条目数
$totalPages = ceil($total / $perPage); // 总页数
$offset = ($page - 1) * $perPage;
$collections = array_slice($collections, $offset, $perPage); // 当前页数据

$html = '';
foreach ($collections as $item) {
$html .= '<div class="column">';
$html .= '<a class="bangumi-item" href="' . esc_url($item['url']) . '" target="_blank" rel="nofollow">';
$html .= '<img class="lazyload bangumi-image" data-src="' . esc_url($item['images']) . '" alt="' . esc_attr($item['name']) . '" onerror="imgError(this)" src="' . esc_url($item['images']) . '">';
$html .= '<noscript><img class="bangumi-image" src="' . esc_url($item['images']) . '" alt="' . esc_attr($item['name']) . '"></noscript>';
$html .= '<div class="bangumi-info">';
$html .= '<h3 class="bangumi-title" title="' . esc_attr($item['name_cn'] ?: $item['name']) . '">' . esc_html($item['name_cn'] ?: $item['name']) . '</h3>';
$html .= '<div class="bangumi-date">' . __('上映日期: ', 'sakurairo') . esc_html($item['date']) . '</div>';
$html .= '<div class="bangumi-status">';
$html .= '<div class="bangumi-status-bar" style="width: ' . esc_attr(($item['ep_status'] / $item['eps']) * 100) . '%"></div>';
$html .= '<p>' . __('已观看集数: ', 'sakurairo') . esc_html($item['ep_status'] . '/' . $item['eps']) . '</p>';
$html .= '<div class="bangumi-summary">' . esc_html($item['summary'] ?: __('No introduction yet', 'sakurairo')) . '</div>';
$html .= '</div></div></a></div>';
}

// 分页
if ($page < $totalPages) {
$nextPageUrl = rest_url('sakura/v1/bangumi') . '?userID=' . urlencode($userID) . '&page=' . ($page + 1);
$html .= '<div id="bangumi-pagination">' . self::anchor_pagination_next($nextPageUrl) . '</div>';
}

return $html;
} catch (\Exception $e) {
return '<p>' . __('An error occured: ', 'sakurairo') . esc_html($e->getMessage()) . '</p>';
}
}

private static function anchor_pagination_next(string $href)
{
return '<a class="bangumi-next" data-href="' . esc_url($href) . '"><i class="fa-solid fa-bolt-lightning"></i> ' . __('Load more...', 'sakurairo') . '</a>';
}
}
10 changes: 10 additions & 0 deletions opt/options/theme-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
),
Expand Down Expand Up @@ -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' => '944883'
),

array(
'type' => 'subheading',
'content' => __('Friend Link Template Settings','sakurairo_csf'),
Expand Down
85 changes: 48 additions & 37 deletions user/page-bangumi.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
Template Name: 追番模板
* Template Name: 追番模板
*/
get_header();
?>
Expand All @@ -12,42 +12,53 @@
</style>
</head>

<?php while(have_posts()) : the_post(); ?>
<?php if(!iro_opt('patternimg') || !get_post_thumbnail_id(get_the_ID())) { ?>
<span class="linkss-title"><?php the_title();?></span>
<?php } ?>
<article <?php post_class("post-item"); ?>>
<?php the_content( '', true ); ?>
<section class="bangumi">
<?php if (iro_opt('bangumi_source') == 'bilibili'):?>
<?php if (iro_opt('bilibili_id') ):?>
<div class="row">
<?php
$bgm = new \Sakura\API\Bilibili();
echo $bgm->get_bgm_items();
?>
<?php else: ?>
<div class="row">
<p> <?php _e("Please fill in the Bilibili UID in Sakura Options.","sakurairo"); ?></p>
</div>
<?php endif; ?>
<?php else:?>
<?php if (iro_opt('my_anime_list_username') ):?>
<div class="row">
<?php
$bgm = new \Sakura\API\MyAnimeList();
echo $bgm->get_all_items();
?>
</div>
<?php else: ?>
<div class="row">
<p> <?php _e("Please fill in the My Anime List Username in Sakura Options.","sakurairo"); ?></p>
</div>
<?php endif; ?>
<?php endif; ?>
</section>
</article>
<?php while (have_posts()) : the_post(); ?>
<?php
if (!iro_opt('patternimg') || !get_post_thumbnail_id(get_the_ID())) {
?>
<span class="linkss-title"><?php the_title(); ?></span>
<?php
}
?>

<article <?php post_class("post-item"); ?>>
<?php the_content('', true); ?>
<section class="bangumi">
<?php
$bangumi_source = iro_opt('bangumi_source');
$bilibili_id = iro_opt('bilibili_id');
$mal_username = iro_opt('my_anime_list_username');
$bangumi_id = iro_opt('bangumi_id');
?>

<div class="row">
<?php
if ($bangumi_source === 'bilibili') {
if ($bilibili_id) {
$bgm = new \Sakura\API\Bilibili();
echo $bgm->get_bgm_items();
} else {
echo '<p>' . __("Please fill in the Bilibili UID in Sakura Options.", "sakurairo") . '</p>';
}
} elseif ($bangumi_source === 'bangumi') {
if (!empty($bangumi_id)) {
$bgmList = new \Sakura\API\BangumiList();
echo $bgmList->get_bgm_items($bangumi_id);
} else {
echo '<p>' . __("Please fill in the Bangumi UID in Sakura Options.", "sakurairo") . '</p>';
}
} elseif ($mal_username) {
$bgm = new \Sakura\API\MyAnimeList();
echo $bgm->get_all_items();
} else {
echo '<p>' . __("Please fill in the My Anime List Username in Sakura Options.", "sakurairo") . '</p>';
}
?>
</div>
</section>
</article>
<?php endwhile; ?>

<?php
get_footer();
get_footer();
?>