|
|
|
@ -1,7 +1,7 @@
@@ -1,7 +1,7 @@
|
|
|
|
|
<?php |
|
|
|
|
|
|
|
|
|
$DEBUG = false; |
|
|
|
|
$NEED_TRANSLATION = false; |
|
|
|
|
$DEBUG = true; |
|
|
|
|
$NEED_TRANSLATION = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -94,7 +94,6 @@ class LudysuNetEaseLrc {
@@ -94,7 +94,6 @@ class LudysuNetEaseLrc {
|
|
|
|
|
* Downloads a file with the specific ID |
|
|
|
|
*/ |
|
|
|
|
public function getLyrics($id, $info) { |
|
|
|
|
//TODO combine translated lrc |
|
|
|
|
$lrc = $this->downloadLyric($id); |
|
|
|
|
if ($this->isNullOrEmptyString($lrc)) { |
|
|
|
|
printf("download lyrics from server failed\n"); |
|
|
|
@ -154,13 +153,73 @@ class LudysuNetEaseLrc {
@@ -154,13 +153,73 @@ class LudysuNetEaseLrc {
|
|
|
|
|
private function downloadLyric($music_id) { |
|
|
|
|
// lv = original version; tv = translated version; kv = karaoke version, rarely available. Set value to 0 if don't want |
|
|
|
|
$url = "http://music.163.com/api/song/lyric?os=pc&id=" . $music_id . "&lv=-1&kv=0&tv=-1"; |
|
|
|
|
$response = $this->download($url); |
|
|
|
|
$response = '{"sgc":false,"sfy":false,"qfy":false,"transUser":{"id":479938456,"status":99,"demand":1,"userid":124108722,"nickname":"丶吟游","uptime":1495604317130},"lrc":{"version":2,"lyric":"[by:丶吟游]\n[00:00.00] 作曲 : 田淵智也\n[00:00.27] 作词 : LiSA\n[00:00.81]\n[00:21.81]そっと 吐き出す ため息を吸い込んだ 後悔は苦い味残して\n[00:31.37]いつも なんで? 肝心なこと言えないまま 次の朝日が顔だしてる\n[00:39.81]\n[00:40.14]嫌になった運命を ナイフで切り刻んで\n[00:46.26]もう一度やり直したら キミに出会えないかも\n[00:55.36]\n[00:55.89]僕の声が響いた瞬間に始まる 命のリミット 心臓がカウントしてる\n[01:07.22]叶えても叶えても 終わらない願い\n[01:16.05]汗をかいて走った 世界の秒針は いつか止まった僕を置いていく\n[01:27.48]あと何回キミと笑えるの?\n[01:33.82]試してるんだ 僕を Catch the Moment\n[01:37.82]\n[01:47.62]一個幸せを数えるたびに 変わっていく未来に怯えてしまうけど\n[01:57.05]\n[01:57.38]愛情の種を大切に育てよう\n[02:04.20]分厚い雲も やがて突き破るかな\n[02:11.43]\n[02:11.69]キミの声が響いた 僕の全身を通って 心臓のドアをノックしてる\n[02:23.03]「臆病」でも開けちゃうんだよ 信じたいから\n[02:31.74]何にもないと思ったはずの足元に いつか深く確かな根を生やす\n[02:43.24]嵐の夜が来たとしても 揺らいだりはしない\n[02:52.05]\n[02:52.29]何度でも\n[02:52.91]追いついたり 追い越したり キミがふいに分かんなくなって\n[02:57.98]息をしたタイミングが合うだけで 嬉しくなったりして\n[03:04.83]集めた一秒を 永遠にして行けるかな\n[03:16.45]\n[03:27.67]僕の声が響いた瞬間に始まる 命のリミット 心臓がカウントしてる\n[03:38.78]叶えても叶えても 終わらない願い\n[03:47.70]汗をかいて走った 世界の秒針が いつか止まった僕を置いていく\n[03:59.05]あと何回キミと笑えるの?\n[04:05.35]試してるんだ 僕を Catch the Moment\n[04:09.43]\n[04:10.60]逃さないよ僕は\n[04:12.86]この瞬間を掴め Catch the Moment\n[04:20.14]\n"},"klyric":{"version":0,"lyric":""},"tlyric":{"version":2,"lyric":"[00:21.81]轻轻吐出的叹息 又默默咽回腹中 反悔总是留下苦涩的余味\n[00:31.37]这到底是为什么?重要的话还未来得及说出口 翌日的朝阳却已露出了脸庞\n[00:40.14]深恶痛疾的命运 用利刃切成粉碎\n[00:46.26]即使一切能从头来过 或许你我也无法邂逅\n[00:55.89]在我的声音响起的瞬间 心脏也随即开始倒数着生命的极限\n[01:07.22]无论多少次得偿所愿 愿望却始终不见尽头\n[01:16.05]挥洒汗水竭力奔走 世界的秒针 终有一日会抛下裹足不前的我\n[01:27.48]余生我又能与你共笑多少次?\n[01:33.82]这正是上帝赋予我的严峻考验 把握这一瞬\n[01:47.62]细数每一个降临的幸福 就会对渐渐改变的未来感到恐惧\n[01:57.38]用心的培育爱情的种子\n[02:04.20]天那边厚重的乌云 不久也会云开雾散\n[02:11.69]你的声音响起 贯穿我的全身 叩响心脏之门\n[02:23.03]就算胆怯 也要把心扉打开 因为我想要坚信\n[02:31.74]曾以为 空无一物的脚边 不知何时 长出深根扎于大地\n[02:43.24]就算风雨交加的夜晚来袭 我也不会有任何动摇\n[02:52.29]无论多少次\n[02:52.91]我都会追寻着你 追赶上你 就算蓦然之间 变得不再懂你\n[02:57.98]只要有那么一瞬间与你情投意合 我就无比欣喜\n[03:04.83]所收集的每一秒 能否拼凑成永恒\n[03:27.67]在我的声音响起的瞬间 心脏也随即开始倒数着生命的极限\n[03:38.78]无论多少次得偿所愿 愿望却始终不见尽头\n[03:47.70]挥洒汗水竭力奔走 世界的秒针 终有一日会抛下裹足不前的我\n[03:59.05]余生我又能与你共笑多少次?\n[04:05.35]这正是上帝赋予我的严峻考验 把握这一瞬\n[04:10.60]我不会再错过\n[04:12.86]紧握这个瞬间"},"code":200}';//$this->download($url); |
|
|
|
|
if ($this->isNullOrEmptyString($response)) { |
|
|
|
|
return NULL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$json = json_decode($response, true); |
|
|
|
|
return $json['lrc']['lyric']; |
|
|
|
|
$orgLrc = $json['lrc']['lyric']; |
|
|
|
|
$transLrc = $json['tlyric']['lyric']; // Chinese translation lyric, but only some songs have |
|
|
|
|
|
|
|
|
|
global $NEED_TRANSLATION; |
|
|
|
|
$resultLrc = $orgLrc; |
|
|
|
|
if ($NEED_TRANSLATION && !$this->isNullOrEmptyString($transLrc)) { |
|
|
|
|
$resultLrc = ""; |
|
|
|
|
$orgLines = $this->processLrcLine($orgLrc); |
|
|
|
|
$transLines = $this->processLrcLine($transLrc); |
|
|
|
|
var_dump($orgLines); |
|
|
|
|
|
|
|
|
|
foreach ($orgLines as $key => $value) { |
|
|
|
|
$resultLrc .= $key . $value; |
|
|
|
|
|
|
|
|
|
// Find matching translation |
|
|
|
|
$trans = ""; |
|
|
|
|
// $lastMatchCursor = 0; |
|
|
|
|
// for ($i = $lastMatchCursor; $i < count($transLines); $i++) { |
|
|
|
|
// // Check for matching time tag |
|
|
|
|
// if ($key === $transLines[0]) { |
|
|
|
|
// $lastMatchCursor = $i; |
|
|
|
|
// $trans = $transLines[1]; |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
if (!$this->isNullOrEmptyString($key) && $this->isNullOrEmptyString($trans)) { // $key is empty when it's not time tag, just metadata |
|
|
|
|
$resultLrc .= " 【" . $trans . "】\n"; |
|
|
|
|
} |
|
|
|
|
$resultLrc .= "\n"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// var_dump($resultLrc); |
|
|
|
|
} |
|
|
|
|
return $resultLrc; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private function processLrcLine($lrc) { |
|
|
|
|
$result = array(); |
|
|
|
|
foreach (explode("\n", $lrc) as $line) { |
|
|
|
|
$key = substr($line, 0, 10); |
|
|
|
|
$value = substr($line, 10, strlen($line) - 10); |
|
|
|
|
if (!$this->isValidLrcTime($key)) { |
|
|
|
|
$key = ""; |
|
|
|
|
$value = $line; |
|
|
|
|
} |
|
|
|
|
array_push($result, array($key => $value)); |
|
|
|
|
} |
|
|
|
|
return $result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private function isValidLrcTime($str) { |
|
|
|
|
if ($this->isNullOrEmptyString($str) || strlen($str) != 10 || $str[0] !== "[" || $str[9] != "]") { |
|
|
|
|
return FALSE; |
|
|
|
|
} |
|
|
|
|
for ($count = 1; $count < 9; $count++) { |
|
|
|
|
$ch = $str[$count]; |
|
|
|
|
if ($ch !== ":" && $ch !== "." && !is_numeric($ch)) { |
|
|
|
|
return FALSE; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return TRUE; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Function for basic field validation (present and neither empty nor only white space |
|
|
|
@ -237,20 +296,20 @@ if ($DEBUG == true) {
@@ -237,20 +296,20 @@ if ($DEBUG == true) {
|
|
|
|
|
|
|
|
|
|
$testObj = new TestObj(); |
|
|
|
|
$downloader = (new ReflectionClass("LudysuNetEaseLrc"))->newInstance(); |
|
|
|
|
$count = $downloader->getLyricsList($artist, $title, $testObj); |
|
|
|
|
if ($count > 0) { |
|
|
|
|
$item = $testObj->getFirstItem(); |
|
|
|
|
// $count = $downloader->getLyricsList($artist, $title, $testObj); |
|
|
|
|
// if ($count > 0) { |
|
|
|
|
// $item = $testObj->getFirstItem(); |
|
|
|
|
|
|
|
|
|
if (array_key_exists('id', $item)) { |
|
|
|
|
// if (array_key_exists('id', $item)) { |
|
|
|
|
$downloader->getLyrics($item['id'], $testObj); |
|
|
|
|
} else { |
|
|
|
|
echo "\nno id to query lyric\n"; |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
echo " ****************************\n"; |
|
|
|
|
echo " *** Failed to find lyric ***\n"; |
|
|
|
|
echo " ****************************\n"; |
|
|
|
|
} |
|
|
|
|
// } else { |
|
|
|
|
// echo "\nno id to query lyric\n"; |
|
|
|
|
// } |
|
|
|
|
// } else { |
|
|
|
|
// echo " ****************************\n"; |
|
|
|
|
// echo " *** Failed to find lyric ***\n"; |
|
|
|
|
// echo " ****************************\n"; |
|
|
|
|
// } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|