1
0

BilibiliFavList.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Sakura\API;
  3. class BilibiliFavList
  4. {
  5. private $uid;
  6. public function __construct()
  7. {
  8. $this->uid = iro_opt('bilibili_id');
  9. }
  10. function fetch_folder_api()
  11. {
  12. $uid = $this->uid;
  13. $url = "https://api.bilibili.com/x/v3/fav/folder/created/list-all?up_mid=$uid&jsonp=jsonp";
  14. $args = array(
  15. 'headers' => array(
  16. 'Host' => 'api.bilibili.com',
  17. 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97'
  18. )
  19. );
  20. $response = wp_remote_get($url, $args);
  21. if(is_array($response)){
  22. return json_decode($response["body"], true) || false;
  23. }else{
  24. return false;
  25. }
  26. }
  27. function fetch_folder_item_api(int $folder_id, int $page)
  28. {
  29. $url = "https://api.bilibili.com/x/v3/fav/resource/list?media_id=$folder_id&pn=$page&ps=9&platform=web&jsonp=jsonp";
  30. $args = array(
  31. 'headers' => array(
  32. 'Host' => 'api.bilibili.com',
  33. 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97'
  34. )
  35. );
  36. $response = wp_remote_get($url, $args);
  37. if(is_array($response)){
  38. return json_decode($response["body"], true) || false;
  39. }else{
  40. return false;
  41. }
  42. }
  43. public function get_folders()
  44. {
  45. $resp = $this->fetch_folder_api();
  46. if ($resp === false)
  47. {
  48. return "<div>" . __('Backend error', 'sakurairo') . "</div>";
  49. }
  50. $folders_data = $resp['data'];
  51. $folders = $folders_data['list'];
  52. $html = '';
  53. foreach ((array)$folders as $folder){
  54. $html .= BilibiliFavList::folder_display($folder['id']);
  55. }
  56. return $html;
  57. }
  58. public function folder_display(int $folder_id)
  59. {
  60. $folder_resp = $this->fetch_folder_item_api($folder_id, 1);
  61. $folder_content_info = $folder_resp['data']['info'];
  62. $html = '<div class="folder"><div class="folder-top">'.
  63. lazyload_img(str_replace('http://', 'https://', $folder_content_info['cover']),'folder-img',array('alt'=>$folder_content_info['title'])).
  64. '<div class="folder-detail"><h3>' . $folder_content_info['title'] . '</h3>'.
  65. '<p>' . __('Item count: ', 'sakurairo') . $folder_content_info['media_count'] . '</p>'.
  66. '<button class="expand-button">' . __('Expand', 'sakurairo') . '</button></div></div>'.
  67. '<hr><div class="folder-content">';
  68. $load = BilibiliFavList::load_more(rest_url('sakura/v1/favlist/bilibili') . '?page=1' . '&folder_id=' . $folder_id);
  69. return $html . $load . '</div></div></br>';
  70. }
  71. public function load_folder_items(int $folder_id, $page = 1)
  72. {
  73. $folder_resp = $this->fetch_folder_item_api($folder_id, $page);
  74. $folder_content = $folder_resp['data']['medias'];
  75. $html = '';
  76. foreach ((array)$folder_content as $item) {
  77. $html .= BilibiliFavList::folder_item_display($item);
  78. }
  79. if ($folder_resp['data']['has_more']){
  80. $load = BilibiliFavList::load_more(rest_url('sakura/v1/favlist/bilibili') . '?page=' . ++$page . '&folder_id=' . $folder_id);
  81. }else{
  82. $load = '<a class="load-more"><i class="fa fa-ban" aria-hidden="true"></i>' . __('All item has been loaded.', 'sakurairo') . '</a>';
  83. }
  84. return $html . $load;
  85. }
  86. private static function folder_item_display(array $item)
  87. {
  88. if ($item['type'] == 24){
  89. $link = $item['link'];
  90. }else{
  91. $link = "https://www.bilibili.com/video/" . $item['bvid'];
  92. }
  93. // TODO: add lazyload to item-image with typescript
  94. return '<div class="column"><a class="folder-item" href="' . $link . '" target="_blank" rel="nofollow">'.
  95. '<img class="item-image" src="' . $item['cover'] . '">'.
  96. '<div class="item-info"><h3 class="item-title" title="' . $item['title'] . '">' . $item['title'] . '</h3>'.
  97. '<div class="item-intro" title="' . $item['intro'] . '">' . $item['intro'] . '</div>'.
  98. '</div></a></div>';
  99. }
  100. private static function load_more($href)
  101. {
  102. return '<a class="load-more" data-href="' . $href . '"><i class="fa fa-bolt" aria-hidden="true"></i>' . __('Load More', 'sakurairo') . '</a>';
  103. }
  104. }