1
0

Bilibili.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace Sakura\API;
  3. class Bilibili
  4. {
  5. private $uid;
  6. private $cookies;
  7. public function __construct()
  8. {
  9. $this->uid = iro_opt('bilibili_id');
  10. $this->cookies = iro_opt('bilibili_cookie');
  11. }
  12. /**
  13. * 获取Bilibili用户追番列表
  14. * @param integer $type
  15. * @param integer $page 页数
  16. * @author siroi <mrgaopw@hotmail.com>
  17. * @author KotoriK
  18. */
  19. function fetch_api(int $type, int $page = 1)
  20. {
  21. $uid = $this->uid;
  22. $cookies = $this->cookies;
  23. $url = "https://api.bilibili.com/x/space/bangumi/follow/list?type=$type&pn=$page&ps=15&follow_status=0&vmid=$uid";
  24. $args = array(
  25. 'headers' => array(
  26. 'Cookie' => $cookies,
  27. 'Host' => 'api.bilibili.com',
  28. 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97'
  29. )
  30. );
  31. $response = wp_remote_get($url, $args);
  32. if(is_array($response)){
  33. return json_decode($response["body"], true);
  34. }else{
  35. return array('code'=>-1);
  36. }
  37. }
  38. public function get_bgm_items($page = 1)
  39. {
  40. $resp = $this->fetch_api(1, $page);
  41. $code = $resp["code"];
  42. switch ($code) {
  43. case -1://指示在网络请求阶段发生了错误
  44. return "<div>" . __('Backend error', 'sakurairo') . "</div>";
  45. case 0: {
  46. $bgm = $resp['data'];
  47. $totalpage = $bgm["total"] / 15;
  48. if ($totalpage - $page < 0) {
  49. $next = "<span>" . __('Following ', 'sakurairo') . $bgm["total"] . __(' anime.', 'sakurairo') . "</span>";
  50. } else {
  51. $next = Bilibili::anchor_pagination_next(rest_url('sakura/v1/bangumi/bilibili') . '?page=' . ++$page);
  52. }
  53. $lists = $bgm["list"];
  54. $html = "";
  55. foreach ((array)$lists as $item) {
  56. $percent = Bilibili::get_percent($item);
  57. $html .= Bilibili::bangumi_item($item, $percent);
  58. }
  59. $html .= '</div><br><div id="bangumi-pagination">' . $next . '</div>';
  60. return $html;
  61. }
  62. case 53013: //用户隐私设置未公开
  63. //TODO:制作错误页面
  64. return "<div>" . __('The author seems to have hidden their bangumi list.', 'sakurairo') . "</div>";
  65. }
  66. }
  67. public function get_bfv_items($page = 1)
  68. {
  69. $resp = $this->fetch_api(2, $page);
  70. $code = $resp["code"];
  71. switch ($code) {
  72. case 0: {
  73. $bgm = $resp['data'];
  74. $totalpage = $bgm["total"] / 15;
  75. if ($totalpage - $page < 0) {
  76. $next = '<span>共追剧' . $bgm["total"] . '部,继续加油吧!٩(ˊᗜˋ*)و</span>';
  77. } else {
  78. $next = Bilibili::anchor_pagination_next(rest_url('sakura/v1/bangumi/bilibili-ctp') . '?page=' . ++$page);
  79. }
  80. $lists = $bgm["list"];
  81. $html = "";
  82. foreach ((array)$lists as $item) {
  83. $percent = Bilibili::get_percent($item);
  84. $html .= Bilibili::bangumi_item($item, $percent);
  85. }
  86. $html .= '</div><br><div id="bangumi-pagination">' . $next . '</div>';
  87. return $html;
  88. }
  89. }
  90. }
  91. private static function anchor_pagination_next(string $href)
  92. {
  93. return '<a class="bangumi-next" data-href="' . $href . '"><i class="fa fa-bolt" aria-hidden="true"></i> NEXT </a>';
  94. }
  95. private static function bangumi_item(array $item, $percent)
  96. {
  97. //in_array('index_show','new_ep')
  98. return '<div class="column">' .
  99. '<a class="bangumi-item" href="https://bangumi.bilibili.com/anime/' . $item['season_id'] . '/" target="_blank" rel="nofollow">'
  100. .lazyload_img(str_replace('http://', 'https://', $item['cover']),'bangumi-image',array('alt'=>$item['title'])).
  101. '<div class="bangumi-info">' .
  102. '<h3 class="bangumi-title" title="' . $item['title'] . '">' . $item['title'] . '</h2>'
  103. . '<div class="bangumi-summary"> ' . $item['evaluate'] . ' </div>' .
  104. '<div class="bangumi-status">'
  105. . '<div class="bangumi-status-bar" style="width: ' . $percent . '%"></div>'
  106. . '<p>' . ($item['new_ep']['index_show'] ?? ''). '</p>'
  107. . '</div>'
  108. . '</div>'
  109. . '</a>'
  110. . '</div>';
  111. }
  112. private static function get_percent(array $item)
  113. {
  114. $percent = 0;
  115. if (preg_match('/看完/m', $item["progress"], $matches_finish)) {
  116. $percent = 100;
  117. } else {
  118. preg_match('/第(\d+)./m', $item['progress'], $matches_progress);
  119. preg_match('/第(\d+)./m', $item["new_ep"]['index_show'] ?? null, $matches_new);
  120. if (isset($matches_progress[1])) {
  121. $progress = is_numeric($matches_progress[1]) ? $matches_progress[1] : 0;
  122. } else {
  123. $progress = 0;
  124. }
  125. $total = (isset($matches_new[1]) && is_numeric($matches_new[1])) ? $matches_new[1] : $item['total_count'];
  126. if ($total < 0) {
  127. //电影类剧集$total可能得到0
  128. $percent = 0;
  129. } else {
  130. $percent = $progress / $total * 100;
  131. }
  132. }
  133. return $percent;
  134. }
  135. }