MyAnimeList.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace Sakura\API;
  3. class MyAnimeList
  4. {
  5. private $username;
  6. private $sort;
  7. public function __construct()
  8. {
  9. $this->username = iro_opt('my_anime_list_username');
  10. $this->sort = iro_opt('my_anime_list_sort');
  11. }
  12. /**
  13. * Get anime list with username from https://myanimelist.net/
  14. * @author siroi <mrgaopw@hotmail.com>
  15. * @author KotoriK
  16. * @author cocdeshijie <cocdeshijie@berkeley.edu>
  17. */
  18. function get_data()
  19. {
  20. $username = $this->username;
  21. $sort = $this->sort;
  22. switch ($sort) {
  23. case 1: // Status and Last Updated
  24. $sort = 'order=16&order2=5&status=7';
  25. break;
  26. case 2: // Last Updated
  27. $sort = 'order=5&status=7';
  28. break;
  29. case 3: // Status
  30. $sort = 'order=16&status=7';
  31. break;
  32. }
  33. $url = "https://myanimelist.net/animelist/$username/load.json?$sort";
  34. $args = array(
  35. 'headers' => array(
  36. 'Host' => 'myanimelist.net',
  37. 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97'
  38. )
  39. );
  40. $response = wp_remote_get($url, $args);
  41. if(is_array($response)){
  42. return json_decode($response["body"], true);
  43. }else{
  44. return false;
  45. }
  46. }
  47. public function get_all_items()
  48. {
  49. $resp = $this->get_data();
  50. if ($resp === false)
  51. {
  52. return "<div>" . __('Backend error', 'sakurairo') . "</div>";
  53. }
  54. else
  55. {
  56. $html = '';
  57. $item_count = count($resp);
  58. $total_episodes = 0;
  59. foreach ((array)$resp as $item)
  60. {
  61. $html .= MyAnimeList::get_item_details($item);
  62. $total_episodes += $item['num_watched_episodes'];
  63. }
  64. $top_info = '<br><div id="bangumi-pagination"><span>' .
  65. __('Following ', 'sakurairo') . $item_count . __(' anime.', 'sakurairo') .
  66. __(' Watched ', 'sakurairo') . $total_episodes . __(' episodes.', 'sakurairo') .
  67. '</span></div>';
  68. $html = $top_info . $html;
  69. return $html;
  70. }
  71. }
  72. private static function get_item_details(array $item)
  73. {
  74. return '<div class="column">' .
  75. '<a class="bangumi-item" href="https://myanimelist.net' . $item['anime_url'] . '/" target="_blank" rel="nofollow">'
  76. .lazyload_img(MyAnimeList::get_image($item['anime_image_path']),'bangumi-image',array('alt'=>$item['anime_title'])).
  77. '<div class="bangumi-info">' .
  78. '<h3 class="bangumi-title" title="' . $item['anime_title'] . '">' . $item['anime_title'] . '</h2>' .
  79. '<div class="bangumi-summary"> ' . $item['anime_title_eng'] . ' </div>' .
  80. '<div class="bangumi-status">' .
  81. MyAnimeList::bangumi_status($item) .
  82. '</div>' .
  83. '</div>' .
  84. '</a>' .
  85. '</div>';
  86. }
  87. private static function get_image(string $image_path)
  88. {
  89. preg_match('/\/anime(.*?)\./', $image_path, $output);
  90. return "https://cdn.myanimelist.net/images/anime/$output[1].jpg";
  91. }
  92. private static function bangumi_status(array $item)
  93. {
  94. switch($item['status'])
  95. {
  96. case 2: // Completed
  97. {
  98. return '<div class="bangumi-status-bar" style="width: 100%; background: #5cb85c;"></div>' .
  99. '<p>' .
  100. __('Finished ', 'sakurairo') . $item['num_watched_episodes'] . '/' . $item['anime_num_episodes'] .
  101. '</p>';
  102. }
  103. case 1: // Watching
  104. {
  105. return '<div class="bangumi-status-bar" style="width: '.
  106. MyAnimeList::status_percent($item) .
  107. '%; background: #0275d8;"></div>' .
  108. '<p>' .
  109. __('Watching ', 'sakurairo') . $item['num_watched_episodes'] . '/' . $item['anime_num_episodes'] .
  110. '</p>';
  111. }
  112. case 6: // Plan to watch
  113. {
  114. return '<div class="bangumi-status-bar" style="width: 100%; background: #969ea4;"></div>' .
  115. '<p>' .
  116. __('Planning to Watch ', 'sakurairo') .
  117. '</p>';
  118. }
  119. case 4: // Dropped
  120. {
  121. return '<div class="bangumi-status-bar" style="width: '.
  122. MyAnimeList::status_percent($item) .
  123. '%; background: #d9534f;"></div>' .
  124. '<p>' .
  125. __('Dropped ', 'sakurairo') . $item['num_watched_episodes'] . '/' . $item['anime_num_episodes'] .
  126. '</p>';
  127. }
  128. case 3: // On Hold
  129. {
  130. return '<div class="bangumi-status-bar" style="width: '.
  131. MyAnimeList::status_percent($item) .
  132. '%; background: #f0ad4e;"></div>' .
  133. '<p>' .
  134. __('Paused ', 'sakurairo') . $item['num_watched_episodes'] . '/' . $item['anime_num_episodes'] .
  135. '</p>';
  136. }
  137. default: // TODO: other possible status code
  138. {
  139. return '';
  140. }
  141. }
  142. }
  143. private static function status_percent(array $item)
  144. {
  145. if ($item['anime_num_episodes'] == 0)
  146. {
  147. return 0;
  148. }
  149. else
  150. {
  151. return ($item['num_watched_episodes']/$item['anime_num_episodes']) * 100;
  152. }
  153. }
  154. }