Aplayer.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace Sakura\API;
  3. class Aplayer
  4. {
  5. public $server;
  6. public $playlist_id;
  7. private $cookies;
  8. public $api_url;
  9. public $proxy;
  10. public function __construct()
  11. {
  12. $this->server = iro_opt('aplayer_server');
  13. $this->playlist_id = iro_opt('aplayer_playlistid');
  14. $this->cookies = iro_opt('aplayer_cookie');
  15. $this->api_url = rest_url('sakura/v1/meting/aplayer');
  16. $this->proxy = iro_opt('aplayer_server_proxy', '');
  17. require('Meting.php');
  18. }
  19. public function get_data($type, $id)
  20. {
  21. $server = $this->server;
  22. $cookies = $this->cookies;
  23. $playlist_id = $this->playlist_id;
  24. $proxy = $this->proxy;
  25. $api = new \Sakura\API\Meting($server);
  26. if (!empty($cookies) && $server === "netease")
  27. $api->cookie($cookies);
  28. if ($proxy != '')
  29. $api->proxy($proxy);
  30. switch ($type) {
  31. case 'song':
  32. $data = $api->format(true)->song($id);
  33. $data = json_decode($data, true)["url"];
  34. $data = $this->song_url($data);
  35. break;
  36. // case 'album':
  37. // $data = $api->format(true)->album($id);
  38. // $data=json_decode($data, true)["url"];
  39. // break;
  40. case 'playlist':
  41. $data = $api->format(true)->playlist($playlist_id);
  42. $data = $this->format_playlist($data);
  43. break;
  44. case 'lyric':
  45. $data = $api->format(true)->lyric($id);
  46. $data = $this->format_lyric($data);
  47. break;
  48. case 'pic':
  49. $data = $api->format(true)->pic($id);
  50. $data = json_decode($data, true)["url"];
  51. break;
  52. // case 'search':
  53. // $data = $api->format(true)->search($id);
  54. // $data=json_decode($data, true);
  55. // break;
  56. default:
  57. $data = $api->format(true)->url($id);
  58. $data = json_decode($data, true)["url"];
  59. $data = $this->song_url($data);
  60. break;
  61. }
  62. return $data;
  63. }
  64. private function format_playlist($data)
  65. {
  66. $server = $this->server;
  67. $_api_url = $this->api_url;
  68. $api_url = $_api_url . (preg_match('/index.php\?/i', $_api_url) ? '&' : '?');
  69. $data = json_decode($data);
  70. $playlist = array();
  71. foreach ((array) $data as $value) {
  72. $name = $value->name;
  73. $artists = implode(" / ", (array) $value->artist);
  74. $mp3_url = $api_url . "server=$server&type=url&id=" . $value->url_id . '&meting_nonce=' . wp_create_nonce('url#:' . $value->url_id);
  75. $cover = $api_url . "server=$server&type=pic&id=" . $value->pic_id . '&meting_nonce=' . wp_create_nonce('pic#:' . $value->url_id);
  76. $lyric = $api_url . "server=$server&type=lyric&id=" . $value->lyric_id . '&meting_nonce=' . wp_create_nonce('lyric#:' . $value->url_id);
  77. $playlist[] = array(
  78. "name" => $name,
  79. "artist" => $artists,
  80. "url" => $mp3_url,
  81. "cover" => $cover,
  82. "lrc" => $lyric
  83. );
  84. }
  85. return $playlist;
  86. }
  87. private function song_url($url)
  88. {
  89. $server = $this->server;
  90. if ($server == 'netease') {
  91. $url = str_replace('://m7c.', '://m7.', $url);
  92. $url = str_replace('://m8c.', '://m8.', $url);
  93. $url = str_replace('http://m8.', 'https://m9.', $url);
  94. $url = str_replace('http://m7.', 'https://m9.', $url);
  95. $url = str_replace('http://m10.', 'https://m10.', $url);
  96. } elseif ($server == 'xiami') {
  97. $url = str_replace('http://', 'https://', $url);
  98. } elseif ($server == 'baidu') {
  99. $url = str_replace('http://zhangmenshiting.qianqian.com', 'https://gss3.baidu.com/y0s1hSulBw92lNKgpU_Z2jR7b2w6buu', $url);
  100. }
  101. return $url;
  102. }
  103. private function format_lyric($data)
  104. {
  105. $server = $this->server;
  106. $data = json_decode($data, true);
  107. $data = $this->lrctran($data['lyric'], $data['tlyric']);
  108. if (empty($data)) {
  109. $data = "[00:00.000]此歌曲暂无歌词,请您欣赏";
  110. }
  111. if ($server === 'tencent') {
  112. $data = html_entity_decode($data, ENT_QUOTES | ENT_HTML5);
  113. }
  114. return $data;
  115. }
  116. private function lrctran($lyric, $tlyric)
  117. {
  118. $lyric = $this->lrctrim($lyric);
  119. $tlyric = $this->lrctrim($tlyric);
  120. $len1 = count($lyric);
  121. $len2 = count($tlyric);
  122. $result = "";
  123. for ($i = 0, $j = 0; $i < $len1 && $j < $len2; $i++) {
  124. while ($lyric[$i][0] > $tlyric[$j][0] && $j + 1 < $len2) {
  125. $j++;
  126. }
  127. if ($lyric[$i][0] == $tlyric[$j][0]) {
  128. $tlyric[$j][2] = str_replace('/', '', $tlyric[$j][2]);
  129. if (!empty($tlyric[$j][2])) {
  130. $lyric[$i][2] .= " ({$tlyric[$j][2]})";
  131. }
  132. $j++;
  133. }
  134. }
  135. for ($i = 0; $i < $len1; $i++) {
  136. $t = $lyric[$i][0];
  137. $result .= sprintf("[%02d:%02d.%03d]%s\n", $t / 60000, $t % 60000 / 1000, $t % 1000, $lyric[$i][2]);
  138. }
  139. return $result;
  140. }
  141. private function lrctrim($lyrics)
  142. {
  143. $lyrics = explode("\n", $lyrics);
  144. $data = array();
  145. foreach ($lyrics as $key => $lyric) {
  146. preg_match('/\[(\d{2}):(\d{2}[\.:]?\d*)]/', $lyric, $lrcTimes);
  147. $lrcText = preg_replace('/\[(\d{2}):(\d{2}[\.:]?\d*)]/', '', $lyric);
  148. if (empty($lrcTimes)) {
  149. continue;
  150. }
  151. $lrcTimes = intval($lrcTimes[1]) * 60000 + intval(floatval($lrcTimes[2]) * 1000);
  152. $lrcText = preg_replace('/\s\s+/', ' ', $lrcText);
  153. $lrcText = trim($lrcText);
  154. $data[] = array($lrcTimes, $key, $lrcText);
  155. }
  156. sort($data);
  157. return $data;
  158. }
  159. }