diff --git a/NLyric/NLyricImpl.cs b/NLyric/NLyricImpl.cs index 8e02080..ab57189 100644 --- a/NLyric/NLyricImpl.cs +++ b/NLyric/NLyricImpl.cs @@ -27,9 +27,11 @@ internal static class NLyricImpl { private static AllCaches _allCaches; public static async Task ExecuteAsync(Arguments arguments) { + Logger.Instance.LogInfo("登录可避免出现大部分API错误!!!", ConsoleColor.Green); Logger.Instance.LogInfo("程序会自动过滤相似度为0的结果与歌词未被收集的结果!!!", ConsoleColor.Green); Logger.Instance.LogNewLine(); _allCachesPath = Path.Combine(arguments.Directory, ".nlyric"); + await LoginIfNeedAsync(); LoadLocalCaches(); foreach (string audioPath in Directory.EnumerateFiles(arguments.Directory, "*", SearchOption.AllDirectories)) { string lrcPath; @@ -51,6 +53,37 @@ public static async Task ExecuteAsync(Arguments arguments) { SaveLocalCaches(); } + private static async Task LoginIfNeedAsync() { + do { + string userInput; + + Logger.Instance.LogInfo("如果需要登录,请输入Y,反之输入N"); + userInput = Console.ReadLine().Trim().ToUpperInvariant(); + if (userInput == "Y") { + string account; + string password; + + Console.WriteLine("请输入账号"); + account = Console.ReadLine(); + Console.WriteLine("请输入密码"); + password = Console.ReadLine(); + if (await CloudMusic.LoginAsync(account, password)) { + Logger.Instance.LogInfo("登录成功", ConsoleColor.Green); + Logger.Instance.LogNewLine(); + break; + } + else { + Logger.Instance.LogError("登录失败,请重试"); + Logger.Instance.LogNewLine(); + } + } + else if (userInput == "N") + break; + else + Logger.Instance.LogWarning("输入有误,请重新输入!"); + } while (true); + } + private static bool CanSkip(string audioPath, string lrcPath) { string extension; diff --git a/NLyric/Ncm/CloudMusic.cs b/NLyric/Ncm/CloudMusic.cs index 5a3bc59..dad3ff4 100644 --- a/NLyric/Ncm/CloudMusic.cs +++ b/NLyric/Ncm/CloudMusic.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text.RegularExpressions; using System.Threading.Tasks; using NeteaseCloudMusicApi; using Newtonsoft.Json.Linq; @@ -11,6 +12,19 @@ namespace NLyric.Ncm { public static class CloudMusic { private static readonly CloudMusicApi _api = new CloudMusicApi(); + public static async Task LoginAsync(string account, string password) { + Dictionary queries; + bool isPhone; + bool isOk; + + queries = new Dictionary(); + isPhone = Regex.Match(account, "^[0-9]+$").Success; + queries[isPhone ? "phone" : "email"] = account; + queries["password"] = password; + (isOk, _) = await _api.RequestAsync(isPhone ? CloudMusicApiProviders.LoginCellphone : CloudMusicApiProviders.Login, queries); + return isOk; + } + public static async Task SearchTrackAsync(Track track, int limit, bool withArtists) { List keywords; bool isOk; @@ -34,7 +48,7 @@ public static async Task SearchTrackAsync(Track track, int limit, bo throw new ApplicationException(nameof(CloudMusicApiProviders.Search) + " API错误"); json = (JObject)json["result"]; if (json is null) - throw new ApplicationException(nameof(CloudMusicApiProviders.Search) + " API错误"); + throw new ArgumentException($"\"{string.Join(" ", keywords)}\" 中有关键词被屏蔽"); if ((int)json["songCount"] == 0) return Array.Empty(); return ((JArray)json["songs"]).Select(t => ParseTrack(t, false)).ToArray(); @@ -63,7 +77,7 @@ public static async Task SearchAlbumAsync(Album album, int limit, bo throw new ApplicationException(nameof(CloudMusicApiProviders.Search) + " API错误"); json = (JObject)json["result"]; if (json is null) - throw new ApplicationException(nameof(CloudMusicApiProviders.Search) + " API错误"); + throw new ArgumentException($"\"{string.Join(" ", keywords)}\" 中有关键词被屏蔽"); if ((int)json["albumCount"] == 0) return Array.Empty(); return ((JArray)json["albums"]).Select(t => ParseAlbum(t)).ToArray(); diff --git a/NLyric/Settings.json b/NLyric/Settings.json index 4ab2274..9f0037d 100644 --- a/NLyric/Settings.json +++ b/NLyric/Settings.json @@ -13,7 +13,8 @@ "Separators": "|;,/\\&:", // 分隔符,用于分割歌手名 "WholeWordReplace": { "sh0ut": "shØut", - "あやぽんず*": "あやぽんず*" + "あやぽんず*": "あやぽんず", + "あやぽんず*": "あやぽんず" }, // 前面是被替换的词,后面是要替换成的词,比如歌名"sh0ut",搜索的时候会被替换成"shØut"来搜索,网易云音乐部分歌曲歌手名收录有问题,只能这么办 "Limit": 15 // 搜索结果数量 },