MyAnimeList.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. $item_count = count($resp);
  57. $total_episodes = 0;
  58. foreach ((array)$resp as $item)
  59. {
  60. $html .= MyAnimeList::get_item_details($item);
  61. $total_episodes += $item['num_watched_episodes'];
  62. }
  63. $top_info = '<br><div id="bangumi-pagination"><span>' .
  64. __('Following ', 'sakurairo') . $item_count . __(' anime.', 'sakurairo') .
  65. __(' Watched ', 'sakurairo') . $total_episodes . __(' episodes.', 'sakurairo') .
  66. '</span></div>';
  67. $html = $top_info . $html . '</div>';
  68. return $html;
  69. }
  70. }
  71. private static function get_item_details(array $item)
  72. {
  73. return '<div class="column">' .
  74. '<a class="bangumi-item" href="https://myanimelist.net' . $item['anime_url'] . '/" target="_blank" rel="nofollow">'
  75. .lazyload_img(MyAnimeList::get_image($item['anime_image_path']),'bangumi-image',array('alt'=>$item['anime_title'])).
  76. '<div class="bangumi-info">' .
  77. '<h3 class="bangumi-title" title="' . $item['anime_title'] . '">' . $item['anime_title'] . '</h2>' .
  78. '<div class="bangumi-summary"> ' . $item['anime_title_eng'] . ' </div>' .
  79. '<div class="bangumi-status">' .
  80. MyAnimeList::bangumi_status($item) .
  81. '</div>' .
  82. '</div>' .
  83. '</a>' .
  84. '</div>';
  85. }
  86. private static function get_image(string $image_path)
  87. {
  88. preg_match('/\/anime(.*?)\./', $image_path, $output);
  89. return "https://cdn.myanimelist.net/images/anime/$output[1].jpg";
  90. }
  91. private static function bangumi_status(array $item)
  92. {
  93. switch($item['status'])
  94. {
  95. case 2: // Completed
  96. {
  97. return '<div class="bangumi-status-bar" style="width: 100%; background: #5cb85c;"></div>' .
  98. '<p>' .
  99. __('Finished ', 'sakurairo') . $item['num_watched_episodes'] . '/' . $item['anime_num_episodes'] .
  100. '</p>';
  101. }
  102. case 1: // Watching
  103. {
  104. return '<div class="bangumi-status-bar" style="width: '.
  105. MyAnimeList::status_percent($item) .
  106. '%; background: #0275d8;"></div>' .
  107. '<p>' .
  108. __('Watching ', 'sakurairo') . $item['num_watched_episodes'] . '/' . $item['anime_num_episodes'] .
  109. '</p>';
  110. }
  111. case 6: // Plan to watch
  112. {
  113. return '<div class="bangumi-status-bar" style="width: 100%; background: #969ea4;"></div>' .
  114. '<p>' .
  115. __('Planning to Watch ', 'sakurairo') .
  116. '</p>';
  117. }
  118. case 4: // Dropped
  119. {
  120. return '<div class="bangumi-status-bar" style="width: '.
  121. MyAnimeList::status_percent($item) .
  122. '%; background: #d9534f;"></div>' .
  123. '<p>' .
  124. __('Dropped ', 'sakurairo') . $item['num_watched_episodes'] . '/' . $item['anime_num_episodes'] .
  125. '</p>';
  126. }
  127. case 3: // On Hold
  128. {
  129. return '<div class="bangumi-status-bar" style="width: '.
  130. MyAnimeList::status_percent($item) .
  131. '%; background: #f0ad4e;"></div>' .
  132. '<p>' .
  133. __('Paused ', 'sakurairo') . $item['num_watched_episodes'] . '/' . $item['anime_num_episodes'] .
  134. '</p>';
  135. }
  136. default: // TODO: other possible status code
  137. {
  138. return '';
  139. }
  140. }
  141. }
  142. private static function status_percent(array $item)
  143. {
  144. if ($item['anime_num_episodes'] == 0)
  145. {
  146. return 0;
  147. }
  148. else
  149. {
  150. return ($item['num_watched_episodes']/$item['anime_num_episodes']) * 100;
  151. }
  152. }
  153. }