Skip to content

Commit

Permalink
Merge pull request #498 from cocdeshijie/preview
Browse files Browse the repository at this point in the history
哔哩哔哩收藏模板
  • Loading branch information
mirai-mamori committed Apr 30, 2022
2 parents 97c8f6e + db3726c commit a26a954
Show file tree
Hide file tree
Showing 16 changed files with 1,068 additions and 578 deletions.
33 changes: 30 additions & 3 deletions inc/api.php
Expand Up @@ -2,8 +2,8 @@
/**
* @Author: fuukei
* @Date: 2022-03-13 18:16:15
* @Last Modified by: bymoye
* @Last Modified time: 2022-03-27 00:47:46
* @Last Modified by: cocdeshijie
* @Last Modified time: 2022-04-16 13:27:30
*/


Expand All @@ -16,7 +16,8 @@
include_once('classes/Images.php');
include_once('classes/QQ.php');
include_once('classes/Captcha.php');
include_once ('classes/MyAnimeList.php');
include_once('classes/MyAnimeList.php');
include_once('classes/BilibiliFavList.php');
use Sakura\API\Images;
use Sakura\API\QQ;
use Sakura\API\Cache;
Expand Down Expand Up @@ -57,6 +58,10 @@
'methods' => 'POST',
'callback' => 'bgm_bilibili',
));
register_rest_route('sakura/v1', '/favlist/bilibili', array(
'methods' => 'POST',
'callback' => 'favlist_bilibili',
));
register_rest_route('sakura/v1', '/meting/aplayer', array(
'methods' => 'GET',
'callback' => 'meting_aplayer',
Expand Down Expand Up @@ -275,9 +280,31 @@ function bgm_bilibili() {
$html = preg_replace("/\s+|\n+|\r/", ' ', $bgm->get_bgm_items($page));
$response = new WP_REST_Response($html, 200);
}
$page = $_GET["page"] ?: 2;
$bgm = new \Sakura\API\Bilibili();
$html = preg_replace("/\s+|\n+|\r/", ' ', $bgm->get_bgm_items($page));
$response = new WP_REST_Response($html, 200);
return $response;
}

function favlist_bilibili() {
if (!check_ajax_referer('wp_rest', '_wpnonce', false)) {
$output = array(
'status' => 403,
'success' => false,
'message' => 'Unauthorized client.'
);
$response = new WP_REST_Response($output, 403);
} else {
$page = $_GET["page"] ?: 2;
$folder_id = $_GET["folder_id"];
$bgm = new \Sakura\API\BilibiliFavList();
$html = preg_replace("/\s+|\n+|\r/", ' ', $bgm->load_folder_items($folder_id, $page));
$response = new WP_REST_Response($html, 200);
}
return $response;
}

function meting_aplayer() {
$type = $_GET['type'];
$id = $_GET['id'];
Expand Down
114 changes: 114 additions & 0 deletions inc/classes/BilibiliFavList.php
@@ -0,0 +1,114 @@
<?php

namespace Sakura\API;

class BilibiliFavList
{
private $uid;

public function __construct()
{
$this->uid = iro_opt('bilibili_id');
}

function fetch_folder_api()
{
$uid = $this->uid;
$url = "https://api.bilibili.com/x/v3/fav/folder/created/list-all?up_mid=$uid&jsonp=jsonp";
$args = array(
'headers' => array(
'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);
if(is_array($response)){
return json_decode($response["body"], true);
}else{
return false;
}
}

function fetch_folder_item_api(int $folder_id, int $page)
{
$url = "https://api.bilibili.com/x/v3/fav/resource/list?media_id=$folder_id&pn=$page&ps=9&platform=web&jsonp=jsonp";
$args = array(
'headers' => array(
'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);
if(is_array($response)){
return json_decode($response["body"], true);
}else{
return false;
}
}

public function get_folders()
{
$resp = $this->fetch_folder_api();
if ($resp === false)
{
return "<div>" . __('Backend error', 'sakurairo') . "</div>";
}
$folders_data = $resp['data'];
$folders = $folders_data['list'];
$html = '';
foreach ((array)$folders as $folder){
$html .= BilibiliFavList::folder_display($folder['id']);
}
return $html;
}

public function folder_display(int $folder_id)
{
$folder_resp = $this->fetch_folder_item_api($folder_id, 1);
$folder_content_info = $folder_resp['data']['info'];
$html = '<div class="folder"><div class="folder-top">'.
lazyload_img(str_replace('http://', 'https://', $folder_content_info['cover']),'folder-img',array('alt'=>$folder_content_info['title'])).
'<div class="folder-detail"><h3>' . $folder_content_info['title'] . '</h3>'.
'<p>' . __('Item count: ', 'sakurairo') . $folder_content_info['media_count'] . '</p>'.
'<button class="expand-button">' . __('Expand', 'sakurairo') . '</button></div></div>'.
'<hr><div class="folder-content">';
$load = BilibiliFavList::load_more(rest_url('sakura/v1/favlist/bilibili') . '?page=1' . '&folder_id=' . $folder_id);
return $html . $load . '</div></div></br>';
}

public function load_folder_items(int $folder_id, $page = 1)
{
$folder_resp = $this->fetch_folder_item_api($folder_id, $page);
$folder_content = $folder_resp['data']['medias'];
$html = '';
foreach ((array)$folder_content as $item) {
$html .= BilibiliFavList::folder_item_display($item);
}
if ($folder_resp['data']['has_more']){
$load = BilibiliFavList::load_more(rest_url('sakura/v1/favlist/bilibili') . '?page=' . ++$page . '&folder_id=' . $folder_id);
}else{
$load = '<a class="load-more"><i class="fa fa-ban" aria-hidden="true"></i>' . __('All item has been loaded.', 'sakurairo') . '</a>';
}
return $html . $load;
}

private static function folder_item_display(array $item)
{
if ($item['type'] == 24){
$link = $item['link'];
}else{
$link = "https://www.bilibili.com/video/" . $item['bvid'];
}
// TODO: add lazyload to item-image with typescript
return '<div class="column"><a class="folder-item" href="' . $link . '" target="_blank" rel="nofollow">'.
'<img class="item-image" src="' . $item['cover'] . '">'.
'<div class="item-info"><h3 class="item-title" title="' . $item['title'] . '">' . $item['title'] . '</h3>'.
'<div class="item-intro" title="' . $item['intro'] . '">' . $item['intro'] . '</div>'.
'</div></a></div>';
}

private static function load_more($href)
{
return '<a class="load-more" data-href="' . $href . '"><i class="fa fa-bolt" aria-hidden="true"></i>' . __('Load More', 'sakurairo') . '</a>';
}
}
11 changes: 11 additions & 0 deletions inc/classes/MyAnimeList.php
Expand Up @@ -23,6 +23,17 @@ function get_data()
{
$username = $this->username;
$sort = $this->sort;
switch ($sort) {
case 1: // Status and Last Updated
$sort = 'order=16&order2=5&status=7';
break;
case 2: // Last Updated
$sort = 'order=5&status=7';
break;
case 3: // Status
$sort = 'order=16&status=7';
break;
}
$url = "https://myanimelist.net/animelist/$username/load.json?$sort";
$args = array(
'headers' => array(
Expand Down

0 comments on commit a26a954

Please sign in to comment.