public async void getSongs(String path) { string listIdPath = path + "listid.ini"; StreamReader listIdReader = new StreamReader(listIdPath); String listId = listIdReader.ReadLine(); CloudMusicApi api = new CloudMusicApi(); bool isOk; JObject json = new JObject(); Dictionary <String, String> param = new Dictionary <string, string> { { "id", listId } }; (isOk, json) = await api.RequestAsync(CloudMusicApiProviders.PlaylistDetail, param); var songList = json.SelectTokens("playlist.tracks[*]").ToDictionary(t => t["id"], t => t["name"]); //foreach (var item in songList) //{ // Console.WriteLine(item.Key + ":" + item.Value); //} string filePath = path + "songs.json"; StreamWriter writer = File.CreateText(filePath); writer.Write(JObject.FromObject(songList)); writer.Flush(); writer.Close(); }
/// <summary> /// 用api搜音乐 /// </summary> /// <param name="keyword"></param> /// <param name="offset"></param> /// <returns></returns> public async Task <List <MusicInfo> > SearchMusicAsync2(string keyword, int offset = 0) { List <MusicInfo> songs = new List <MusicInfo>(); using (CloudMusicApi api = new CloudMusicApi()) { try { bool isOk; JObject json; (isOk, json) = await api.RequestAsync(CloudMusicApiProviders.Search, new Dictionary <string, string> { { "keywords", keyword.ToString() }, { "offset", offset.ToString() } }); if (!isOk) { throw new ApplicationException($"获取专辑信息失败: {json}"); } //var a = json.ToString(); var songsTemp = json["result"]["songs"].ToArray(); songs = new List <MusicInfo>(); foreach (var t in songsTemp) { //var b = t.ToString(); var artistsTemp = t["artists"].Select(p => (string)p["name"]).ToArray(); var artistsIdsTemp = t["artists"].Select(p => ((int)p["id"]).ToString()).ToArray(); var duration = TimeSpan.FromMilliseconds((int)t["duration"]); songs.Add( new MusicInfo { Id = (int)t["id"], Name = t["name"].ToString(), Album = t["album"]?["name"].ToString(), AlbumId = (int)t["album"]["id"], Artist = string.Join("/", artistsTemp), ArtistIds = artistsIdsTemp[0], /*先做成只能搜索单个歌手*/ Duration = $"{duration.TotalMinutes:00}:{duration.Seconds:00}", File = @"http://music.163.com/song/media/outer/url?id=" + t["id"].ToString() + ".mp3", MvId = (int)t["mvid"], Type = 1/*网页音乐*/ }); } Console.WriteLine(); } catch { } } return(songs); }
public static async void Search(SoraMessage e, string keyword) { var search = await CloudMusicApi.SearchSong(keyword); if (search != null && search.Length > 0) { Play(e, search[0].Id); } else { await e.ReplyToOriginal("未搜索到相关结果"); } }
public async Task Search(string keyword) { var search = await CloudMusicApi.SearchSong(keyword); if (search != null && search.Length > 0) { await Play(search[0].Id); } else { await ReplyAsync("未搜索到相关结果"); } }
public static async void Play(SoraMessage e, long id) { var detail = await CloudMusicApi.GetSongDetail(id); if (detail == null) { await e.ReplyToOriginal("曲目信息获取失败"); } else { var url = await CloudMusicApi.GetSongUrl(id, 128000); if (url.Id == detail.Id && url.Id == id) { await e.Reply(CQCode.CQImage(detail.Album.GetPicUrl(512, 512)), new StringBuilder().AppendLine() .AppendLine("♬ " + detail.Name) .AppendLine("✎ " + string.Join(" / ", detail.Artists)) .AppendLine(detail.Url) .Append("√ 曲目链接已解析,正在下载中……") .ToString()); await e.Reply(CQCode.CQRecord(url.Url)); } else { await e.Reply(CQCode.CQImage(detail.Album.GetPicUrl(512, 512)), new StringBuilder().AppendLine() .AppendLine("♬ " + detail.Name) .AppendLine("✎ " + string.Join(" / ", detail.Artists)) .AppendLine(detail.Url) .Append("× 解析曲目链接失败") .ToString()); } } }
public async Task Play(long id) { var msg = await ReplyAsync("曲目搜索中……"); var detail = await CloudMusicApi.GetSongDetail(id); if (detail == null) { await msg.ModifyAsync(x => x.Content = "曲目信息获取失败"); } else { var url = await CloudMusicApi.GetSongUrl(id); if (url.Id == detail.Id && url.Id == id) { await msg.ModifyAsync(x => x.Content = new StringBuilder() .AppendLine(detail.Album.GetPicUrl(512, 512)) .AppendLine("♬ " + detail.Name) .AppendLine("✎ " + string.Join(" / ", detail.Artists)) .AppendLine(detail.Url) .Append("√ 曲目链接:" + url.Url) .ToString()); } else { await msg.ModifyAsync(x => x.Content = new StringBuilder() .AppendLine(detail.Album.GetPicUrl(512, 512)) .AppendLine("♬ " + detail.Name) .AppendLine("✎ " + string.Join(" / ", detail.Artists)) .AppendLine(detail.Url) .Append("× 解析曲目链接失败") .ToString()); } } }
public async void getUserListAsync(string path) { string uIdPath = path + "uid.ini"; while (!File.Exists(uIdPath)) { } StreamReader listIdReader = new StreamReader(uIdPath); String uid = listIdReader.ReadLine(); CloudMusicApi api = new CloudMusicApi(); bool isOk; JObject json = new JObject(); Dictionary <String, String> param = new Dictionary <string, string> { { "uid", uid } }; (isOk, json) = await api.RequestAsync(CloudMusicApiProviders.UserPlaylist, param); var playList = json.SelectTokens("playlist[*]").ToDictionary(t => t["id"], t => t["name"]); //foreach (var item in playList) //{ // Console.WriteLine(item.Key + ":" + item.Value); //} string filePath = path + "playlist.json"; StreamWriter writer = File.CreateText(filePath); writer.Write(JObject.FromObject(playList)); writer.Flush(); String defalutPlayId = playList.FirstOrDefault().Key.ToString(); String listIdPath = path + "listid.ini"; writer = File.CreateText(listIdPath); writer.Write(defalutPlayId); writer.Flush(); writer.Close(); }
private static async Task Main() { try { var api = new CloudMusicApi(); /******************** 登录 ********************/ while (true) { var queries = new Dictionary <string, object>(); Console.WriteLine("请输入账号(邮箱或手机)"); string account = Console.ReadLine(); bool isPhone = Regex.Match(account, "^[0-9]+$").Success; queries[isPhone ? "phone" : "email"] = account; Console.WriteLine("请输入密码"); queries["password"] = Console.ReadLine(); if (!CloudMusicApi.IsSuccess(await api.RequestAsync(isPhone ? CloudMusicApiProviders.LoginCellphone : CloudMusicApiProviders.Login, queries, false))) { Console.WriteLine("登录失败,账号或密码错误"); } else { break; } } Console.WriteLine("登录成功"); Console.WriteLine(); /******************** 登录 ********************/ /******************** 获取账号信息 ********************/ var json = await api.RequestAsync(CloudMusicApiProviders.LoginStatus); long uid = (long)json["profile"]["userId"]; Console.WriteLine($"账号ID: {uid}"); Console.WriteLine($"账号昵称: {json["profile"]["nickname"]}"); Console.WriteLine(); /******************** 获取账号信息 ********************/ /******************** 获取我喜欢的音乐 ********************/ json = await api.RequestAsync(CloudMusicApiProviders.UserPlaylist, new Dictionary <string, object> { ["uid"] = uid }); json = await api.RequestAsync(CloudMusicApiProviders.PlaylistDetail, new Dictionary <string, object> { ["id"] = json["playlist"][0]["id"] }); int[] trackIds = json["playlist"]["trackIds"].Select(t => (int)t["id"]).ToArray(); json = await api.RequestAsync(CloudMusicApiProviders.SongDetail, new Dictionary <string, object> { ["ids"] = trackIds }); Console.WriteLine($"我喜欢的音乐({trackIds.Length} 首):"); foreach (var song in json["songs"]) { Console.WriteLine($"{string.Join(",", song["ar"].Select(t => t["name"]))} - {song["name"]}"); } Console.WriteLine(); /******************** 获取我喜欢的音乐 ********************/ /******************** 获取我的关注 ********************/ /******************** 获取我的关注 ********************/ json = await api.RequestAsync(CloudMusicApiProviders.UserFollows, new Dictionary <string, object> { ["uid"] = uid }); Console.WriteLine($"我的关注:"); foreach (var user in json["follow"]) { Console.WriteLine(user["nickname"]); } Console.WriteLine(); /******************** 获取我的动态 ********************/ json = await api.RequestAsync(CloudMusicApiProviders.UserEvent, new Dictionary <string, object> { ["uid"] = uid }); Console.WriteLine($"我的动态:"); foreach (var @event in json["events"]) { Console.WriteLine(JObject.Parse((string)@event["json"])["msg"]); } Console.WriteLine(); /******************** 获取我的动态 ********************/ /******************** 退出登录 ********************/ json = await api.RequestAsync(CloudMusicApiProviders.Logout); Console.WriteLine("退出登录成功"); Console.WriteLine(); /******************** 退出登录 ********************/ } catch (Exception ex) { Console.WriteLine(ex); } Console.ReadKey(true); }
private static async Task Main() { using (CloudMusicApi api = new CloudMusicApi()) { try { bool isOk; JObject json; int uid; int[] trackIds; /******************** 登录 ********************/ do { Dictionary <string, string> queries; string account; bool isPhone; queries = new Dictionary <string, string>(); Console.WriteLine("请输入账号(邮箱或手机)"); account = Console.ReadLine(); isPhone = Regex.Match(account, "^[0-9]+$").Success; queries[isPhone ? "phone" : "email"] = account; Console.WriteLine("请输入密码"); queries["password"] = Console.ReadLine(); (isOk, json) = await api.RequestAsync(isPhone?CloudMusicApiProviders.LoginCellphone : CloudMusicApiProviders.Login, queries); if (!isOk) { Console.WriteLine("登录失败,账号或密码错误"); } } while (!isOk); Console.WriteLine("登录成功"); Console.WriteLine(); /******************** 登录 ********************/ /******************** 获取账号信息 ********************/ (isOk, json) = await api.RequestAsync(CloudMusicApiProviders.LoginStatus, CloudMusicApi.EmptyQueries); if (!isOk) { throw new ApplicationException($"获取账号信息失败: {json}"); } uid = (int)json["profile"]["userId"]; Console.WriteLine($"账号ID: {uid}"); Console.WriteLine($"账号昵称: {json["profile"]["nickname"]}"); Console.WriteLine(); /******************** 获取账号信息 ********************/ /******************** 获取我喜欢的音乐 ********************/ (isOk, json) = await api.RequestAsync(CloudMusicApiProviders.UserPlaylist, new Dictionary <string, string> { { "uid", uid.ToString() } }); if (!isOk) { throw new ApplicationException($"获取用户歌单失败: {json}"); } (isOk, json) = await api.RequestAsync(CloudMusicApiProviders.PlaylistDetail, new Dictionary <string, string> { { "id", json["playlist"][0]["id"].ToString() } }); if (!isOk) { throw new ApplicationException($"获取歌单详情失败: {json}"); } trackIds = json["playlist"]["trackIds"].Select(t => (int)t["id"]).ToArray(); (isOk, json) = await api.RequestAsync(CloudMusicApiProviders.SongDetail, new Dictionary <string, string> { { "ids", string.Join(",", trackIds) } }); if (!isOk) { throw new ApplicationException($"获取歌曲详情失败: {json}"); } Console.WriteLine($"我喜欢的音乐 ({trackIds.Length} 首):"); foreach (JObject song in json["songs"]) { Console.WriteLine($"{string.Join(",", song["ar"].Select(t => t["name"]))} - {song["name"]}"); } Console.WriteLine(); /******************** 获取我喜欢的音乐 ********************/ /******************** 退出登录 ********************/ (isOk, json) = await api.RequestAsync(CloudMusicApiProviders.Logout, CloudMusicApi.EmptyQueries); if (!isOk) { throw new ApplicationException($"退出登录失败: {json}"); } Console.WriteLine("退出登录成功"); Console.WriteLine(); /******************** 退出登录 ********************/ } catch (Exception ex) { Console.WriteLine(ex); } } Console.ReadKey(true); }