Skip to content

Commit

Permalink
重構
Browse files Browse the repository at this point in the history
  • Loading branch information
jim60105 committed Dec 20, 2022
1 parent 01e0918 commit 7c2d13a
Show file tree
Hide file tree
Showing 11 changed files with 555 additions and 468 deletions.
100 changes: 100 additions & 0 deletions Downloader/LyricsDownloader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using Lyrics.Models;
using NeteaseCloudMusicApi;
using Newtonsoft.Json.Linq;
using System.Text.RegularExpressions;

namespace Lyrics.Downloader;

public class LyricsDownloader
{
private readonly CloudMusicApi _cloudMusicApi;

public LyricsDownloader(CloudMusicApi cloudMusicApi)
{
_cloudMusicApi = cloudMusicApi;
}

public async Task DownloadLyricAndWriteFileAsync(int songId)
{
if (songId <= 0)
{
throw new ArgumentException("SongId invalid.", nameof(songId));
}

// Find local .lrc file.
if (File.Exists($"Lyrics/{songId}.lrc"))
{
Console.WriteLine($"Lyric file {songId}.lrc already exists.");
return;
}

await Task.Delay(TimeSpan.FromMilliseconds(new Random().Next(500, 1500)));

// Download lyric by id at Netease Cloud Music.
string? lyricString = await GetLyricAsync(songId);

if (string.IsNullOrEmpty(lyricString))
{
throw new Exception("Can't find lyric.");
}

if (lyricString.Contains("纯音乐,请欣赏")
// 沒有時間資訊
|| !Regex.IsMatch(lyricString, @"\[\d{2}:\d{2}.\d{1,5}\]")
// 小於6行
|| lyricString.Split('\n').Length < 6)
{
throw new Exception("Found an invalid lyric.");
}

await File.WriteAllTextAsync($"Lyrics/{songId}.lrc", lyricString, System.Text.Encoding.UTF8);
Console.WriteLine($"Write new lyric file {songId}.lrc.");
}

public async Task<(int songId, string songName)> GetSongIdAsync(ISong song, int offset = 0)
{
if (string.IsNullOrEmpty(song.Title))
{
throw new ArgumentException("Song Title invalid");
}

(bool isOk, JObject json) = await _cloudMusicApi.RequestAsync(CloudMusicApiProviders.Search,
new Dictionary<string, object> {
{ "keywords", song.Title},
{ "type", 1 },
{ "limit", 1 },
{ "offset", offset }
});
if (!isOk || null == json)
{
Console.Error.WriteLine($"API response ${json?["code"] ?? "error"} while getting song id.");
return default;
}

json = (JObject)json["result"];
return null == json
|| json["songs"] is not IEnumerable<JToken> result
? default
: result.Select(t => ((int)t["id"], (string)t["name"]))
.FirstOrDefault();
}

public async Task<string?> GetLyricAsync(int songId)
{
(bool isOk, JObject json) = await _cloudMusicApi.RequestAsync(CloudMusicApiProviders.Lyric,
new Dictionary<string, object> {
{ "id", songId }
});
if (!isOk || null == json)
{
Console.Error.WriteLine($"API response ${json?["code"] ?? "error"} while getting lyric.");
return null;
}

return (bool?)json["uncollected"] != true
&& (bool?)json["nolyric"] != true
? json["lrc"]["lyric"].ToString().Trim()
: null;
}

}
9 changes: 3 additions & 6 deletions Lyrics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
</PropertyGroup>

<ItemGroup>
<Compile Remove="Playlists\AutoGenerator\AutoGenerator\**" />
<Compile Remove="Playlists\QuonTama\CreateRadioQTamaSubtitles\**" />
<EmbeddedResource Remove="Playlists\AutoGenerator\AutoGenerator\**" />
<EmbeddedResource Remove="Playlists\QuonTama\CreateRadioQTamaSubtitles\**" />
<None Remove="Playlists\AutoGenerator\AutoGenerator\**" />
<None Remove="Playlists\QuonTama\CreateRadioQTamaSubtitles\**" />
<Compile Remove="Playlists\**" />
<EmbeddedResource Remove="Playlists\**" />
<None Remove="Playlists\**" />
</ItemGroup>

<ItemGroup>
Expand Down
147 changes: 0 additions & 147 deletions PrepareLyrics.cs

This file was deleted.

Loading

0 comments on commit 7c2d13a

Please sign in to comment.