-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheraser.php
137 lines (109 loc) · 4.24 KB
/
eraser.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
<?php
/**
* Created by PhpStorm.
* User: lifanko lee
* Date: 2017/10/1
* Time: 21:25
*/
require_once 'Meting.php';
use Metowolf\Meting;
$source = getParam('source', 'netease'); //源
$Eraser = new Meting($source);
$Eraser->format(true); // 启用格式化功能
function jsonKeyClear($json)
{ //去掉jsonKey的引号,适应js数据格式
$json = preg_replace('/"(\w+)"(\s*:\s*)/is', '$1$2', $json); //去掉key的双引号
return $json;
}
switch (getParam('type')) {
case 'playList':
$list = saveInfo('playList');
echo jsonKeyClear($list);
break;
case 'search': // 搜索歌曲
$s = getParam('name'); // 歌名
$limit = getParam('count', 15); // 每页显示数量
$pages = getParam('pages', 1); // 页码
$data = $Eraser->search($s, $pages, $limit);
$arr = json_decode($data, true);
$songList = array();
foreach ($arr as $value) {
$url = json_decode($Eraser->url($value['url_id']), true)['url'];
$pic = json_decode($Eraser->pic($value['pic_id']), true)['url'];
$lrc = json_decode($Eraser->lrc($value['lrc_id']), true)['lyric'];
$name = $value['name'];
$artist = $value['artist'][0];
$song = ['title' => $name, 'author' => $artist, 'url' => $url, 'pic' => $pic, 'lrc' => $lrc];
array_push($songList, $song);
}
$list = json_encode($songList);
echo jsonKeyClear($list);
if (count($songList)) { //搜索有效时才进行记录
recordHotSearch($s, $list); //搜索词记录
}
break;
case 'hotSearch': //获取热搜词
if (empty($max = getParam('max'))) { //显示的关键词数量
$max = 10;
}
$jsonHotSearch = saveInfo('hotSearch');
if (!empty($jsonHotSearch)) {
$arrHotSearch = json_decode($jsonHotSearch, true); //解析为数组格式
arsort($arrHotSearch); //按从多到少排序
$arrHotSearch = array_keys($arrHotSearch); //将关键词(键)保存为新数组
$hotWordNum = count($arrHotSearch);
for ($i = 0; $i < ($hotWordNum > $max ? $max : $hotWordNum); $i++) { //最多显示$max个热搜词
echo "<li>{$arrHotSearch[$i]}</li>";
}
} else { //文件为空
echo "<li>无</li>";
}
break;
default:
echo '橡皮音乐-接口未匹配';
}
function getParam($key, $value = '')
{
return trim($key && is_string($key) ? (isset($_POST[$key]) ? $_POST[$key] : (isset($_GET[$key]) ? $_GET[$key] : $value)) : $value);
}
function recordHotSearch($hotWord, $list)
{
$jsonHotSearch = saveInfo('hotSearch');
if (!empty($jsonHotSearch)) {
$arrHotSearch = json_decode($jsonHotSearch, true); //解析为数组格式
if (array_key_exists($hotWord, $arrHotSearch)) { //有记录则加一
$arrHotSearch[$hotWord] += 1;
if ($arrHotSearch[$hotWord] == max($arrHotSearch)) { //搜索最多的作为默认列表
saveInfo('playList', $list);
}
} else { //无记录则在数组中创建
$arrHotSearch[$hotWord] = 1;
}
$jsonHotSearch = json_encode($arrHotSearch);
} else { //文件为空
$arrHotSearch = [$hotWord => 1];
$jsonHotSearch = json_encode($arrHotSearch);
saveInfo('playList', $list);
}
saveInfo('hotSearch', $jsonHotSearch);
}
function saveInfo($dir, $new = '')
{
$filePath = $dir . '.txt';
if (file_exists($filePath)) {
if (empty($new)) { //$new为空时是读取状态,不为空时为写入状态
$fp = fopen($filePath, "r");
$str = fread($fp, filesize($filePath)); //指定读取大小,这里把整个文件内容读取出来
fclose($fp);
return $str;
} else {
$fp = fopen($filePath, "w");
flock($fp, LOCK_EX);
fwrite($fp, $new);
flock($fp, LOCK_UN);
fclose($fp);
return true;
}
}
return false;
}