-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathplayer.php
127 lines (117 loc) · 3.33 KB
/
player.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* Created by PhpStorm.
* User: Moon
* Date: 2014/11/25 0025
* Time: 0:08
*/
include 'list.php';
function curl_get($url)
{
$refer = "http://music.163.com/";
$header[] = "Cookie: " . "appver=1.5.0.75771;";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, $refer);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
function get_playlist_info($playlist_id)
{
$url = "http://music.163.com/api/playlist/detail?id=" . $playlist_id;
return curl_get($url);
}
function get_music_info($music_id)
{
$url = "http://music.163.com/api/song/detail/?id=" . $music_id . "&ids=%5B" . $music_id . "%5D";
return curl_get($url);
}
function get_music_lyric($music_id)
{
$url = "http://music.163.com/api/song/lyric?os=pc&id=" . $music_id . "&lv=-1&kv=-1&tv=-1";
return curl_get($url);
}
function rand_music()
{
global $player_list;
$sum = count($player_list);
$id = $player_list[rand(0, $sum - 1)];
return $id;
}
function get_music_id()
{
$played = isset($_COOKIE["played"]) ? json_decode($_COOKIE["played"]) : null;
$id = rand_music();
if ($played != null) {
global $player_list;
$sum = count($player_list);
if ($sum >= 2) {
$sum = $sum * 0.5;
} else {
$sum -= 1;
}
while (in_array($id, $played)) {
$id = rand_music();
}
if (count($played) >= $sum) {
array_shift($played);
}
}
$played[] = $id;
setcookie("played", json_encode($played), time() + 3600);
return $id;
}
foreach ($playlist_list as $key) {
$json = get_playlist_info($key);
$arr = json_decode($json, true);
foreach ($arr["result"]["tracks"] as $key2) {
$id = $key2["id"];
if (!in_array($id, $player_list)) {
$player_list[] = $id;
}
}
}
//获取数据
$id = get_music_id();
$music_info = json_decode(get_music_info($id), true);
$lrc_info = json_decode(get_music_lyric($id), true);
//处理音乐信息
$play_info["cover"] = $music_info["songs"][0]["album"]["picUrl"];
$play_info["mp3"] = $music_info["songs"][0]["mp3Url"];
$play_info["mp3"] = str_replace("http://m", "http://p", $play_info["mp3"]);
$play_info["music_name"] = $music_info["songs"][0]["name"];
foreach ($music_info["songs"][0]["artists"] as $key) {
if (!isset($play_info["artists"])) {
$play_info["artists"] = $key["name"];
} else {
$play_info["artists"] .= "," . $key["name"];
}
}
//处理歌词
if (isset($lrc_info["lrc"]["lyric"])) {
$lrc = explode("\n", $lrc_info["lrc"]["lyric"]);
array_pop($lrc);
foreach ($lrc as $rows) {
$row = explode("]", $rows);
if (count($row) == 1) {
$play_info["lrc"] = "no";
break;
} else {
$lyric = array();
$col_text = end($row);
array_pop($row);
foreach ($row as $key) {
$time = explode(":", substr($key, 1));
$time = $time[0] * 60 + $time[1];
$play_info["lrc"][$time] = $col_text;
}
}
}
} else {
$play_info["lrc"] = "no";
}
echo json_encode($play_info);