Skip to content

Commit

Permalink
fix: 修复部分歌词翻译内容在拼接时可能会遗漏的问题
Browse files Browse the repository at this point in the history
由于翻译歌词的时间标签与原文歌词存在误差所导致;
优化部分代码,移除正则表达式的使用
  • Loading branch information
Light authored and Light committed Jun 16, 2022
1 parent 718c5ad commit e35a849
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 99 deletions.
85 changes: 0 additions & 85 deletions src/common.php

This file was deleted.

19 changes: 6 additions & 13 deletions src/qqHandler.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
require('common.php');
require('qqTranslation.php');

class AumQQHandler {
public static $siteSearch = 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp?aggr=1&format=json&cr=1&flag_qc=0&p=1&n=30&w=';
Expand All @@ -26,13 +26,13 @@ public static function getContent($url, $defaultValue) {
if ($result === false) {
return $defaultValue;
} else {
return AumQQHandler::getCleanJsonData($result);
return $result;
}
}

public static function search($word) {
public static function search($title, $artist) {
$results = array();
$url = AumQQHandler::$siteSearch . urlencode($word);
$url = AumQQHandler::$siteSearch . urlencode($title);
$jsonContent = AumQQHandler::getContent($url, '{"data":{"song":{"list":[]}}}');
$json = json_decode($jsonContent, true);

Expand Down Expand Up @@ -68,16 +68,9 @@ public static function downloadLyric($songId) {
// Chinese translation
if (strlen($json['trans']) > 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;
}
}
2 changes: 1 addition & 1 deletion src/qqSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
132 changes: 132 additions & 0 deletions src/qqTranslation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

class AumQQTranslation {
private $orgLrc;
private $transLrc;
public function __construct($orgLrc, $transLrc) {
$this->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;
}
}

0 comments on commit e35a849

Please sign in to comment.