1
0

Meting.php 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  1. <?php
  2. /**
  3. * Meting music framework
  4. * https://i-meto.com
  5. * https://github.com/metowolf/Meting
  6. * Version 1.5.11.
  7. *
  8. * Copyright 2019, METO Sheel <i@i-meto.com>
  9. * Released under the MIT license
  10. */
  11. namespace Sakura\API;
  12. class Meting
  13. {
  14. const VERSION = '1.5.11';
  15. public $raw;
  16. public $data;
  17. public $info;
  18. public $error;
  19. public $status;
  20. public $server;
  21. public $proxy = null;
  22. public $format = false;
  23. public $header;
  24. public function __construct($value = 'netease')
  25. {
  26. $this->site($value);
  27. }
  28. public function site($value)
  29. {
  30. $suppose = array('netease', 'tencent', 'xiami', 'kugou', 'baidu', 'kuwo');
  31. $this->server = in_array($value, $suppose) ? $value : 'netease';
  32. $this->header = $this->curlset();
  33. return $this;
  34. }
  35. public function cookie($value)
  36. {
  37. $this->header['Cookie'] = $value;
  38. return $this;
  39. }
  40. public function format($value = true)
  41. {
  42. $this->format = $value;
  43. return $this;
  44. }
  45. public function proxy($value)
  46. {
  47. $this->proxy = $value;
  48. return $this;
  49. }
  50. private function exec($api)
  51. {
  52. if (isset($api['encode'])) {
  53. $api = call_user_func_array(array($this, $api['encode']), array($api));
  54. }
  55. if ($api['method'] == 'GET') {
  56. if (isset($api['body'])) {
  57. $api['url'] .= '?'.http_build_query($api['body']);
  58. $api['body'] = null;
  59. }
  60. }
  61. $this->curl($api['url'], $api['body']);
  62. if (!$this->format) {
  63. return $this->raw;
  64. }
  65. $this->data = $this->raw;
  66. if (isset($api['decode'])) {
  67. $this->data = call_user_func_array(array($this, $api['decode']), array($this->data));
  68. }
  69. if (isset($api['format'])) {
  70. $this->data = $this->clean($this->data, $api['format']);
  71. }
  72. return $this->data;
  73. }
  74. private function curl($url, $payload = null, $headerOnly = 0)
  75. {
  76. $header = array_map(function ($k, $v) {
  77. return $k.': '.$v;
  78. }, array_keys($this->header), $this->header);
  79. $curl = curl_init();
  80. if (!is_null($payload)) {
  81. curl_setopt($curl, CURLOPT_POST, 1);
  82. curl_setopt($curl, CURLOPT_POSTFIELDS, is_array($payload) ? http_build_query($payload) : $payload);
  83. }
  84. curl_setopt($curl, CURLOPT_HEADER, $headerOnly);
  85. curl_setopt($curl, CURLOPT_TIMEOUT, 20);
  86. curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
  87. curl_setopt($curl, CURLOPT_IPRESOLVE, 1);
  88. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  89. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  90. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
  91. curl_setopt($curl, CURLOPT_URL, $url);
  92. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  93. if ($this->proxy) {
  94. curl_setopt($curl, CURLOPT_PROXY, $this->proxy);
  95. }
  96. for ($i = 0; $i < 3; $i++) {
  97. $this->raw = curl_exec($curl);
  98. $this->info = curl_getinfo($curl);
  99. $this->error = curl_errno($curl);
  100. $this->status = $this->error ? curl_error($curl) : '';
  101. if (!$this->error) {
  102. break;
  103. }
  104. }
  105. curl_close($curl);
  106. return $this;
  107. }
  108. private function pickup($array, $rule)
  109. {
  110. $t = explode('.', $rule);
  111. foreach ($t as $vo) {
  112. if (!isset($array[$vo])) {
  113. return array();
  114. }
  115. $array = $array[$vo];
  116. }
  117. return $array;
  118. }
  119. private function clean($raw, $rule)
  120. {
  121. $raw = json_decode($raw, true);
  122. if (!empty($rule)) {
  123. $raw = $this->pickup($raw, $rule);
  124. }
  125. if (!isset($raw[0]) && count($raw)) {
  126. $raw = array($raw);
  127. }
  128. $result = array_map(array($this, 'format_'.$this->server), $raw);
  129. return json_encode($result);
  130. }
  131. public function search($keyword, $option = null)
  132. {
  133. switch ($this->server) {
  134. case 'netease':
  135. $api = array(
  136. 'method' => 'POST',
  137. 'url' => 'http://music.163.com/api/cloudsearch/pc',
  138. 'body' => array(
  139. 's' => $keyword,
  140. 'type' => isset($option['type']) ? $option['type'] : 1,
  141. 'limit' => isset($option['limit']) ? $option['limit'] : 30,
  142. 'total' => 'true',
  143. 'offset' => isset($option['page']) && isset($option['limit']) ? ($option['page'] - 1) * $option['limit'] : 0,
  144. ),
  145. 'encode' => 'netease_AESCBC',
  146. 'format' => 'result.songs',
  147. );
  148. break;
  149. case 'tencent':
  150. $api = array(
  151. 'method' => 'GET',
  152. 'url' => 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp',
  153. 'body' => array(
  154. 'format' => 'json',
  155. 'p' => isset($option['page']) ? $option['page'] : 1,
  156. 'n' => isset($option['limit']) ? $option['limit'] : 30,
  157. 'w' => $keyword,
  158. 'aggr' => 1,
  159. 'lossless' => 1,
  160. 'cr' => 1,
  161. 'new_json' => 1,
  162. ),
  163. 'format' => 'data.song.list',
  164. );
  165. break;
  166. case 'xiami':
  167. $api = array(
  168. 'method' => 'GET',
  169. 'url' => 'https://acs.m.xiami.com/h5/mtop.alimusic.search.searchservice.searchsongs/1.0/',
  170. 'body' => array(
  171. 'data' => array(
  172. 'key' => $keyword,
  173. 'pagingVO' => array(
  174. 'page' => isset($option['page']) ? $option['page'] : 1,
  175. 'pageSize' => isset($option['limit']) ? $option['limit'] : 30,
  176. ),
  177. ),
  178. 'r' => 'mtop.alimusic.search.searchservice.searchsongs',
  179. ),
  180. 'encode' => 'xiami_sign',
  181. 'format' => 'data.data.songs',
  182. );
  183. break;
  184. case 'kugou':
  185. $api = array(
  186. 'method' => 'GET',
  187. 'url' => 'http://mobilecdn.kugou.com/api/v3/search/song',
  188. 'body' => array(
  189. 'api_ver' => 1,
  190. 'area_code' => 1,
  191. 'correct' => 1,
  192. 'pagesize' => isset($option['limit']) ? $option['limit'] : 30,
  193. 'plat' => 2,
  194. 'tag' => 1,
  195. 'sver' => 5,
  196. 'showtype' => 10,
  197. 'page' => isset($option['page']) ? $option['page'] : 1,
  198. 'keyword' => $keyword,
  199. 'version' => 8990,
  200. ),
  201. 'format' => 'data.info',
  202. );
  203. break;
  204. case 'baidu':
  205. $api = array(
  206. 'method' => 'GET',
  207. 'url' => 'http://musicapi.taihe.com/v1/restserver/ting',
  208. 'body' => array(
  209. 'from' => 'qianqianmini',
  210. 'method' => 'baidu.ting.search.merge',
  211. 'isNew' => 1,
  212. 'platform' => 'darwin',
  213. 'page_no' => isset($option['page']) ? $option['page'] : 1,
  214. 'query' => $keyword,
  215. 'version' => '11.2.1',
  216. 'page_size' => isset($option['limit']) ? $option['limit'] : 30,
  217. ),
  218. 'format' => 'result.song_info.song_list',
  219. );
  220. break;
  221. case 'kuwo':
  222. $api = array(
  223. 'method' => 'GET',
  224. 'url' => 'http://www.kuwo.cn/api/www/search/searchMusicBykeyWord',
  225. 'body' => array(
  226. 'key' => $keyword,
  227. 'pn' => isset($option['page']) ? $option['page'] : 1,
  228. 'rn' => isset($option['limit']) ? $option['limit'] : 30,
  229. 'httpsStatus' => 1,
  230. ),
  231. 'format' => 'data.list',
  232. );
  233. break;
  234. }
  235. return $this->exec($api);
  236. }
  237. public function song($id)
  238. {
  239. switch ($this->server) {
  240. case 'netease':
  241. $api = array(
  242. 'method' => 'POST',
  243. 'url' => 'http://music.163.com/api/v3/song/detail/',
  244. 'body' => array(
  245. 'c' => '[{"id":'.$id.',"v":0}]',
  246. ),
  247. 'encode' => 'netease_AESCBC',
  248. 'format' => 'songs',
  249. );
  250. break;
  251. case 'tencent':
  252. $api = array(
  253. 'method' => 'GET',
  254. 'url' => 'https://c.y.qq.com/v8/fcg-bin/fcg_play_single_song.fcg',
  255. 'body' => array(
  256. 'songmid' => $id,
  257. 'platform' => 'yqq',
  258. 'format' => 'json',
  259. ),
  260. 'format' => 'data',
  261. );
  262. break;
  263. case 'xiami':
  264. $api = array(
  265. 'method' => 'GET',
  266. 'url' => 'https://acs.m.xiami.com/h5/mtop.alimusic.music.songservice.getsongdetail/1.0/',
  267. 'body' => array(
  268. 'data' => array(
  269. 'songId' => $id,
  270. ),
  271. 'r' => 'mtop.alimusic.music.songservice.getsongdetail',
  272. ),
  273. 'encode' => 'xiami_sign',
  274. 'format' => 'data.data.songDetail',
  275. );
  276. break;
  277. case 'kugou':
  278. $api = array(
  279. 'method' => 'POST',
  280. 'url' => 'http://m.kugou.com/app/i/getSongInfo.php',
  281. 'body' => array(
  282. 'cmd' => 'playInfo',
  283. 'hash' => $id,
  284. 'from' => 'mkugou',
  285. ),
  286. 'format' => '',
  287. );
  288. break;
  289. case 'baidu':
  290. $api = array(
  291. 'method' => 'GET',
  292. 'url' => 'http://musicapi.taihe.com/v1/restserver/ting',
  293. 'body' => array(
  294. 'from' => 'qianqianmini',
  295. 'method' => 'baidu.ting.song.getInfos',
  296. 'songid' => $id,
  297. 'res' => 1,
  298. 'platform' => 'darwin',
  299. 'version' => '1.0.0',
  300. ),
  301. 'encode' => 'baidu_AESCBC',
  302. 'format' => 'songinfo',
  303. );
  304. break;
  305. case 'kuwo':
  306. $api = array(
  307. 'method' => 'GET',
  308. 'url' => 'http://www.kuwo.cn/api/www/music/musicInfo',
  309. 'body' => array(
  310. 'mid' => $id,
  311. 'httpsStatus' => 1,
  312. ),
  313. 'format' => 'data',
  314. );
  315. break;
  316. }
  317. return $this->exec($api);
  318. }
  319. public function album($id)
  320. {
  321. switch ($this->server) {
  322. case 'netease':
  323. $api = array(
  324. 'method' => 'POST',
  325. 'url' => 'http://music.163.com/api/v1/album/'.$id,
  326. 'body' => array(
  327. 'total' => 'true',
  328. 'offset' => '0',
  329. 'id' => $id,
  330. 'limit' => '1000',
  331. 'ext' => 'true',
  332. 'private_cloud' => 'true',
  333. ),
  334. 'encode' => 'netease_AESCBC',
  335. 'format' => 'songs',
  336. );
  337. break;
  338. case 'tencent':
  339. $api = array(
  340. 'method' => 'GET',
  341. 'url' => 'https://c.y.qq.com/v8/fcg-bin/fcg_v8_album_detail_cp.fcg',
  342. 'body' => array(
  343. 'albummid' => $id,
  344. 'platform' => 'mac',
  345. 'format' => 'json',
  346. 'newsong' => 1,
  347. ),
  348. 'format' => 'data.getSongInfo',
  349. );
  350. break;
  351. case 'xiami':
  352. $api = array(
  353. 'method' => 'GET',
  354. 'url' => 'https://acs.m.xiami.com/h5/mtop.alimusic.music.albumservice.getalbumdetail/1.0/',
  355. 'body' => array(
  356. 'data' => array(
  357. 'albumId' => $id,
  358. ),
  359. 'r' => 'mtop.alimusic.music.albumservice.getalbumdetail',
  360. ),
  361. 'encode' => 'xiami_sign',
  362. 'format' => 'data.data.albumDetail.songs',
  363. );
  364. break;
  365. case 'kugou':
  366. $api = array(
  367. 'method' => 'GET',
  368. 'url' => 'http://mobilecdn.kugou.com/api/v3/album/song',
  369. 'body' => array(
  370. 'albumid' => $id,
  371. 'area_code' => 1,
  372. 'plat' => 2,
  373. 'page' => 1,
  374. 'pagesize' => -1,
  375. 'version' => 8990,
  376. ),
  377. 'format' => 'data.info',
  378. );
  379. break;
  380. case 'baidu':
  381. $api = array(
  382. 'method' => 'GET',
  383. 'url' => 'http://musicapi.taihe.com/v1/restserver/ting',
  384. 'body' => array(
  385. 'from' => 'qianqianmini',
  386. 'method' => 'baidu.ting.album.getAlbumInfo',
  387. 'album_id' => $id,
  388. 'platform' => 'darwin',
  389. 'version' => '11.2.1',
  390. ),
  391. 'format' => 'songlist',
  392. );
  393. break;
  394. case 'kuwo':
  395. $api = array(
  396. 'method' => 'GET',
  397. 'url' => 'http://www.kuwo.cn/api/www/album/albumInfo',
  398. 'body' => array(
  399. 'albumId' => $id,
  400. 'pn' => 1,
  401. 'rn' => 1000,
  402. 'httpsStatus' => 1,
  403. ),
  404. 'format' => 'data.musicList',
  405. );
  406. break;
  407. }
  408. return $this->exec($api);
  409. }
  410. public function artist($id, $limit = 50)
  411. {
  412. switch ($this->server) {
  413. case 'netease':
  414. $api = array(
  415. 'method' => 'POST',
  416. 'url' => 'http://music.163.com/api/v1/artist/'.$id,
  417. 'body' => array(
  418. 'ext' => 'true',
  419. 'private_cloud' => 'true',
  420. 'ext' => 'true',
  421. 'top' => $limit,
  422. 'id' => $id,
  423. ),
  424. 'encode' => 'netease_AESCBC',
  425. 'format' => 'hotSongs',
  426. );
  427. break;
  428. case 'tencent':
  429. $api = array(
  430. 'method' => 'GET',
  431. 'url' => 'https://c.y.qq.com/v8/fcg-bin/fcg_v8_singer_track_cp.fcg',
  432. 'body' => array(
  433. 'singermid' => $id,
  434. 'begin' => 0,
  435. 'num' => $limit,
  436. 'order' => 'listen',
  437. 'platform' => 'mac',
  438. 'newsong' => 1,
  439. ),
  440. 'format' => 'data.list',
  441. );
  442. break;
  443. case 'xiami':
  444. $api = array(
  445. 'method' => 'GET',
  446. 'url' => 'https://acs.m.xiami.com/h5/mtop.alimusic.music.songservice.getartistsongs/1.0/',
  447. 'body' => array(
  448. 'data' => array(
  449. 'artistId' => $id,
  450. 'pagingVO' => array(
  451. 'page' => 1,
  452. 'pageSize' => $limit,
  453. ),
  454. ),
  455. 'r' => 'mtop.alimusic.music.songservice.getartistsongs',
  456. ),
  457. 'encode' => 'xiami_sign',
  458. 'format' => 'data.data.songs',
  459. );
  460. break;
  461. case 'kugou':
  462. $api = array(
  463. 'method' => 'GET',
  464. 'url' => 'http://mobilecdn.kugou.com/api/v3/singer/song',
  465. 'body' => array(
  466. 'singerid' => $id,
  467. 'area_code' => 1,
  468. 'page' => 1,
  469. 'plat' => 0,
  470. 'pagesize' => $limit,
  471. 'version' => 8990,
  472. ),
  473. 'format' => 'data.info',
  474. );
  475. break;
  476. case 'baidu':
  477. $api = array(
  478. 'method' => 'GET',
  479. 'url' => 'http://musicapi.taihe.com/v1/restserver/ting',
  480. 'body' => array(
  481. 'from' => 'qianqianmini',
  482. 'method' => 'baidu.ting.artist.getSongList',
  483. 'artistid' => $id,
  484. 'limits' => $limit,
  485. 'platform' => 'darwin',
  486. 'offset' => 0,
  487. 'tinguid' => 0,
  488. 'version' => '11.2.1',
  489. ),
  490. 'format' => 'songlist',
  491. );
  492. break;
  493. case 'kuwo':
  494. $api = array(
  495. 'method' => 'GET',
  496. 'url' => 'http://www.kuwo.cn/api/www/artist/artistMusic',
  497. 'body' => array(
  498. 'artistid' => $id,
  499. 'pn' => 1,
  500. 'rn' => $limit,
  501. 'httpsStatus' => 1,
  502. ),
  503. 'format' => 'data.list',
  504. );
  505. break;
  506. }
  507. return $this->exec($api);
  508. }
  509. public function playlist($id)
  510. {
  511. switch ($this->server) {
  512. case 'netease':
  513. $api = array(
  514. 'method' => 'POST',
  515. 'url' => 'http://music.163.com/api/v6/playlist/detail',
  516. 'body' => array(
  517. 's' => '0',
  518. 'id' => $id,
  519. 'n' => '1000',
  520. 't' => '0',
  521. ),
  522. 'encode' => 'netease_AESCBC',
  523. 'format' => 'playlist.tracks',
  524. );
  525. break;
  526. case 'tencent':
  527. $api = array(
  528. 'method' => 'GET',
  529. 'url' => 'https://c.y.qq.com/v8/fcg-bin/fcg_v8_playlist_cp.fcg',
  530. 'body' => array(
  531. 'id' => $id,
  532. 'format' => 'json',
  533. 'newsong' => 1,
  534. 'platform' => 'jqspaframe.json',
  535. ),
  536. 'format' => 'data.cdlist.0.songlist',
  537. );
  538. break;
  539. case 'xiami':
  540. $api = array(
  541. 'method' => 'GET',
  542. 'url' => 'https://acs.m.xiami.com/h5/mtop.alimusic.music.list.collectservice.getcollectdetail/1.0/',
  543. 'body' => array(
  544. 'data' => array(
  545. 'listId' => $id,
  546. 'isFullTags' => false,
  547. 'pagingVO' => array(
  548. 'page' => 1,
  549. 'pageSize' => 1000,
  550. ),
  551. ),
  552. 'r' => 'mtop.alimusic.music.list.collectservice.getcollectdetail',
  553. ),
  554. 'encode' => 'xiami_sign',
  555. 'format' => 'data.data.collectDetail.songs',
  556. );
  557. break;
  558. case 'kugou':
  559. $api = array(
  560. 'method' => 'GET',
  561. 'url' => 'http://mobilecdn.kugou.com/api/v3/special/song',
  562. 'body' => array(
  563. 'specialid' => $id,
  564. 'area_code' => 1,
  565. 'page' => 1,
  566. 'plat' => 2,
  567. 'pagesize' => -1,
  568. 'version' => 8990,
  569. ),
  570. 'format' => 'data.info',
  571. );
  572. break;
  573. case 'baidu':
  574. $api = array(
  575. 'method' => 'GET',
  576. 'url' => 'http://musicapi.taihe.com/v1/restserver/ting',
  577. 'body' => array(
  578. 'from' => 'qianqianmini',
  579. 'method' => 'baidu.ting.diy.gedanInfo',
  580. 'listid' => $id,
  581. 'platform' => 'darwin',
  582. 'version' => '11.2.1',
  583. ),
  584. 'format' => 'content',
  585. );
  586. break;
  587. case 'kuwo':
  588. $api = array(
  589. 'method' => 'GET',
  590. 'url' => 'http://www.kuwo.cn/api/www/playlist/playListInfo',
  591. 'body' => array(
  592. 'pid' => $id,
  593. 'pn' => 1,
  594. 'rn' => 1000,
  595. 'httpsStatus' => 1,
  596. ),
  597. 'format' => 'data.musicList',
  598. );
  599. break;
  600. }
  601. return $this->exec($api);
  602. }
  603. public function url($id, $br = 320)
  604. {
  605. switch ($this->server) {
  606. case 'netease':
  607. $api = array(
  608. 'method' => 'POST',
  609. 'url' => 'http://music.163.com/api/song/enhance/player/url',
  610. 'body' => array(
  611. 'ids' => array($id),
  612. 'br' => $br * 1000,
  613. ),
  614. 'encode' => 'netease_AESCBC',
  615. 'decode' => 'netease_url',
  616. );
  617. break;
  618. case 'tencent':
  619. $api = array(
  620. 'method' => 'GET',
  621. 'url' => 'https://c.y.qq.com/v8/fcg-bin/fcg_play_single_song.fcg',
  622. 'body' => array(
  623. 'songmid' => $id,
  624. 'platform' => 'yqq',
  625. 'format' => 'json',
  626. ),
  627. 'decode' => 'tencent_url',
  628. );
  629. break;
  630. case 'xiami':
  631. $api = array(
  632. 'method' => 'GET',
  633. 'url' => 'https://acs.m.xiami.com/h5/mtop.alimusic.music.songservice.getsongs/1.0/',
  634. 'body' => array(
  635. 'data' => array(
  636. 'songIds' => array(
  637. $id,
  638. ),
  639. ),
  640. 'r' => 'mtop.alimusic.music.songservice.getsongs',
  641. ),
  642. 'encode' => 'xiami_sign',
  643. 'decode' => 'xiami_url',
  644. );
  645. break;
  646. case 'kugou':
  647. $api = array(
  648. 'method' => 'POST',
  649. 'url' => 'http://media.store.kugou.com/v1/get_res_privilege',
  650. 'body' => json_encode(
  651. array(
  652. 'relate' => 1,
  653. 'userid' => '0',
  654. 'vip' => 0,
  655. 'appid' => 1000,
  656. 'token' => '',
  657. 'behavior' => 'download',
  658. 'area_code' => '1',
  659. 'clientver' => '8990',
  660. 'resource' => array(array(
  661. 'id' => 0,
  662. 'type' => 'audio',
  663. 'hash' => $id,
  664. )), )
  665. ),
  666. 'decode' => 'kugou_url',
  667. );
  668. break;
  669. case 'baidu':
  670. $api = array(
  671. 'method' => 'GET',
  672. 'url' => 'http://musicapi.taihe.com/v1/restserver/ting',
  673. 'body' => array(
  674. 'from' => 'qianqianmini',
  675. 'method' => 'baidu.ting.song.getInfos',
  676. 'songid' => $id,
  677. 'res' => 1,
  678. 'platform' => 'darwin',
  679. 'version' => '1.0.0',
  680. ),
  681. 'encode' => 'baidu_AESCBC',
  682. 'decode' => 'baidu_url',
  683. );
  684. break;
  685. case 'kuwo':
  686. $api = array(
  687. 'method' => 'GET',
  688. 'url' => 'http://www.kuwo.cn/url',
  689. 'body' => array(
  690. 'rid' => $id,
  691. 'response' => 'url',
  692. 'type' => 'convert_url3',
  693. 'br' => '128kmp3',
  694. 'from' => 'web',
  695. 't' => time(),
  696. 'httpsStatus' => 1,
  697. ),
  698. 'decode' => 'kuwo_url',
  699. );
  700. break;
  701. }
  702. $this->temp['br'] = $br;
  703. return $this->exec($api);
  704. }
  705. public function lyric($id)
  706. {
  707. switch ($this->server) {
  708. case 'netease':
  709. $api = array(
  710. 'method' => 'POST',
  711. 'url' => 'http://music.163.com/api/song/lyric',
  712. 'body' => array(
  713. 'id' => $id,
  714. 'os' => 'linux',
  715. 'lv' => -1,
  716. 'kv' => -1,
  717. 'tv' => -1,
  718. ),
  719. 'encode' => 'netease_AESCBC',
  720. 'decode' => 'netease_lyric',
  721. );
  722. break;
  723. case 'tencent':
  724. $api = array(
  725. 'method' => 'GET',
  726. 'url' => 'https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric_new.fcg',
  727. 'body' => array(
  728. 'songmid' => $id,
  729. 'g_tk' => '5381',
  730. ),
  731. 'decode' => 'tencent_lyric',
  732. );
  733. break;
  734. case 'xiami':
  735. $api = array(
  736. 'method' => 'GET',
  737. 'url' => 'https://acs.m.xiami.com/h5/mtop.alimusic.music.lyricservice.getsonglyrics/1.0/',
  738. 'body' => array(
  739. 'data' => array(
  740. 'songId' => $id,
  741. ),
  742. 'r' => 'mtop.alimusic.music.lyricservice.getsonglyrics',
  743. ),
  744. 'encode' => 'xiami_sign',
  745. 'decode' => 'xiami_lyric',
  746. );
  747. break;
  748. case 'kugou':
  749. $api = array(
  750. 'method' => 'GET',
  751. 'url' => 'http://krcs.kugou.com/search',
  752. 'body' => array(
  753. 'keyword' => '%20-%20',
  754. 'ver' => 1,
  755. 'hash' => $id,
  756. 'client' => 'mobi',
  757. 'man' => 'yes',
  758. ),
  759. 'decode' => 'kugou_lyric',
  760. );
  761. break;
  762. case 'baidu':
  763. $api = array(
  764. 'method' => 'GET',
  765. 'url' => 'http://musicapi.taihe.com/v1/restserver/ting',
  766. 'body' => array(
  767. 'from' => 'qianqianmini',
  768. 'method' => 'baidu.ting.song.lry',
  769. 'songid' => $id,
  770. 'platform' => 'darwin',
  771. 'version' => '1.0.0',
  772. ),
  773. 'decode' => 'baidu_lyric',
  774. );
  775. break;
  776. case 'kuwo':
  777. $api = array(
  778. 'method' => 'GET',
  779. 'url' => 'http://m.kuwo.cn/newh5/singles/songinfoandlrc',
  780. 'body' => array(
  781. 'musicId' => $id,
  782. 'httpsStatus' => 1,
  783. ),
  784. 'decode' => 'kuwo_lyric',
  785. );
  786. break;
  787. }
  788. return $this->exec($api);
  789. }
  790. public function pic($id, $size = 300)
  791. {
  792. switch ($this->server) {
  793. case 'netease':
  794. $url = 'https://p3.music.126.net/'.$this->netease_encryptId($id).'/'.$id.'.jpg?param='.$size.'y'.$size;
  795. break;
  796. case 'tencent':
  797. $url = 'https://y.gtimg.cn/music/photo_new/T002R'.$size.'x'.$size.'M000'.$id.'.jpg?max_age=2592000';
  798. break;
  799. case 'xiami':
  800. $format = $this->format;
  801. $data = $this->format(false)->song($id);
  802. $this->format = $format;
  803. $data = json_decode($data, true);
  804. $url = $data['data']['data']['songDetail']['albumLogo'];
  805. $url = str_replace('http:', 'https:', $url).'@1e_1c_100Q_'.$size.'h_'.$size.'w';
  806. break;
  807. case 'kugou':
  808. $format = $this->format;
  809. $data = $this->format(false)->song($id);
  810. $this->format = $format;
  811. $data = json_decode($data, true);
  812. $url = $data['imgUrl'];
  813. $url = str_replace('{size}', '400', $url);
  814. break;
  815. case 'baidu':
  816. $format = $this->format;
  817. $data = $this->format(false)->song($id);
  818. $this->format = $format;
  819. $data = json_decode($data, true);
  820. $url = isset($data['songinfo']['pic_radio']) ? $data['songinfo']['pic_radio'] : $data['songinfo']['pic_small'];
  821. break;
  822. case 'kuwo':
  823. $format = $this->format;
  824. $data = $this->format(false)->song($id);
  825. $this->format = $format;
  826. $data = json_decode($data, true);
  827. $url = isset($data['data']['pic']) ? $data['data']['pic'] : $data['data']['albumpic'];
  828. break;
  829. }
  830. return json_encode(array('url' => $url));
  831. }
  832. private function curlset()
  833. {
  834. switch ($this->server) {
  835. case 'netease':
  836. return array(
  837. 'Referer' => 'https://music.163.com/',
  838. 'Cookie' => 'appver=8.2.30; os=iPhone OS; osver=15.0; EVNSM=1.0.0; buildver=2206; channel=distribution; machineid=iPhone13.3',
  839. 'User-Agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 CloudMusic/0.1.1 NeteaseMusic/8.2.30',
  840. 'X-Real-IP' => long2ip(mt_rand(1884815360, 1884890111)),
  841. 'Accept' => '*/*',
  842. 'Accept-Language' => 'zh-CN,zh;q=0.8,gl;q=0.6,zh-TW;q=0.4',
  843. 'Connection' => 'keep-alive',
  844. 'Content-Type' => 'application/x-www-form-urlencoded',
  845. );
  846. case 'tencent':
  847. return array(
  848. 'Referer' => 'http://y.qq.com',
  849. 'Cookie' => 'pgv_pvi=22038528; pgv_si=s3156287488; pgv_pvid=5535248600; yplayer_open=1; ts_last=y.qq.com/portal/player.html; ts_uid=4847550686; yq_index=0; qqmusic_fromtag=66; player_exist=1',
  850. 'User-Agent' => 'QQ%E9%9F%B3%E4%B9%90/54409 CFNetwork/901.1 Darwin/17.6.0 (x86_64)',
  851. 'Accept' => '*/*',
  852. 'Accept-Language' => 'zh-CN,zh;q=0.8,gl;q=0.6,zh-TW;q=0.4',
  853. 'Connection' => 'keep-alive',
  854. 'Content-Type' => 'application/x-www-form-urlencoded',
  855. );
  856. case 'xiami':
  857. return array(
  858. 'Cookie' => '_m_h5_tk=15d3402511a022796d88b249f83fb968_1511163656929; _m_h5_tk_enc=b6b3e64d81dae577fc314b5c5692df3c',
  859. 'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) XIAMI-MUSIC/3.1.1 Chrome/56.0.2924.87 Electron/1.6.11 Safari/537.36',
  860. 'Accept' => 'application/json',
  861. 'Content-type' => 'application/x-www-form-urlencoded',
  862. 'Accept-Language' => 'zh-CN',
  863. );
  864. case 'kugou':
  865. return array(
  866. 'User-Agent' => 'IPhone-8990-searchSong',
  867. 'UNI-UserAgent' => 'iOS11.4-Phone8990-1009-0-WiFi',
  868. );
  869. case 'baidu':
  870. return array(
  871. 'Cookie' => 'BAIDUID='.$this->getRandomHex(32).':FG=1',
  872. 'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) baidu-music/1.2.1 Chrome/66.0.3359.181 Electron/3.0.5 Safari/537.36',
  873. 'Accept' => '*/*',
  874. 'Content-type' => 'application/json;charset=UTF-8',
  875. 'Accept-Language' => 'zh-CN',
  876. );
  877. case 'kuwo':
  878. return array(
  879. 'Cookie' => 'Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1623339177,1623339183; _ga=GA1.2.1195980605.1579367081; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1623339982; kw_token=3E7JFQ7MRPL; _gid=GA1.2.747985028.1623339179; _gat=1',
  880. 'csrf' => '3E7JFQ7MRPL',
  881. 'Host' => 'www.kuwo.cn',
  882. 'Referer' => 'http://www.kuwo.cn/',
  883. 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36',
  884. );
  885. }
  886. }
  887. private function getRandomHex($length)
  888. {
  889. if (function_exists('random_bytes')) {
  890. return bin2hex(random_bytes($length / 2));
  891. }
  892. if (function_exists('mcrypt_create_iv')) {
  893. return bin2hex(mcrypt_create_iv($length / 2, MCRYPT_DEV_URANDOM));
  894. }
  895. if (function_exists('openssl_random_pseudo_bytes')) {
  896. return bin2hex(openssl_random_pseudo_bytes($length / 2));
  897. }
  898. }
  899. private function bchexdec($hex)
  900. {
  901. $dec = 0;
  902. $len = strlen($hex);
  903. for ($i = 1; $i <= $len; $i++) {
  904. $dec = bcadd($dec, bcmul(strval(hexdec($hex[$i - 1])), bcpow('16', strval($len - $i))));
  905. }
  906. return $dec;
  907. }
  908. private function bcdechex($dec)
  909. {
  910. $hex = '';
  911. do {
  912. $last = bcmod($dec, 16);
  913. $hex = dechex($last).$hex;
  914. $dec = bcdiv(bcsub($dec, $last), 16);
  915. } while ($dec > 0);
  916. return $hex;
  917. }
  918. private function str2hex($string)
  919. {
  920. $hex = '';
  921. for ($i = 0; $i < strlen($string); $i++) {
  922. $ord = ord($string[$i]);
  923. $hexCode = dechex($ord);
  924. $hex .= substr('0'.$hexCode, -2);
  925. }
  926. return $hex;
  927. }
  928. private function netease_AESCBC($api)
  929. {
  930. $modulus = '157794750267131502212476817800345498121872783333389747424011531025366277535262539913701806290766479189477533597854989606803194253978660329941980786072432806427833685472618792592200595694346872951301770580765135349259590167490536138082469680638514416594216629258349130257685001248172188325316586707301643237607';
  931. $pubkey = '65537';
  932. $nonce = '0CoJUm6Qyw8W8jud';
  933. $vi = '0102030405060708';
  934. if (extension_loaded('bcmath')) {
  935. $skey = $this->getRandomHex(16);
  936. } else {
  937. $skey = 'B3v3kH4vRPWRJFfH';
  938. }
  939. $body = json_encode($api['body']);
  940. if (function_exists('openssl_encrypt')) {
  941. $body = openssl_encrypt($body, 'aes-128-cbc', $nonce, false, $vi);
  942. $body = openssl_encrypt($body, 'aes-128-cbc', $skey, false, $vi);
  943. } else {
  944. $pad = 16 - (strlen($body) % 16);
  945. $body = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $nonce, $body.str_repeat(chr($pad), $pad), MCRYPT_MODE_CBC, $vi));
  946. $pad = 16 - (strlen($body) % 16);
  947. $body = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $skey, $body.str_repeat(chr($pad), $pad), MCRYPT_MODE_CBC, $vi));
  948. }
  949. if (extension_loaded('bcmath')) {
  950. $skey = strrev(utf8_encode($skey));
  951. $skey = $this->bchexdec($this->str2hex($skey));
  952. $skey = bcpowmod($skey, $pubkey, $modulus);
  953. $skey = $this->bcdechex($skey);
  954. $skey = str_pad($skey, 256, '0', STR_PAD_LEFT);
  955. } else {
  956. $skey = '85302b818aea19b68db899c25dac229412d9bba9b3fcfe4f714dc016bc1686fc446a08844b1f8327fd9cb623cc189be00c5a365ac835e93d4858ee66f43fdc59e32aaed3ef24f0675d70172ef688d376a4807228c55583fe5bac647d10ecef15220feef61477c28cae8406f6f9896ed329d6db9f88757e31848a6c2ce2f94308';
  957. }
  958. $api['url'] = str_replace('/api/', '/weapi/', $api['url']);
  959. $api['body'] = array(
  960. 'params' => $body,
  961. 'encSecKey' => $skey,
  962. );
  963. return $api;
  964. }
  965. private function baidu_AESCBC($api)
  966. {
  967. $key = 'DBEECF8C50FD160E';
  968. $vi = '1231021386755796';
  969. $data = 'songid='.$api['body']['songid'].'&ts='.intval(microtime(true) * 1000);
  970. if (function_exists('openssl_encrypt')) {
  971. $data = openssl_encrypt($data, 'aes-128-cbc', $key, false, $vi);
  972. } else {
  973. $pad = 16 - (strlen($data) % 16);
  974. $data = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data.str_repeat(chr($pad), $pad), MCRYPT_MODE_CBC, $vi));
  975. }
  976. $api['body']['e'] = $data;
  977. return $api;
  978. }
  979. private function xiami_sign($api)
  980. {
  981. $data = $this->curl('https://acs.m.xiami.com/h5/mtop.alimusic.recommend.songservice.getdailysongs/1.0/?appKey=12574478&t=1560663823000&dataType=json&data=%7B%22requestStr%22%3A%22%7B%5C%22header%5C%22%3A%7B%5C%22platformId%5C%22%3A%5C%22mac%5C%22%7D%2C%5C%22model%5C%22%3A%5B%5D%7D%22%7D&api=mtop.alimusic.recommend.songservice.getdailysongs&v=1.0&type=originaljson&sign=22ad1377ee193f3e2772c17c6192b17c', null, 1);
  982. preg_match_all('/_m_h5[^;]+/', $data->raw, $match);
  983. $this->header['Cookie'] = $match[0][0].'; '.$match[0][1];
  984. $data = json_encode(array(
  985. 'requestStr' => json_encode(array(
  986. 'header' => array(
  987. 'platformId' => 'mac',
  988. ),
  989. 'model' => $api['body']['data'],
  990. )),
  991. ));
  992. $appkey = '12574478';
  993. $cookie = $this->header['Cookie'];
  994. preg_match('/_m_h5_tk=([^_]+)/', $cookie, $match);
  995. $token = $match[1];
  996. $t = time() * 1000;
  997. $sign = md5(sprintf('%s&%s&%s&%s', $token, $t, $appkey, $data));
  998. $api['body'] = array(
  999. 'appKey' => $appkey,
  1000. 't' => $t,
  1001. 'dataType' => 'json',
  1002. 'data' => $data,
  1003. 'api' => $api['body']['r'],
  1004. 'v' => '1.0',
  1005. 'type' => 'originaljson',
  1006. 'sign' => $sign,
  1007. );
  1008. return $api;
  1009. }
  1010. private function netease_encryptId($id)
  1011. {
  1012. $magic = str_split('3go8&$8*3*3h0k(2)2');
  1013. $song_id = str_split($id);
  1014. for ($i = 0; $i < count($song_id); $i++) {
  1015. $song_id[$i] = chr(ord($song_id[$i]) ^ ord($magic[$i % count($magic)]));
  1016. }
  1017. $result = base64_encode(md5(implode('', $song_id), 1));
  1018. $result = str_replace(array('/', '+'), array('_', '-'), $result);
  1019. return $result;
  1020. }
  1021. private function netease_url($result)
  1022. {
  1023. $data = json_decode($result, true);
  1024. if (isset($data['data'][0]['uf']['url'])) {
  1025. $data['data'][0]['url'] = $data['data'][0]['uf']['url'];
  1026. }
  1027. if (isset($data['data'][0]['url'])) {
  1028. $url = array(
  1029. 'url' => $data['data'][0]['url'],
  1030. 'size' => $data['data'][0]['size'],
  1031. 'br' => $data['data'][0]['br'] / 1000,
  1032. );
  1033. } else {
  1034. $url = array(
  1035. 'url' => '',
  1036. 'size' => 0,
  1037. 'br' => -1,
  1038. );
  1039. }
  1040. return json_encode($url);
  1041. }
  1042. private function tencent_url($result)
  1043. {
  1044. $data = json_decode($result, true);
  1045. $guid = mt_rand() % 10000000000;
  1046. $type = array(
  1047. array('size_flac', 999, 'F000', 'flac'),
  1048. array('size_320mp3', 320, 'M800', 'mp3'),
  1049. array('size_192aac', 192, 'C600', 'm4a'),
  1050. array('size_128mp3', 128, 'M500', 'mp3'),
  1051. array('size_96aac', 96, 'C400', 'm4a'),
  1052. array('size_48aac', 48, 'C200', 'm4a'),
  1053. array('size_24aac', 24, 'C100', 'm4a'),
  1054. );
  1055. $uin = '0';
  1056. preg_match('/uin=(\d+)/', $this->header['Cookie'], $uin_match);
  1057. if (count($uin_match)) {
  1058. $uin = $uin_match[1];
  1059. }
  1060. $payload = array(
  1061. 'req_0' => array(
  1062. 'module' => 'vkey.GetVkeyServer',
  1063. 'method' => 'CgiGetVkey',
  1064. 'param' => array(
  1065. 'guid' => (string) $guid,
  1066. 'songmid' => array(),
  1067. 'filename' => array(),
  1068. 'songtype' => array(),
  1069. 'uin' => $uin,
  1070. 'loginflag' => 1,
  1071. 'platform' => '20',
  1072. ),
  1073. ),
  1074. );
  1075. foreach ($type as $vo) {
  1076. $payload['req_0']['param']['songmid'][] = $data['data'][0]['mid'];
  1077. $payload['req_0']['param']['filename'][] = $vo[2].$data['data'][0]['file']['media_mid'].'.'.$vo[3];
  1078. $payload['req_0']['param']['songtype'][] = $data['data'][0]['type'];
  1079. }
  1080. $api = array(
  1081. 'method' => 'GET',
  1082. 'url' => 'https://u.y.qq.com/cgi-bin/musicu.fcg',
  1083. 'body' => array(
  1084. 'format' => 'json',
  1085. 'platform' => 'yqq.json',
  1086. 'needNewCode' => 0,
  1087. 'data' => json_encode($payload),
  1088. ),
  1089. );
  1090. $response = json_decode($this->exec($api), true);
  1091. $vkeys = $response['req_0']['data']['midurlinfo'];
  1092. foreach ($type as $index => $vo) {
  1093. if ($data['data'][0]['file'][$vo[0]] && $vo[1] <= $this->temp['br']) {
  1094. if (!empty($vkeys[$index]['vkey'])) {
  1095. $url = array(
  1096. 'url' => $response['req_0']['data']['sip'][0].$vkeys[$index]['purl'],
  1097. 'size' => $data['data'][0]['file'][$vo[0]],
  1098. 'br' => $vo[1],
  1099. );
  1100. break;
  1101. }
  1102. }
  1103. }
  1104. if (!isset($url['url'])) {
  1105. $url = array(
  1106. 'url' => '',
  1107. 'size' => 0,
  1108. 'br' => -1,
  1109. );
  1110. }
  1111. return json_encode($url);
  1112. }
  1113. private function xiami_url($result)
  1114. {
  1115. $data = json_decode($result, true);
  1116. $type = array(
  1117. 's' => 740,
  1118. 'h' => 320,
  1119. 'l' => 128,
  1120. 'f' => 64,
  1121. 'e' => 32,
  1122. );
  1123. $max = 0;
  1124. $url = array();
  1125. foreach ($data['data']['data']['songs'][0]['listenFiles'] as $vo) {
  1126. if ($type[$vo['quality']] <= $this->temp['br'] && $type[$vo['quality']] > $max) {
  1127. $max = $type[$vo['quality']];
  1128. $url = array(
  1129. 'url' => $vo['listenFile'],
  1130. 'size' => $vo['fileSize'],
  1131. 'br' => $type[$vo['quality']],
  1132. );
  1133. }
  1134. }
  1135. if (!isset($url['url'])) {
  1136. $url = array(
  1137. 'url' => '',
  1138. 'size' => 0,
  1139. 'br' => -1,
  1140. );
  1141. }
  1142. return json_encode($url);
  1143. }
  1144. private function kugou_url($result)
  1145. {
  1146. $data = json_decode($result, true);
  1147. $max = 0;
  1148. $url = array();
  1149. foreach ($data['data'][0]['relate_goods'] as $vo) {
  1150. if ($vo['info']['bitrate'] <= $this->temp['br'] && $vo['info']['bitrate'] > $max) {
  1151. $api = array(
  1152. 'method' => 'GET',
  1153. 'url' => 'http://trackercdn.kugou.com/i/v2/',
  1154. 'body' => array(
  1155. 'hash' => $vo['hash'],
  1156. 'key' => md5($vo['hash'].'kgcloudv2'),
  1157. 'pid' => 3,
  1158. 'behavior' => 'play',
  1159. 'cmd' => '25',
  1160. 'version' => 8990,
  1161. ),
  1162. );
  1163. $t = json_decode($this->exec($api), true);
  1164. if (isset($t['url'])) {
  1165. $max = $t['bitRate'] / 1000;
  1166. $url = array(
  1167. 'url' => reset($t['url']),
  1168. 'size' => $t['fileSize'],
  1169. 'br' => $t['bitRate'] / 1000,
  1170. );
  1171. }
  1172. }
  1173. }
  1174. if (!isset($url['url'])) {
  1175. $url = array(
  1176. 'url' => '',
  1177. 'size' => 0,
  1178. 'br' => -1,
  1179. );
  1180. }
  1181. return json_encode($url);
  1182. }
  1183. private function baidu_url($result)
  1184. {
  1185. $data = json_decode($result, true);
  1186. $max = 0;
  1187. $url = array();
  1188. foreach ($data['songurl']['url'] as $vo) {
  1189. if ($vo['file_bitrate'] <= $this->temp['br'] && $vo['file_bitrate'] > $max) {
  1190. $url = array(
  1191. 'url' => $vo['file_link'],
  1192. 'br' => $vo['file_bitrate'],
  1193. );
  1194. }
  1195. }
  1196. if (!isset($url['url'])) {
  1197. $url = array(
  1198. 'url' => '',
  1199. 'br' => -1,
  1200. );
  1201. }
  1202. return json_encode($url);
  1203. }
  1204. private function kuwo_url($result)
  1205. {
  1206. $data = json_decode($result, true);
  1207. $url = array();
  1208. if ($data['code'] == 200 && isset($data['url'])) {
  1209. $url = array(
  1210. 'url' => $data['url'],
  1211. 'br' => 128,
  1212. );
  1213. } else {
  1214. $url = array(
  1215. 'url' => '',
  1216. 'br' => -1,
  1217. );
  1218. }
  1219. return json_encode($url);
  1220. }
  1221. private function netease_lyric($result)
  1222. {
  1223. $result = json_decode($result, true);
  1224. $data = array(
  1225. 'lyric' => isset($result['lrc']['lyric']) ? $result['lrc']['lyric'] : '',
  1226. 'tlyric' => isset($result['tlyric']['lyric']) ? $result['tlyric']['lyric'] : '',
  1227. );
  1228. return json_encode($data, JSON_UNESCAPED_UNICODE);
  1229. }
  1230. private function tencent_lyric($result)
  1231. {
  1232. $result = substr($result, 18, -1);
  1233. $result = json_decode($result, true);
  1234. $data = array(
  1235. 'lyric' => isset($result['lyric']) ? base64_decode($result['lyric']) : '',
  1236. 'tlyric' => isset($result['trans']) ? base64_decode($result['trans']) : '',
  1237. );
  1238. return json_encode($data, JSON_UNESCAPED_UNICODE);
  1239. }
  1240. private function xiami_lyric($result)
  1241. {
  1242. $result = json_decode($result, true);
  1243. if (count($result['data']['data']['lyrics'])) {
  1244. $data = $result['data']['data']['lyrics'][0]['content'];
  1245. $data = preg_replace('/<[^>]+>/', '', $data);
  1246. preg_match_all('/\[([\d:\.]+)\](.*)\s\[x-trans\](.*)/i', $data, $match);
  1247. if (count($match[0])) {
  1248. for ($i = 0; $i < count($match[0]); $i++) {
  1249. $A[] = '['.$match[1][$i].']'.$match[2][$i];
  1250. $B[] = '['.$match[1][$i].']'.$match[3][$i];
  1251. }
  1252. $arr = array(
  1253. 'lyric' => str_replace($match[0], $A, $data),
  1254. 'tlyric' => str_replace($match[0], $B, $data),
  1255. );
  1256. } else {
  1257. $arr = array(
  1258. 'lyric' => $data,
  1259. 'tlyric' => '',
  1260. );
  1261. }
  1262. } else {
  1263. $arr = array(
  1264. 'lyric' => '',
  1265. 'tlyric' => '',
  1266. );
  1267. }
  1268. return json_encode($arr, JSON_UNESCAPED_UNICODE);
  1269. }
  1270. private function kugou_lyric($result)
  1271. {
  1272. $result = json_decode($result, true);
  1273. $api = array(
  1274. 'method' => 'GET',
  1275. 'url' => 'http://lyrics.kugou.com/download',
  1276. 'body' => array(
  1277. 'charset' => 'utf8',
  1278. 'accesskey' => $result['candidates'][0]['accesskey'],
  1279. 'id' => $result['candidates'][0]['id'],
  1280. 'client' => 'mobi',
  1281. 'fmt' => 'lrc',
  1282. 'ver' => 1,
  1283. ),
  1284. );
  1285. $data = json_decode($this->exec($api), true);
  1286. $arr = array(
  1287. 'lyric' => base64_decode($data['content']),
  1288. 'tlyric' => '',
  1289. );
  1290. return json_encode($arr, JSON_UNESCAPED_UNICODE);
  1291. }
  1292. private function baidu_lyric($result)
  1293. {
  1294. $result = json_decode($result, true);
  1295. $data = array(
  1296. 'lyric' => isset($result['lrcContent']) ? $result['lrcContent'] : '',
  1297. 'tlyric' => '',
  1298. );
  1299. return json_encode($data, JSON_UNESCAPED_UNICODE);
  1300. }
  1301. private function kuwo_lyric($result)
  1302. {
  1303. $result = json_decode($result, true);
  1304. if (count($result['data']['lrclist'])) {
  1305. $kuwolrc = '';
  1306. for ($i = 0; $i < count($result['data']['lrclist']); $i++) {
  1307. $otime = $result['data']['lrclist'][$i]['time'];
  1308. $osec = explode('.', $otime)[0];
  1309. $min = str_pad(floor($osec / 60), 2, "0", STR_PAD_LEFT);
  1310. $sec = str_pad($osec - $min * 60, 2, "0", STR_PAD_LEFT);
  1311. $msec = explode('.', $otime)[1];
  1312. $olyric = $result['data']['lrclist'][$i]['lineLyric'];
  1313. $kuwolrc = $kuwolrc . '[' . $min . ':' . $sec . '.' . $msec . ']' . $olyric . "\n";
  1314. }
  1315. $arr = array(
  1316. 'lyric' => $kuwolrc,
  1317. 'tlyric' => '',
  1318. );
  1319. } else {
  1320. $arr = array(
  1321. 'lyric' => '',
  1322. 'tlyric' => '',
  1323. );
  1324. }
  1325. return json_encode($arr, JSON_UNESCAPED_UNICODE);
  1326. }
  1327. protected function format_netease($data)
  1328. {
  1329. $result = array(
  1330. 'id' => $data['id'],
  1331. 'name' => $data['name'],
  1332. 'artist' => array(),
  1333. 'album' => $data['al']['name'],
  1334. 'pic_id' => isset($data['al']['pic_str']) ? $data['al']['pic_str'] : $data['al']['pic'],
  1335. 'url_id' => $data['id'],
  1336. 'lyric_id' => $data['id'],
  1337. 'source' => 'netease',
  1338. );
  1339. if (isset($data['al']['picUrl'])) {
  1340. preg_match('/\/(\d+)\./', $data['al']['picUrl'], $match);
  1341. $result['pic_id'] = $match[1];
  1342. }
  1343. foreach ($data['ar'] as $vo) {
  1344. $result['artist'][] = $vo['name'];
  1345. }
  1346. return $result;
  1347. }
  1348. protected function format_tencent($data)
  1349. {
  1350. if (isset($data['musicData'])) {
  1351. $data = $data['musicData'];
  1352. }
  1353. $result = array(
  1354. 'id' => $data['mid'],
  1355. 'name' => $data['name'],
  1356. 'artist' => array(),
  1357. 'album' => trim($data['album']['title']),
  1358. 'pic_id' => $data['album']['mid'],
  1359. 'url_id' => $data['mid'],
  1360. 'lyric_id' => $data['mid'],
  1361. 'source' => 'tencent',
  1362. );
  1363. foreach ($data['singer'] as $vo) {
  1364. $result['artist'][] = $vo['name'];
  1365. }
  1366. return $result;
  1367. }
  1368. protected function format_xiami($data)
  1369. {
  1370. $result = array(
  1371. 'id' => $data['songId'],
  1372. 'name' => $data['songName'],
  1373. 'artist' => array(),
  1374. 'album' => $data['albumName'],
  1375. 'pic_id' => $data['songId'],
  1376. 'url_id' => $data['songId'],
  1377. 'lyric_id' => $data['songId'],
  1378. 'source' => 'xiami',
  1379. );
  1380. foreach ($data['singerVOs'] as $vo) {
  1381. $result['artist'][] = $vo['artistName'];
  1382. }
  1383. return $result;
  1384. }
  1385. protected function format_kugou($data)
  1386. {
  1387. $result = array(
  1388. 'id' => $data['hash'],
  1389. 'name' => isset($data['filename']) ? $data['filename'] : $data['fileName'],
  1390. 'artist' => array(),
  1391. 'album' => isset($data['album_name']) ? $data['album_name'] : '',
  1392. 'url_id' => $data['hash'],
  1393. 'pic_id' => $data['hash'],
  1394. 'lyric_id' => $data['hash'],
  1395. 'source' => 'kugou',
  1396. );
  1397. list($result['artist'], $result['name']) = explode(' - ', $result['name'], 2);
  1398. $result['artist'] = explode('、', $result['artist']);
  1399. return $result;
  1400. }
  1401. protected function format_baidu($data)
  1402. {
  1403. $result = array(
  1404. 'id' => $data['song_id'],
  1405. 'name' => $data['title'],
  1406. 'artist' => explode(',', $data['author']),
  1407. 'album' => $data['album_title'],
  1408. 'pic_id' => $data['song_id'],
  1409. 'url_id' => $data['song_id'],
  1410. 'lyric_id' => $data['song_id'],
  1411. 'source' => 'baidu',
  1412. );
  1413. return $result;
  1414. }
  1415. protected function format_kuwo($data)
  1416. {
  1417. $result = array(
  1418. 'id' => $data['rid'],
  1419. 'name' => $data['name'],
  1420. 'artist' => explode('&', $data['artist']),
  1421. 'album' => $data['album'],
  1422. 'pic_id' => $data['rid'],
  1423. 'url_id' => $data['rid'],
  1424. 'lyric_id' => $data['rid'],
  1425. 'source' => 'kuwo',
  1426. );
  1427. return $result;
  1428. }
  1429. }