1
0

Aplayer.php 5.9 KB

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