forked from injahow/meting-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
230 lines (202 loc) · 7.35 KB
/
index.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
// 设置API路径
define('API_URI', api_uri());
// 设置中文歌词
define('TLYRIC', true);
// 设置歌单文件缓存及时间
define('CACHE', false);
define('CACHE_TIME', 86400);
// 设置短期缓存-需要安装apcu
define('APCU_CACHE', false);
// 设置AUTH密钥-更改'meting-secret'
define('AUTH', false);
define('AUTH_SECRET', 'meting-secret');
if (!isset($_GET['type']) || !isset($_GET['id'])) {
include __DIR__ . '/public/index.php';
exit;
}
$server = isset($_GET['server']) ? $_GET['server'] : 'netease';
$type = $_GET['type'];
$id = $_GET['id'];
if (AUTH) {
$auth = isset($_GET['auth']) ? $_GET['auth'] : '';
if (in_array($type, ['url', 'pic', 'lrc'])) {
if ($auth == '' || $auth != auth($server . $type . $id)) {
http_response_code(403);
exit;
}
}
}
// 数据格式
if (in_array($type, ['song', 'playlist'])) {
header('content-type: application/json; charset=utf-8;');
} else if (in_array($type, ['name', 'lrc', 'artist'])) {
header('content-type: text/plain; charset=utf-8;');
}
// 允许跨站
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
// include __DIR__ . '/vendor/autoload.php';
// you can use 'Meting.php' instead of 'autoload.php'
include __DIR__ . '/src/Meting.php';
use Metowolf\Meting;
$api = new Meting($server);
$api->format(true);
// 设置cookie
/*if ($server == 'netease') {
$api->cookie('os=pc; osver=Microsoft-Windows-10-Professional-build-10586-64bit; appver=2.0.3.131777; channel=netease; MUSIC_U=****** ; __remember_me=true');
}*/
if ($type == 'playlist') {
if (CACHE) {
$file_path = __DIR__ . '/cache/playlist/' . $server . '_' . $id . '.json';
if (file_exists($file_path)) {
if ($_SERVER['REQUEST_TIME'] - filemtime($file_path) < CACHE_TIME) {
echo file_get_contents($file_path);
exit;
}
}
}
$data = $api->playlist($id);
if ($data == '[]') {
echo '{"error":"unknown playlist id"}';
exit;
}
$data = json_decode($data);
$playlist = array();
foreach ($data as $song) {
$playlist[] = array(
'name' => $song->name,
'artist' => implode('/', $song->artist),
'url' => API_URI . '?server=' . $song->source . '&type=url&id=' . $song->url_id . (AUTH ? '&auth=' . auth($song->source . 'url' . $song->url_id) : ''),
'pic' => API_URI . '?server=' . $song->source . '&type=pic&id=' . $song->pic_id . (AUTH ? '&auth=' . auth($song->source . 'pic' . $song->pic_id) : ''),
'lrc' => API_URI . '?server=' . $song->source . '&type=lrc&id=' . $song->lyric_id . (AUTH ? '&auth=' . auth($song->source . 'lrc' . $song->lyric_id) : '')
);
}
$playlist = json_encode($playlist);
if (CACHE) {
// ! mkdir /cache/playlist
file_put_contents($file_path, $playlist);
}
echo $playlist;
} else {
$need_song = !in_array($type, ['url', 'pic', 'lrc']);
if ($need_song && !in_array($type, ['name', 'artist', 'song'])) {
echo '{"error":"unknown type"}';
exit;
}
if (APCU_CACHE) {
$apcu_time = $type == 'url' ? 600 : 36000;
$apcu_type_key = $server . $type . $id;
if (apcu_exists($apcu_type_key)) {
$data = apcu_fetch($apcu_type_key);
return_data($type, $data);
}
if ($need_song) {
$apcu_song_id_key = $server . 'song_id' . $id;
if (apcu_exists($apcu_song_id_key)) {
$song = apcu_fetch($apcu_song_id_key);
}
}
}
if (!$need_song) {
$data = song2data($api, null, $type, $id);
} else {
if (!isset($song)) $song = $api->song($id);
if ($song == '[]') {
echo '{"error":"unknown song"}';
exit;
}
if (APCU_CACHE) {
apcu_store($apcu_song_id_key, $song, $apcu_time);
}
$data = song2data($api, json_decode($song)[0], $type, $id);
}
if (APCU_CACHE) {
apcu_store($apcu_type_key, $data, $apcu_time);
}
return_data($type, $data);
}
function api_uri() // static
{
return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . strtok($_SERVER['REQUEST_URI'], '?');
}
function auth($name)
{
return hash_hmac('sha1', $name, AUTH_SECRET);
}
function song2data($api, $song, $type, $id)
{
$data = '';
switch ($type) {
case 'name':
$data = $song->name;
break;
case 'artist':
$data = implode('/', $song->artist);
break;
case 'url':
$m_url = json_decode($api->url($id, 320))->url;
if ($m_url == '') break;
// url format
if ($api->server == 'netease') {
if ($m_url[4] != 's') $m_url = str_replace('http', 'https', $m_url);
}
$data = $m_url;
break;
case 'pic':
$data = json_decode($api->pic($id, 90))->url;
break;
case 'lrc':
$lrc_data = json_decode($api->lyric($id));
if ($lrc_data->lyric == '') {
$lrc = '[00:00.00]这似乎是一首纯音乐呢,请尽情欣赏它吧!';
} else if ($lrc_data->tlyric == '') {
$lrc = $lrc_data->lyric;
} else if (TLYRIC) { // lyric_cn
$lrc_arr = explode("\n", $lrc_data->lyric);
$lrc_cn_arr = explode("\n", $lrc_data->tlyric);
$lrc_cn_map = array();
foreach ($lrc_cn_arr as $i => $v) {
if ($v == '') continue;
$line = explode(']', $v, 2);
// 格式化处理
$line[1] = trim(preg_replace('/\s\s+/', ' ', $line[1]));
$lrc_cn_map[$line[0]] = $line[1];
unset($lrc_cn_arr[$i]);
}
foreach ($lrc_arr as $i => $v) {
if ($v == '') continue;
$key = explode(']', $v, 2)[0];
if (!empty($lrc_cn_map[$key]) && $lrc_cn_map[$key] != '//') {
$lrc_arr[$i] .= ' (' . $lrc_cn_map[$key] . ')';
unset($lrc_cn_map[$key]);
}
}
$lrc = implode("\n", $lrc_arr);
} else {
$lrc = $lrc_data->lyric;
}
$data = $lrc;
break;
case 'song':
$data = json_encode(array(array(
'name' => $song->name,
'artist' => implode('/', $song->artist),
'url' => API_URI . '?server=' . $song->source . '&type=url&id=' . $song->url_id . (AUTH ? '&auth=' . auth($song->source . 'url' . $song->url_id) : ''),
'pic' => API_URI . '?server=' . $song->source . '&type=pic&id=' . $song->pic_id . (AUTH ? '&auth=' . auth($song->source . 'pic' . $song->pic_id) : ''),
'lrc' => API_URI . '?server=' . $song->source . '&type=lrc&id=' . $song->lyric_id . (AUTH ? '&auth=' . auth($song->source . 'lrc' . $song->lyric_id) : '')
)));
break;
}
if ($data == '') exit;
return $data;
}
function return_data($type, $data)
{
if (in_array($type, ['url', 'pic'])) {
header('Location: ' . $data);
} else {
echo $data;
}
exit;
}