Skip to content

Commit

Permalink
feat: add bibibili bangumi support
Browse files Browse the repository at this point in the history
Now you can display your bibibili bangumi status, note: the api is
unofficial, so may be abandoned in the future.
issue #209
  • Loading branch information
spirit1431007 committed Apr 1, 2020
1 parent 7437bfa commit 4210f97
Show file tree
Hide file tree
Showing 11 changed files with 582 additions and 195 deletions.
56 changes: 56 additions & 0 deletions inc/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
'methods' => 'GET',
'callback' => 'get_qq_avatar',
));
register_rest_route('sakura/v1', '/bangumi/bilibili', array(
'methods' => 'POST',
'callback' => 'bgm_bilibili',
));
});

/**
Expand Down Expand Up @@ -422,3 +426,55 @@ function get_qq_avatar(){
}
}
}


function get_the_bgm_items($page = 1){
$cookies = akina_option('bilibili_cookie');
$url = 'https://api.bilibili.com/x/space/bangumi/follow/list?type=1&pn=' . $page . '&ps=15&follow_status=0&vmid=' . akina_option('bilibili_id');
$args = array(
'headers' => array(
'Cookie' => $cookies,
'Host' => 'api.bilibili.com',
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97'
)
);
$response = wp_remote_get($url, $args);
$bgmdata = json_decode($response["body"])->data;
return json_encode($bgmdata);
}

function get_bgm_items($page = 1){
$bgm = json_decode(get_the_bgm_items($page),true);
$totalpage = $bgm["total"] / 15;
if($totalpage - $page < 0){
$next = '<span>共追番' . $bgm["total"] .'部,继续加油吧!٩(ˊᗜˋ*)و</span>';
}else{
$next = '<a class="bangumi-next" href="' . rest_url('sakura/v1/bangumi/bilibili') . '?page=' . ++$page . '"><i class="fa fa-bolt" aria-hidden="true"></i> NEXT </a>';
}
$lists = $bgm["list"];
foreach ((array)$lists as $list) {
preg_match('/看到第(\d+)话/m',$list['progress'], $matches);
$progress = is_numeric($matches[1]) ? $matches[1] : 0;
$html .= '<div class="column">
<a class="bangumi-item" href="https://bangumi.bilibili.com/anime/' . $list['season_id'] . '/" target="_blank" rel="nofollow">
<img class="bangumi-image" src="' . str_replace('http://', 'https://', $list['cover']) . '"/>
<div class="bangumi-info">
<h3 class="bangumi-title" title="' . $list['title'] . '">' . $list['title'] . '</h2>
<div class="bangumi-summary"> '. $list['evaluate'] .' </div>
<div class="bangumi-status">
<div class="bangumi-status-bar" style="width: '. $progress / $list['total_count'] * 100 .'%"></div>
<p>' . $list['new_ep']['index_show'] . '</p>
</div>
</div>
</a>
</div>';
}
$html .= '</div><br><div id="bangumi-pagination">' . $next .'</div>';
return $html;
}

function bgm_bilibili(){
$page = $_GET["page"] ?: 2;
$html = preg_replace("/\s+|\n+|\r/", ' ', get_bgm_items($page));
echo $html;
}
21 changes: 21 additions & 0 deletions js/sakura-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mashiro_global.ini = new function () {
coverVideoIni();
checkskinSecter();
scrollBar();
load_bangumi();
}
this.pjax = function () { // pjax reload functions (pjax 重载函数)
pjaxInit();
Expand All @@ -25,6 +26,7 @@ mashiro_global.ini = new function () {
copy_code_block();
coverVideoIni();
checkskinSecter();
load_bangumi();
}
}

Expand Down Expand Up @@ -1011,6 +1013,22 @@ setTimeout(function () {
activate_widget();
}, 100);

function load_bangumi() {
$('body').on('click', '#bangumi-pagination a', function () {
$("#bangumi-pagination a").addClass("loading").text("");
var xhr = new XMLHttpRequest();
xhr.open('POST', this.href, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
$("#bangumi-pagination").remove();
$(".row").append(xhr.responseText);
}
};
xhr.send();
return false;
});
}

mashiro_global.ini.normalize();
loadCSS(mashiro_option.jsdelivr_css_src);
loadCSS(mashiro_option.entry_content_theme_src);
Expand Down Expand Up @@ -1796,6 +1814,9 @@ var home = location.href,
pc_to_top.onclick = function() {
topFunction();
}
mb_to_top.onclick = function() {
topFunction();
}
}
}
$(function () {
Expand Down
Binary file modified languages/en_US.mo
Binary file not shown.

0 comments on commit 4210f97

Please sign in to comment.