From e35a849b2c2d4c3660387e7ed8922fa2736d3ec0 Mon Sep 17 00:00:00 2001 From: Light <47911535+LightAPIs@users.noreply.github.com> Date: Thu, 16 Jun 2022 17:35:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=AD=8C=E8=AF=8D=E7=BF=BB=E8=AF=91=E5=86=85=E5=AE=B9=E5=9C=A8?= =?UTF-8?q?=E6=8B=BC=E6=8E=A5=E6=97=B6=E5=8F=AF=E8=83=BD=E4=BC=9A=E9=81=97?= =?UTF-8?q?=E6=BC=8F=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 由于翻译歌词的时间标签与原文歌词存在误差所导致; 优化部分代码,移除正则表达式的使用 --- src/common.php | 85 --------------------------- src/qqHandler.php | 19 ++---- src/qqSource.php | 2 +- src/qqTranslation.php | 132 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 139 insertions(+), 99 deletions(-) delete mode 100644 src/common.php create mode 100644 src/qqTranslation.php diff --git a/src/common.php b/src/common.php deleted file mode 100644 index 03646c2..0000000 --- a/src/common.php +++ /dev/null @@ -1,85 +0,0 @@ - $key, 'lrc' => $value)); - } - return $result; - } - - public static function getChineseTranslationLrc($orgLrc, $transLrc) { - $resultLrc = ''; - $orgLines = AumCommon::processLrcLine($orgLrc); - $transLines = AumCommon::processLrcLine($transLrc); - - $transCursor = 0; - foreach ($orgLines as $line) { - $key = $line['tag']; - $value = $line['lrc']; - $resultLrc .= $key . $value; - - $trans = ''; - if ($key !== '') { - $time = AumCommon::getTimeFromTag($key); - for ($i = $transCursor; $i < count($transLines); $i++) { - $tKey = $transLines[$i]['tag']; - if (AumCommon::getTimeFromTag($tKey) > $time) { - $transCursor = $i; - break; - } - - $tValue = $transLines[$i]['lrc']; - if ($key === $tKey) { - $transCursor = $i + 1; - $trans = $tValue; - break; - } - } - } - - if ($trans !== '') { - $resultLrc .= " 【{$trans}】"; - } - $resultLrc .= "\n"; - } - return $resultLrc; - } -} \ No newline at end of file diff --git a/src/qqHandler.php b/src/qqHandler.php index 64bf69e..99670e4 100644 --- a/src/qqHandler.php +++ b/src/qqHandler.php @@ -1,5 +1,5 @@ 0) { $transLyric = base64_decode($json['trans']); - $lyric = AumCommon::getChineseTranslationLrc($lyric, $transLyric); + $tl = new AumQQTranslation($lyric, $transLyric); + $lyric = $tl->getChineseTranslationLrc(); } return $lyric; } - - public static function getCleanJsonData($data) { - if (preg_match('/^\w+\((\{.+})\)\s*$/', $data)) { - preg_match('/^\w+\((\{.+})\)\s*$/', $data, $matches); - return $matches[1]; - } - return $data; - } } diff --git a/src/qqSource.php b/src/qqSource.php index a791446..c6f2494 100644 --- a/src/qqSource.php +++ b/src/qqSource.php @@ -17,7 +17,7 @@ public function getLyricsList($artist, $title, $info) { $this->mTitle = $title; $this->lowTitle = strtolower($title); - $list = AumQQHandler::search($title); + $list = AumQQHandler::search($title, $artist); if (count($list) === 0) { return 0; } diff --git a/src/qqTranslation.php b/src/qqTranslation.php new file mode 100644 index 0000000..b485e60 --- /dev/null +++ b/src/qqTranslation.php @@ -0,0 +1,132 @@ +orgLrc = $orgLrc; + $this->transLrc = $transLrc; + } + + private function getLrcTime($str) { + $key = strstr($str, ']', true); + if ($key === false) { + return ''; + } + + return $key . ']'; + } + + private function getLrcText($str, $key) { + if ($key === '') { + return $str; + } + + return str_replace($key, '', $str); + } + + private function isValidLrcTime($str) { + if (trim($str) === '' || $str[0] !== '[') { + return false; + } + + $keyLen = strlen($str); + if ($keyLen < 9 || $keyLen > 11) { + return false; + } + for ($count = 1; $count < $keyLen - 1; $count++) { + $ch = $str[$count]; + if ($ch !== ':' && $ch !== '.' && !is_numeric($ch)) { + return false; + } + } + + return true; + } + + private function isValidLrcText($str) { + if (trim($str) === '' || trim($str) === '//') { + return false; + } + return true; + } + + private function getTimeFromTag($tag) { + $min = (int)substr($tag, 1, 2); + $sec = (int)substr($tag, 4, 2); + $mil = (int)substr($tag, 7, strlen($tag) - 8); + return $mil + $sec * 1000 + $min * 60 * 1000; + } + + // 若 $lTime > $rTime, 返回 1; $lTime < $rTime, 返回 -1; $lTime == $rTime, 返回 0 + private function compareTime($lTime, $rTime) { + $subVal = $lTime - $rTime; + // 允许有 1ms 的误差 + if ($subVal > 1) { + return 1; + } elseif ($subVal < -1) { + return -1; + } else { + return 0; + } + } + + private function processLrcLine($lrc) { + $result = array(); + foreach (explode("\n", $lrc) as $line) { + $line = trim($line); + $key = $this->getLrcTime($line); + $value = $this->getLrcText($line, $key); + if (!$this->isValidLrcTime($key) || !$this->isValidLrcText($value)) { + $key = ''; + $value = $line; + } + array_push($result, array('tag' => $key, 'lrc' => $value)); + } + return $result; + } + + public function getChineseTranslationLrc() + { + $resultLrc = ''; + $orgLines = $this->processLrcLine($this->orgLrc); + $transLines = $this->processLrcLine($this->transLrc); + + $transCursor = 0; + foreach ($orgLines as $line) { + $key = $line['tag']; + $value = $line['lrc']; + $resultLrc .= $key . $value; + + $trans = ''; + if ($key !== '') { + $time = $this->getTimeFromTag($key); + for ($i = $transCursor; $i < count($transLines); $i++) { + $tKey = $transLines[$i]['tag']; + if ($tKey === '') { + continue; + } + + $tTime = $this->getTimeFromTag($tKey); + if ($this->compareTime($tTime, $time) > 0) { + $transCursor = $i; + break; + } + + $tValue = $transLines[$i]['lrc']; + if ($this->compareTime($tTime, $time) == 0) { + $transCursor = $i + 1; + $trans = $tValue; + break; + } + } + } + + if ($trans !== '') { + $resultLrc .= " 【{$trans}】"; + } + $resultLrc .= "\n"; + } + return $resultLrc; + } +} \ No newline at end of file