private static async Task LoadAllAudioInfoCandidates(AudioInfo[] audioInfoCandidates, Action <AudioInfo> callback) { foreach (var candidate in audioInfoCandidates) { FastConsole.WriteInfo($"开始获取文件\"{Path.GetFileName(candidate.Path)}\"的网易云音乐ID。"); TrackInfo trackInfo; try { trackInfo = await SearchTrackAsync(candidate.Album, candidate.Track); } catch (Exception ex) { FastConsole.WriteException(ex); trackInfo = null; } if (trackInfo is null) { FastConsole.WriteWarning($"无法找到文件\"{Path.GetFileName(candidate.Path)}\"的网易云音乐ID!"); } else { FastConsole.WriteInfo($"已获取文件\"{Path.GetFileName(candidate.Path)}\"的网易云音乐ID: {trackInfo.Id}。"); candidate.TrackInfo = new TrackInfo(candidate.Track, candidate.Album, trackInfo.Id); _database.TrackInfos.Add(candidate.TrackInfo); } callback?.Invoke(candidate); FastConsole.WriteNewLine(); } }
private static async Task LoginIfNeedAsync(Arguments arguments) { if (string.IsNullOrEmpty(arguments.Account) || string.IsNullOrEmpty(arguments.Password)) { FastConsole.WriteLine("登录可避免出现大部分API错误!!!当前是免登录状态,若软件出错请尝试登录!!!", ConsoleColor.Green); FastConsole.WriteLine("强烈建议登录使用软件:\"NLyric.exe -d C:\\Music -a [email protected] -p 123456\"", ConsoleColor.Green); } else { FastConsole.WriteLine("登录中...", ConsoleColor.Green); if (await _cloudMusic.LoginAsync(arguments.Account, arguments.Password)) { FastConsole.WriteLine("登录成功!", ConsoleColor.Green); } else { FastConsole.WriteError("登录失败,输入任意键以免登录模式运行或重新运行尝试再次登录!"); try { FastConsole.ReadKey(true); } catch { } } } FastConsole.WriteNewLine(); }
public static async Task ExecuteAsync(Arguments arguments) { Task loginTask; string databasePath; AudioInfo[] audioInfos; FastConsole.WriteLine("程序会自动过滤相似度为0的结果与歌词未被收集的结果!!!", ConsoleColor.Green); loginTask = LoginIfNeedAsync(arguments); databasePath = Path.Combine(arguments.Directory, ".nlyric"); LoadDatabase(databasePath); audioInfos = LoadAllAudioInfos(arguments.Directory); await loginTask; // 登录同时进行 foreach (AudioInfo audioInfo in audioInfos) { TrackInfo trackInfo; if (!(audioInfo.TrackInfo is null)) { continue; } FastConsole.WriteInfo($"开始获取文件\"{Path.GetFileName(audioInfo.Path)}\"的网易云音乐ID。"); try { trackInfo = await SearchTrackAsync(audioInfo.Album, audioInfo.Track); } catch (Exception ex) { FastConsole.WriteException(ex); trackInfo = null; } if (trackInfo is null) { FastConsole.WriteWarning($"无法找到文件\"{Path.GetFileName(audioInfo.Path)}\"的网易云音乐ID!"); } else { FastConsole.WriteInfo($"已获取文件\"{Path.GetFileName(audioInfo.Path)}\"的网易云音乐ID: {trackInfo.Id}。"); audioInfo.TrackInfo = new TrackInfo(audioInfo.Track, audioInfo.Album, trackInfo.Id); } SaveDatabaseCore(databasePath); FastConsole.WriteNewLine(); } if (arguments.UseBatch) { PrepareAllLyrics(audioInfos); } foreach (AudioInfo audioInfo in audioInfos) { if (!(audioInfo.TrackInfo is null)) { await TryDownloadLyricAsync(audioInfo); } } SaveDatabase(databasePath); }