Exemple #1
0
        private static async Task <NcmTrack[]> GetTracksAsync(NcmAlbum ncmAlbum)
        {
            NcmTrack[] ncmTracks;

            if (!_cachedNcmTrackses.TryGetValue(ncmAlbum.Id, out ncmTracks))
            {
                List <NcmTrack> list;

                list = new List <NcmTrack>();
                foreach (NcmTrack item in await CloudMusic.GetTracksAsync(ncmAlbum.Id))
                {
                    if ((await GetLyricAsync(item.Id)).IsCollected)
                    {
                        list.Add(item);
                    }
                }
                ncmTracks = list.ToArray();
                _cachedNcmTrackses[ncmAlbum.Id] = ncmTracks;
            }
            return(ncmTracks);
        }
Exemple #2
0
        /// <summary>
        /// 同时根据专辑信息以及歌曲信息获取网易云音乐上的歌曲
        /// </summary>
        /// <param name="track"></param>
        /// <param name="album"></param>
        /// <param name="audioPath"></param>
        /// <returns></returns>
        private static async Task <NcmTrack> MapToAsync(Track track, Album album, string audioPath)
        {
            if (track == null)
            {
                throw new ArgumentNullException(nameof(track));
            }
            if (audioPath == null)
            {
                throw new ArgumentNullException(nameof(audioPath));
            }

            string     fileName;
            TrackCache trackCache;
            int        trackId;
            NcmTrack   ncmTrack;

            fileName   = Path.GetFileName(audioPath);
            trackCache = album == null?_allCaches.TrackCaches.Match(track, fileName) : _allCaches.TrackCaches.Match(track, album);

            // 有专辑信息就用专辑信息,没有专辑信息就用文件名
            if (trackCache != null)
            {
                return(new NcmTrack(track, trackCache.Id));
            }
            // 先尝试从缓存获取歌曲
            if (The163KeyHelper.TryGetMusicId(audioPath, out trackId))
            {
                // 尝试从163Key获取ID
                ncmTrack = new NcmTrack(track, trackId);
            }
            else
            {
                NcmAlbum ncmAlbum;

                ncmAlbum = null;
                if (album != null)
                {
                    // 存在专辑信息,尝试获取网易云音乐上对应的专辑
                    AlbumCache albumCache;

                    albumCache = _allCaches.AlbumCaches.Match(album);
                    if (albumCache != null)
                    {
                        ncmAlbum = new NcmAlbum(album, albumCache.Id);
                    }
                    // 先尝试从缓存获取专辑
                    if (ncmAlbum == null)
                    {
                        ncmAlbum = await MapToAsync(album);

                        if (ncmAlbum != null)
                        {
                            UpdateCache(album, ncmAlbum.Id);
                        }
                    }
                }
                if (ncmAlbum == null)
                {
                    // 没有对应的专辑信息,使用无专辑匹配
                    ncmTrack = await MapToAsync(track);
                }
                else
                {
                    // 网易云音乐收录了歌曲所在专辑
                    NcmTrack[] ncmTracks;

                    ncmTracks = (await GetTracksAsync(ncmAlbum)).Where(t => ComputeSimilarity(t.Name, track.Name, false) != 0).ToArray();
                    // 获取网易云音乐上专辑收录的歌曲
                    ncmTrack = MatchByUser(ncmTracks, track);
                    if (ncmTrack == null)
                    {
                        // 网易云音乐上的专辑可能没收录这个歌曲,不清楚为什么,但是确实存在这个情况,比如专辑id:3094396
                        ncmTrack = await MapToAsync(track);
                    }
                }
            }
            if (ncmTrack == null)
            {
                Logger.Instance.LogWarning("歌曲匹配失败!");
            }
            else
            {
                Logger.Instance.LogInfo("歌曲匹配成功!");
                if (album == null)
                {
                    UpdateCache(track, fileName, ncmTrack.Id);
                }
                else
                {
                    UpdateCache(track, album, ncmTrack.Id);
                }
            }
            return(ncmTrack);
        }