Open UPman24 opened 1 year ago
感谢大佬~
本人改写了个php版本
也分享下:
kuwolrc.php
<?php
include("./libs.php");
// 定义strToHexBuffer函数,接受一个字符串参数
$get_id = 0;
if (empty($_GET['id'])) {
http_response_code(403);
echo '{"code":403,"msg":"Wrong params."}';
return;
}
$get_id = $_GET['id'];
$bufkey = strToHexBuffer('yeelion');
$bufkeylen = count($bufkey);
// 调用函数,得到返回值(16进制的buffer数组)
function buildParams($id, $isGetLyrics = true)
{
$params = "user=1,web,web,web&requester=localhost&req=1&rid=MUSIC_$id";
if ($isGetLyrics) $params .= '&lrcx=1';
$bufstr = strToHexBuffer($params);
$bufstrlen = count($bufstr);
$output = $bufstr;
$bufkeylen = $GLOBALS['bufkeylen'];
$bufkey = $GLOBALS['bufkey'];
for ($i = 0; $i < $bufstrlen; $i++) {
$output[$i] = 0;
}
$i = 0;
while ($i < $bufstrlen) {
$j = 0;
while ($j < $bufkeylen && $i < $bufstrlen) {
$output[$i] = $bufkey[$j] ^ $bufstr[$i];
$i++;
$j++;
}
}
//print_r($output);
$string = implode($output);
// 将字符串用base64方式编码
$encoded = base64_encode($string);
return $encoded;
}
function decodeLyrics($base64, $isGetLyrics = true)
{
$str = ($base64);
// echo $str;
// echo (substr($str,0,10) == "tp=content")?"true":"false";
if (substr($str, 0, 10) != 'tp=content') return false;
// $buf = strToHexBuffer($str);
$newstr = substr($str, strpos($str, "\r\n\r\n") + 4);
// echo $newstr;
$unzipstr = base64_decode(gzuncompress($newstr));
$bufStr = strToHexBuffer($unzipstr);
$bufStrlen = count($bufStr);
$output = $bufStr;
$i = 0;
$bufkeylen = $GLOBALS['bufkeylen'];
$bufkey = $GLOBALS['bufkey'];
while($i< $bufStrlen){
$j = 0;
while($j<$bufkeylen && $i<$bufStrlen){
$output[$i] = $bufStr[$i] ^ $bufkey[$j];
$i++;
$j++;
}
}
$result = implode($output);
return detect_encoding($result,'utf8');
// return true;
}
/**
* @ string 需要转换的文字
* @ encoding 目标编码
**/
function detect_encoding($string, $encoding = 'gbk')
{
$is_utf8 = preg_match('%^(?:[\x09\x0A\x0D\x20-\x7E]| [\xC2-\xDF][\x80-\xBF]| \xE0[\xA0-\xBF][\x80-\xBF] | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} | \xED[\x80-\x9F][\x80-\xBF] | \xF0[\x90-\xBF][\x80-\xBF]{2} | [\xF1-\xF3][\x80-\xBF]{3} | \xF4[\x80-\x8F][\x80-\xBF]{2} )*$%xs', $string);
if ($is_utf8 && $encoding == 'utf8') {
return $string;
} elseif ($is_utf8) {
return mb_convert_encoding($string, $encoding, "UTF-8");
} else {
return mb_convert_encoding($string, $encoding, 'gbk,gb2312,big5');
}
}
$url = "http://newlyric.kuwo.cn/newlyric.lrc?" . buildParams($get_id);
//echo $url;
$data = fetchURL($url, false);
if ($data == false) {
http_response_code(500);
echo '{"msg":"获取失败 (Step#1)","code":500}';
}
$result = decodeLyrics($data);
if ($result == false) {
http_response_code(500);
echo '{"msg":"获取失败 (Step#2)","code":500}';
}
echo $result;
libs.php
function strToHexBuffer($str)
{
// 将字符串转换为字节数组
$bytes = unpack("C*", $str);
// 定义输出数组,用于存储16进制的buffer元素
$output = array();
// 循环遍历字节数组,将每个字节转换为16进制,并添加到输出数组中
foreach ($bytes as $byte) {
// 使用sprintf函数格式化16进制,并在前面补0(如果需要)
$hex = sprintf("%02x", $byte);
// 使用pack函数将16进制转换为二进制,并添加到输出数组中
$output[] = pack("H*", $hex);
}
// 返回输出数组
return $output;
}
function fetchURL($url, $ispost = false, $postcontent = "")
{
//echo $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cache-Control: no-cache"));
curl_setopt($ch, CURLOPT_COOKIE, "NMTID=00OTrNMnhnWaFznwESKkN7usch8O14AAAGDKs_klA;");
if ($ispost) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postcontent);
}
$output = curl_exec($ch);
// echo json_encode(curl_getinfo($ch));
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($output === false) {
if (in_array(intval(curl_errno($ch)), [7, 28], true)) {
echo '{"success":"fail","msg":"连接超时,请重试。","code":3}';
return false;
//超时的处理代码
} else if (in_array(intval(curl_errno($ch)), [3], true)) {
echo '{"success":"fail","msg":"C++ CURL 不支持的 URL:' . $url . '","code":4}';
} else {
echo '{"success":"fail","msg":"[' . curl_errno($ch) . '] ' . curl_strerror(curl_errno($ch)) . '; ' . curl_error($ch) . '","code":4}';
return false;
}
}
if ($httpCode >= 400 && $httpCode < 404) {
echo '{"success":"fail","msg":"无法访问该文件,请联系站点管理员。","code":5}';
return false;
} else if ($httpCode >= 404 && $httpCode < 500) {
echo '{"success":"fail","msg":"服务器无法找到文件。","code":5}';
return false;
} else if ($httpCode >= 500) {
echo '{"success":"fail","msg":"服务器发生错误,请稍后重试。(Http Status:' . $httpCode . ')","code":6}';
return false;
}
curl_close($ch);
return $output;
}
web官网歌词页经常奔溃,不稳定,下面是改用PC客户端稳定获取歌词的方法
1.在根目录引入如下 4 个包(由于酷我PC客户端歌词是加密的所以需要引入新的包进行解密)
npm install iconv-lite
npm install needle
npm install process
npm install zlib
2.找到
app => service => lrc.ts
3.替换代码(代码是兼容的不影响其他模块)
4.原本代码如下:
5.将上面的代码替换成下面这样的代码
6.进入
.eslintrc
文件,把下面代码替换(此替换不影响其他地方功能)7.使用
npm run ci
编译成 js 代码8.最后启动项目
npm run start
9.可以进行歌词接口代替,使用桌面客户端解密歌词接口更稳定,更快。