Пример #1
0
        public static async Task <NcmTrack[]> GetTracksAsync(int albumId)
        {
            if (_isLoggedIn)
            {
                bool    isOk;
                JObject json;

                (isOk, json) = await _api.RequestAsync(CloudMusicApiProviders.Album, new Dictionary <string, string> {
                    { "id", albumId.ToString() }
                });

                if (!isOk)
                {
                    throw new ApplicationException(nameof(CloudMusicApiProviders.Album) + " API错误");
                }
                return(json["songs"].Select(t => ParseTrack(t, true)).ToArray());
            }
            else
            {
                JObject json;

                json = await NormalApi.GetAlbumAsync(albumId);

                return(json["album"]["songs"].Select(t => ParseTrack(t, false)).ToArray());
            }
        }
Пример #2
0
        public static async Task <NcmAlbum[]> SearchAlbumAsync(Album album, int limit, bool withArtists)
        {
            List <string> keywords;
            bool          isOk;
            JObject       json;
            JArray        albums;

            keywords = new List <string>();
            if (album.Name.Length != 0)
            {
                keywords.Add(album.Name);
            }
            if (withArtists)
            {
                keywords.AddRange(album.Artists);
            }
            if (keywords.Count == 0)
            {
                throw new ArgumentException("专辑信息无效");
            }
            for (int i = 0; i < keywords.Count; i++)
            {
                keywords[i] = keywords[i].WholeWordReplace();
            }
            if (_isLoggedIn)
            {
                (isOk, json) = await _api.RequestAsync(CloudMusicApiProviders.Search, new Dictionary <string, string> {
                    { "keywords", string.Join(" ", keywords) },
                    { "type", "10" },
                    { "limit", limit.ToString() }
                });
            }
            else
            {
                json = await NormalApi.SearchAsync(keywords, NormalApi.SearchType.Album, limit);

                isOk = true;
            }
            if (!isOk)
            {
                throw new ApplicationException(nameof(CloudMusicApiProviders.Search) + " API错误");
            }
            json = (JObject)json["result"];
            if (json is null)
            {
                throw new KeywordForbiddenException(string.Join(" ", keywords));
            }
            albums = json["albums"] as JArray;
            if (albums is null)
            {
                return(Array.Empty <NcmAlbum>());
            }
            // albumCount不可信,搜索"U-87 陈奕迅"返回albums有内容,但是albumCount为0
            return(albums.Select(t => ParseAlbum(t)).ToArray());
        }
Пример #3
0
        public static async Task <NcmTrack[]> SearchTrackAsync(Track track, int limit, bool withArtists)
        {
            List <string> keywords;
            bool          isOk;
            JObject       json;
            JArray        songs;

            keywords = new List <string>();
            if (track.Name.Length != 0)
            {
                keywords.Add(track.Name);
            }
            if (withArtists)
            {
                keywords.AddRange(track.Artists);
            }
            if (keywords.Count == 0)
            {
                throw new ArgumentException("歌曲信息无效");
            }
            for (int i = 0; i < keywords.Count; i++)
            {
                keywords[i] = keywords[i].WholeWordReplace();
            }
            if (_isLoggedIn)
            {
                (isOk, json) = await _api.RequestAsync(CloudMusicApiProviders.Search, new Dictionary <string, string> {
                    { "keywords", string.Join(" ", keywords) },
                    { "type", "1" },
                    { "limit", limit.ToString() }
                });
            }
            else
            {
                json = await NormalApi.SearchAsync(keywords, NormalApi.SearchType.Track, limit);

                isOk = true;
            }
            if (!isOk)
            {
                throw new ApplicationException(nameof(CloudMusicApiProviders.Search) + " API错误");
            }
            json = (JObject)json["result"];
            if (json is null)
            {
                throw new KeywordForbiddenException(string.Join(" ", keywords));
            }
            songs = json["songs"] as JArray;
            if (songs is null)
            {
                return(Array.Empty <NcmTrack>());
            }
            return(songs.Select(t => ParseTrack(t, false)).ToArray());
        }
Пример #4
0
        public static async Task <NcmLyric> GetLyricAsync(int trackId)
        {
            bool    isOk;
            JObject json;
            Lrc     rawLrc;
            int     rawVersion;
            Lrc     translatedLrc;
            int     translatedVersion;

            if (_isLoggedIn)
            {
                (isOk, json) = await _api.RequestAsync(CloudMusicApiProviders.Lyric, new Dictionary <string, string> {
                    { "id", trackId.ToString() }
                });
            }
            else
            {
                json = await NormalApi.GetLyricAsync(trackId);

                isOk = true;
            }
            if (!isOk)
            {
                throw new ApplicationException(nameof(CloudMusicApiProviders.Lyric) + " API错误");
            }
            if ((bool?)json["uncollected"] == true)
            {
                // 未收录
                return(new NcmLyric(trackId, false, false, null, 0, null, 0));
            }
            if ((bool?)json["nolyric"] == true)
            {
                // 纯音乐
                return(new NcmLyric(trackId, true, true, null, 0, null, 0));
            }
            (rawLrc, rawVersion) = ParseLyric(json["lrc"]);
            (translatedLrc, translatedVersion) = ParseLyric(json["tlyric"]);
            return(new NcmLyric(trackId, true, false, rawLrc, rawVersion, translatedLrc, translatedVersion));
        }