/// <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; } }
/// <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; } }
/// <summary> /// 根据网易云用户ID获取其生日(若用户未填写,返回DateTime.MinValue) /// </summary> /// <param name="userId">用户ID</param> /// <returns>生日</returns> public static DateTime GetNeteaseMusicUserBirthDay(int userId) { JObject data = new JObject(new JProperty("uid", userId), new JProperty("limit", 30), new JProperty("offset", 0)); CryptoHelper.Encrypted encrypted = CryptoHelper.WebApiEncrypt(data); string json = HttpHelper.HttpPost("https://music.163.com/weapi/user/playlist", encrypted.GetFormdata(), userAgent: DefaultUserAgent); JObject j = JObject.Parse(json); long? birthday = j["playlist"].FirstOrDefault(p => p["creator"]["userId"].ToObject <int>() == userId)?["creator"]["birthday"].ToObject <long>(); return(birthday.HasValue ? Utils.UnixTimeStamp2DateTime(birthday.Value) : default(DateTime)); }
/// <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; } } }
/// <summary> /// 按给定的信息执行搜索 /// <para> /// 由于类型不定,本方法将返回json /// </para> /// </summary> /// <param name="keyWords">关键词</param> /// <param name="type">搜索类型</param> /// <param name="pageSize">返回的json中,实体个数上限</param> /// <param name="offset">偏移量</param> /// <returns>服务器返回的Json</returns> public static string Search(string keyWords, SearchType type, int pageSize = 30, int offset = 0) { IDictionary <string, object> data = new Dictionary <string, object> { ["s"] = keyWords, ["type"] = (int)type, ["limit"] = pageSize, ["offset"] = offset }; CryptoHelper.Encrypted encrypted = CryptoHelper.WebApiEncrypt(data); string json = HttpHelper.HttpPost("https://music.163.com/weapi/search/get", encrypted.GetFormdata(), userAgent: DefaultUserAgent); return(json); }
/// <summary> /// 获取给定单曲ID的歌词 /// </summary> /// <exception cref="NotImplementedException"/> /// <param name="songId">单曲ID</param> public static LyricInfo GetLyric(long songId) { IDictionary <string, object> data = new Dictionary <string, object> { ["csrf_token"] = "", ["id"] = songId.ToString(), ["lv"] = -1, ["tv"] = -1 }; CryptoHelper.Encrypted encrypted = CryptoHelper.WebApiEncrypt(data); string json = HttpHelper.HttpPost("https://music.163.com/weapi/song/lyric", encrypted.GetFormdata(), userAgent: DefaultUserAgent); JObject j = JObject.Parse(json); if (j["code"].ToObject <int>() == 200) { string lyricText = j["lrc"]?["lyric"]?.ToString(); string translatedLyricText = j["tlyric"]?["lyric"]?.ToString(); LyricInfo lyric = null; if (!string.IsNullOrEmpty(lyricText)) { lyric = new LyricInfo(lyricText); } if (!string.IsNullOrEmpty(translatedLyricText)) { lyric.AppendLrc(translatedLyricText); } return(lyric); } else { NotImplementedException exception = new NotImplementedException($"未知的服务器返回"); exception.Data.Add("Response", j.ToString()); throw exception; } }