Skip to content

Commit

Permalink
fix: #6 优化翻译歌词显示, 尝试下载歌词到本地
Browse files Browse the repository at this point in the history
  • Loading branch information
psychopasss committed May 23, 2022
1 parent 4ddb33b commit 9bd953a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
4 changes: 2 additions & 2 deletions INFO
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "QQLrc",
"displayname": "QQ音乐",
"description": "特色:根据曲名、艺术家匹配程度高低自动排序;可自动添加中文翻译",
"version": "1.2.1",
"version": "1.2.2",
"site": "http://music.qq.com",
"module": "qqmusic.php",
"type": "lyric",
"class": "ZainQQLrc"
"class": "QQLrc"
}
37 changes: 23 additions & 14 deletions qqmusic.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@
* @author PsychoPass (https://github.com/psychopasss/Synology-Lrc-Plugin-For-QQ-Music)
* @see https://global.download.synology.com/download/Document/DeveloperGuide/AS_Guide.pdf
*/
class ZainQQLrc {
class QQLrc {
private $mArtist = "";
private $mTitle = "";
public $mOriginTitle = "";

///////////////////////////// Synology API ///////////////////////////////////////

/**
* Searches for a song with the artist and title, and returns the matching result list. Result is sorted based on similarity of artist and title.
*/
public function getLyricsList($artist, $title, $info) {
$this->mOriginTitle = $title;
$artist = trim($artist);
$title = trim($title);
$this->mArtist = $artist;
Expand Down Expand Up @@ -72,10 +74,10 @@ public function getLyricsList($artist, $title, $info) {
$foundArray = array();
foreach ($songArray as $song) {
$elem = array(
'id' => $song['songid'],
'artist' => $song['singer'][0]["name"],
'title' => $song['songname'],
'alt' => $song['alias'][0] . "; Album: " . $song['albumname']
'id' => key_exists('songid',$song)?$song['songid']:'',
'artist' => key_exists('singer',$song)?$song['singer'][0]["name"]:'',
'title' => key_exists('songname',$song)?$song['songname']:'',
'alt' => key_exists('alias',$song)?$song['alias'][0] . "; ":'' . "Album: " . $song['albumname']
);
// Find the best match artist from all artists belong to a song
$max = 0;
Expand Down Expand Up @@ -110,6 +112,10 @@ public function getLyrics($id, $info) {

$info->addLyrics($lrc, $id);

// save lrc file
$myfile = fopen($this->mOriginTitle.".lrc","w");
fwrite($myfile, $lrc);

return true;
}

Expand Down Expand Up @@ -142,10 +148,13 @@ private function downloadLyric($music_id) {

// Find matching translation
$trans = "";
// error_log("key=".$key);
if (!$this->isNullOrEmptyString($key)) {
$time = $this->getTimeFromTag($key);
for ($i = $transCursor; $i < count($transLines); $i++) {
$tKey = $transLines[$i]['tag'];
// error_log(json_encode($transLines[$i]));
// error_log("tKey=".$tKey);
if ($this->getTimeFromTag($tKey) > $time) { // trans time tag is greater than org, no match found
$transCursor = $i;
break;
Expand All @@ -159,6 +168,8 @@ private function downloadLyric($music_id) {
break;
}
}
} else {
$transCursor++;
}

if (!$this->isNullOrEmptyString($trans)) { // $key is empty when it's not time tag, just metadata
Expand Down Expand Up @@ -212,7 +223,7 @@ private function processLrcLine($lrc) {
}
array_push($result, array(
'tag' => $key,
'lrc' => $value
'lrc' => $value=='//'?'':$value
));
}
return $result;
Expand All @@ -236,7 +247,7 @@ private static function isNullOrEmptyString($question){
return (!isset($question) || trim($question)==='');
}

///////////////////////////// Netease API ///////////////////////////////////////
///////////////////////////// QQ-Music API ///////////////////////////////////////

/**
* Searches for a song based on title.
Expand Down Expand Up @@ -292,16 +303,13 @@ function __construct() {
$this->items = array();
}

public function addLyrics($lyric, $id, $title) {
public function addLyrics($lyric, $id) {
printf("</br>");
printf("song id: %s\n", $id);
printf("</br>\n");
printf("== lyric ==\n");
printf("%s\n", $lyric);
printf("** END of lyric **\n\n");

$myfile = fopen($title.".lrc","w");
fwrite($myfile, $lyric);
}

public function addTrackInfoToList($artist, $title, $id, $prefix) {
Expand Down Expand Up @@ -335,17 +343,18 @@ function getFirstItem() {
/**
* Main
*/
$title = "be my forever";
$title = "封茗囧菌 - マインドブランド";
$artist = "";
echo "Trying to find lyrics for ['$title'] by artist ['$artist'] ...</br>\n";

$testObj = new TestObj();
$downloader = (new ReflectionClass("ZainQQLrc"))->newInstance();
$downloader = (new ReflectionClass("QQLrc"))->newInstance();
$count = $downloader->getLyricsList($artist, $title, $testObj);
if ($count > 0) {
$item = $testObj->getFirstItem();
if (array_key_exists('id', $item)) {
$downloader->getLyrics($item['id'], $testObj, $title);
$downloader->mOriginTitle = $title;
$downloader->getLyrics($item['id'], $testObj);
} else {
echo "\nno id to query lyric\n";
}
Expand Down

0 comments on commit 9bd953a

Please sign in to comment.