Пример #1
0
 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();
     }
 }
Пример #2
0
        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);
        }
Пример #3
0
        private static AudioInfo[] LoadAllAudioInfos(string directory)
        {
            return(Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories).Where(audioPath => {
                string lrcPath;

                lrcPath = Path.ChangeExtension(audioPath, ".lrc");
                return !CanSkip(audioPath, lrcPath);
            }).AsParallel().AsOrdered().Select(audioPath => {
                TagLib.File audioFile;
                Tag tag;
                AudioInfo audioInfo;
                TrackInfo trackInfo;
                int trackId;

                audioFile = null;
                audioInfo = new AudioInfo {
                    Path = audioPath
                };
                try {
                    audioFile = TagLib.File.Create(audioPath);
                    tag = audioFile.Tag;
                    if (Album.HasAlbumInfo(tag))
                    {
                        audioInfo.Album = new Album(tag, true);
                    }
                    audioInfo.Track = new Track(tag);
                }
                catch (Exception ex) {
                    FastConsole.WriteError("无效音频文件!");
                    FastConsole.WriteException(ex);
                    return null;
                }
                finally {
                    audioFile?.Dispose();
                }
                trackInfo = _database.TrackInfos.Match(audioInfo.Album, audioInfo.Track);
                if (!(trackInfo is null))
                {
                    audioInfo.TrackInfo = trackInfo;
                    return audioInfo;
                }
                // 尝试从数据库获取歌曲
                if (The163KeyHelper.TryGetTrackId(tag, out trackId))
                {
                    audioInfo.TrackInfo = new TrackInfo(audioInfo.Track, audioInfo.Album, trackId);
                    return audioInfo;
                }
                // 尝试从163Key获取ID
                return audioInfo;
            }).Where(t => !(t is null)).ToArray());
        }