예제 #1
0
        /// <summary>
        /// 获取版权以及下载链接
        /// </summary>
        /// <param name="session"></param>
        /// <param name="bitRate"></param>
        /// <param name="songIds"></param>
        /// <returns></returns>
        private static JObject _GetPlayerUrl(NeteaseSession session, Quality bitRate = Quality.SuperQuality, params long[] songIds)
        {
            IDictionary <string, object> data = new Dictionary <string, object>
            {
                ["ids"] = songIds,
                ["br"]  = (int)bitRate
            };

            CryptoHelper.Encrypted encrypted = CryptoHelper.WebApiEncrypt(data);
            string json = session == null?HttpHelper.HttpPost("https://music.163.com/weapi/song/enhance/player/url", encrypted.GetFormdata(), userAgent : DefaultUserAgent) :
                          session.Session.HttpPost("https://music.163.com/weapi/song/enhance/player/url", encrypted.GetFormdata(), userAgent: DefaultUserAgent);

            JObject j = JObject.Parse(json);

            if (j["code"].ToObject <int>() == 200)
            {
                return(j);
            }
            else
            {
                NotImplementedException exception = new NotImplementedException($"未知的服务器返回");
                exception.Data.Add("Response", j.ToString());
                throw exception;
            }
        }
예제 #2
0
        /// <summary>
        /// 按给定的关键词搜索单曲
        /// </summary>
        /// <param name="keyWords">关键词</param>
        /// <param name="pageSize">本次搜索返回的实例个数上限</param>
        /// <param name="offset">偏移量</param>
        public static SongInfo[] SearchSongs(NeteaseSession session, string keyWords, int pageSize = 30, int offset = 0)
        {
            string  json = Search(keyWords, SearchType.Song, pageSize, offset);
            JObject j    = JObject.Parse(json);

            if (j["code"].ToObject <int>() == 200)
            {
                SongInfo[] result = j["result"]["songs"].Select(p => new SongInfo(p)).ToArray();
                IDictionary <long, bool> canPlayDic = CheckMusicStatus(session, result.Select(p => p.Id).ToArray());
                foreach (SongInfo song in result)
                {
                    if (canPlayDic.TryGetValue(song.Id, out bool canPlay))
                    {
                        song.CanPlay = canPlay;
                    }
                }
                return(result);
            }
            else
            {
                NotImplementedException exception = new NotImplementedException($"未知的服务器返回");
                exception.Data.Add("Response", j.ToString());
                throw exception;
            }
        }
        /// <summary>
        /// 获取歌单内的所有单曲
        /// </summary>
        /// <exception cref="NotImplementedException"/>
        /// <param name="id">歌单Id</param>
        public static SongInfo[] GetPlayList(NeteaseSession session, long id)
        {
            IDictionary <string, object> data = new Dictionary <string, object>()
            {
                ["id"] = id,
                ["n"]  = 100000,
                ["s"]  = 8
            };

            CryptoHelper.Encrypted encrypted = CryptoHelper.WebApiEncrypt(data);
            string  json = HttpHelper.HttpPost("https://music.163.com/weapi/v3/playlist/detail", encrypted.GetFormdata(), userAgent: DefaultUserAgent);
            JObject j    = JObject.Parse(json);

            if (j["code"].ToObject <int>() == 200)
            {
                SongInfo[] result = j["playlist"]["tracks"].Select(p => new SongInfo(p)).ToArray();
                IDictionary <long, bool> canPlayDic = CheckMusicStatus(session, result.Select(p => p.Id).ToArray());
                foreach (SongInfo song in result)
                {
                    if (canPlayDic.TryGetValue(song.Id, out bool canPlay))
                    {
                        song.CanPlay = canPlay;
                    }
                }
                return(result);
            }
            else
            {
                NotImplementedException exception = new NotImplementedException($"未知的服务器返回");
                exception.Data.Add("Response", j.ToString());
                throw exception;
            }
        }
예제 #4
0
        /// <summary>
        /// 获取歌单内的所有单曲
        /// </summary>
        /// <exception cref="NotImplementedException"/>
        /// <param name="id">歌单Id</param>
        public static SongInfo[] GetPlayList(NeteaseSession session, long id)
        {
            IDictionary <string, object> data = new Dictionary <string, object>()
            {
                ["id"] = id,
                ["n"]  = 100000,
                ["s"]  = 8
            };

            CryptoHelper.Encrypted encrypted = CryptoHelper.WebApiEncrypt(data);
            string json = session == null?HttpHelper.HttpPost("https://music.163.com/weapi/v3/playlist/detail", encrypted.GetFormdata(), userAgent: DefaultUserAgent) :
                              session.Session.HttpPost("https://music.163.com/weapi/v3/playlist/detail", encrypted.GetFormdata(), userAgent: DefaultUserAgent);

            JObject j    = JObject.Parse(json);
            int     code = j["code"].ToObject <int>();

            switch (code)
            {
            case 200:
            {
                SongInfo[] result = j["playlist"]["tracks"].Select(p => new SongInfo(p)).ToArray();
                IDictionary <long, bool> canPlayDic = CheckMusicStatus(session, result.Select(p => p.Id).ToArray());
                foreach (SongInfo song in result)
                {
                    if (canPlayDic.TryGetValue(song.Id, out bool canPlay))
                    {
                        song.CanPlay = canPlay;
                    }
                }
                return(result);
            }

            case 401:
            {
                throw new InvalidOperationException("给定的歌单为私有歌单,非歌单所有者无权访问");
            }

            case 404:
            {
                throw new ArgumentException("给定的歌单ID无效", nameof(id));
            }

            default:
            {
                NotImplementedException exception = new NotImplementedException($"未知的服务器返回");
                exception.Data.Add("Response", j.ToString());
                throw exception;
            }
            }
        }
예제 #5
0
        /// <summary>
        /// 批量获取单曲下载链接
        /// </summary>
        /// <param name="bitRate">比特率上限</param>
        /// <param name="songIds">单曲IDs</param>
        public static IDictionary <long, DownloadSongInfo> GetSongsUrl(NeteaseSession session, Quality bitRate = Quality.SuperQuality, params long[] songIds)
        {
            JObject j = _GetPlayerUrl(session, bitRate, songIds);

            return(j["data"].ToDictionary(p => p["id"].ToObject <long>(), p => new DownloadSongInfo(p["id"].ToObject <long>(), p["br"].ToObject <int>(), bitRate, p["url"].ToString(), p["type"].ToString())));
        }
예제 #6
0
        /// <summary>
        /// 检查给定ID对应的音乐有无版权
        /// </summary>
        /// <param name="bitRate">比特率</param>
        /// <param name="songIds">音乐IDs</param>
        /// <returns></returns>
        public static IDictionary <long, bool> CheckMusicStatus(NeteaseSession session, params long[] songIds)
        {
            JObject j = _GetPlayerUrl(session, Quality.SuperQuality, songIds);

            return(j["data"].ToDictionary(p => p["id"].ToObject <long>(), p => p["code"].ToObject <int>() == 200));
        }