コード例 #1
0
ファイル: CloudMusic.cs プロジェクト: yinghekeji/NLyric
        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());
        }
コード例 #2
0
ファイル: CloudMusic.cs プロジェクト: yinghekeji/NLyric
        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());
        }