From c251699528e078a71cd6a227b636b14725ef056b Mon Sep 17 00:00:00 2001 From: Controllerdestiny <523321293@qq.co> Date: Tue, 24 Dec 2024 19:01:17 +0800 Subject: [PATCH] update music and clear code --- Music/Attributes/SongExt.cs | 8 +--- Music/Attributes/SongFormat.cs | 8 +--- Music/Music.cs | 18 ++++----- Music/QQ/Enums/QrcodeLoginType.cs | 8 +--- Music/QQ/Internal/MusicToken/TokenInfo.cs | 3 +- Music/QQ/Internal/Qimei.cs | 10 ++--- Music/QQ/Internal/QuerSong/QuerySongData.cs | 5 --- Music/QQ/Internal/Req.cs | 5 --- Music/QQ/Internal/Response.cs | 1 - Music/QQ/Internal/Search/Body.cs | 6 --- Music/QQ/Internal/Search/ReqData.cs | 5 --- Music/QQ/Internal/Search/Song/Hotness.cs | 5 --- Music/QQ/Internal/Search/Song/KSong.cs | 5 --- Music/QQ/Internal/Search/Song/MV.cs | 5 --- Music/QQ/Internal/Search/Song/Pay.cs | 5 --- Music/QQ/Internal/Search/Song/SingerItem.cs | 5 --- Music/QQ/Internal/Search/Song/SongData.cs | 5 --- Music/QQ/Internal/Search/Song/SongFile.cs | 5 --- Music/QQ/Internal/Search/Song/Volume.cs | 5 --- Music/QQ/Internal/Search/Songs.cs | 5 --- Music/QQ/Internal/User/HomePageData.cs | 2 +- Music/QQ/Login.cs | 22 +++++------ Music/QQ/Music_QQ.cs | 41 ++++++++------------- Music/QQ/Utils.cs | 2 +- Music/WangYi/Music_163.cs | 2 +- Music/WangYi/PlaylistsData.cs | 2 +- OnlineReward/Config.cs | 4 +- 27 files changed, 49 insertions(+), 148 deletions(-) diff --git a/Music/Attributes/SongExt.cs b/Music/Attributes/SongExt.cs index 241ad4c..5fc5ca8 100644 --- a/Music/Attributes/SongExt.cs +++ b/Music/Attributes/SongExt.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Music.Attributes; +namespace Music.Attributes; [AttributeUsage(AttributeTargets.Field)] public class SongExt(string ext) : Attribute diff --git a/Music/Attributes/SongFormat.cs b/Music/Attributes/SongFormat.cs index 954064f..3ce396d 100644 --- a/Music/Attributes/SongFormat.cs +++ b/Music/Attributes/SongFormat.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Music.Attributes; +namespace Music.Attributes; [AttributeUsage(AttributeTargets.Field)] public class SongFormat(string format) : Attribute diff --git a/Music/Music.cs b/Music/Music.cs index 835c4ec..13eacd6 100644 --- a/Music/Music.cs +++ b/Music/Music.cs @@ -34,7 +34,7 @@ public override void Initialize() private async ValueTask ChangeMusicSource(CommandArgs args) { - if(args.Parameters.Count > 0) + if (args.Parameters.Count > 0) { if (args.Parameters[0] == "QQ" || args.Parameters[0] == "网易") { @@ -74,7 +74,7 @@ private async ValueTask DownloadPlay(CommandArgs args) } } else - { + { await args.EventArgs.Reply("请输入一个正确的歌单ID!"); } } @@ -91,9 +91,9 @@ private async ValueTask QrcodeLogin(CommandArgs args) { var (qrsig, buffer) = await Login.GetLoginQrcode(); await args.EventArgs.Reply(MessageBuilder.Group(args.EventArgs.Chain.GroupUin!.Value).Image(buffer).Text("请尽快扫描此二维码60秒后失效")); - await Login.CheckLoginQrcode(qrsig, 120, async (state, token) => - { - if(state == QQ.Enums.QrcodeLoginType.DONE) + await Login.CheckLoginQrcode(qrsig, 120, async (state, token) => + { + if (state == QQ.Enums.QrcodeLoginType.DONE) { Config.Instance.SetToken(token); Config.Instance.SaveTo(); @@ -114,11 +114,11 @@ await Login.CheckLoginQrcode(qrsig, 120, async (state, token) => } }); } - catch(Exception ex) + catch (Exception ex) { await args.EventArgs.Reply(ex.Message); } - + } #region 点歌 @@ -206,8 +206,8 @@ private async ValueTask ChageMusic(CommandArgs args) try { var music = await MusicTool.GetMusicQQ(musicName, id); - var json = MusicSigner.Sign(new("qq", music.PageUrl, music.PlayUrl, music.Album.Picture, music.Name, string.Join(",", music.Singer.Select(i=>i.Name)))); - + var json = MusicSigner.Sign(new("qq", music.PageUrl, music.PlayUrl, music.Album.Picture, music.Name, string.Join(",", music.Singer.Select(i => i.Name)))); + if (json != null) await args.EventArgs.Reply(MessageBuilder.Group(args.EventArgs.Chain.GroupUin!.Value).LightApp(json)); } diff --git a/Music/QQ/Enums/QrcodeLoginType.cs b/Music/QQ/Enums/QrcodeLoginType.cs index c32e961..e2dc950 100644 --- a/Music/QQ/Enums/QrcodeLoginType.cs +++ b/Music/QQ/Enums/QrcodeLoginType.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Music.QQ.Enums; +namespace Music.QQ.Enums; public enum QrcodeLoginType { diff --git a/Music/QQ/Internal/MusicToken/TokenInfo.cs b/Music/QQ/Internal/MusicToken/TokenInfo.cs index f557061..7eb3d24 100644 --- a/Music/QQ/Internal/MusicToken/TokenInfo.cs +++ b/Music/QQ/Internal/MusicToken/TokenInfo.cs @@ -1,5 +1,4 @@ -using Microsoft.VisualBasic; -using Newtonsoft.Json; +using Newtonsoft.Json; namespace Music.QQ.Internal.MusicToken; diff --git a/Music/QQ/Internal/Qimei.cs b/Music/QQ/Internal/Qimei.cs index 89bebc5..c340195 100644 --- a/Music/QQ/Internal/Qimei.cs +++ b/Music/QQ/Internal/Qimei.cs @@ -1,12 +1,8 @@ -using System; -using System.Text; -using System.Linq; -using System.Security.Cryptography; -using System.Net.Http; +using Music.QQ.Internal; using Newtonsoft.Json; using Newtonsoft.Json.Linq; -using System.Collections.Generic; -using Music.QQ.Internal; +using System.Security.Cryptography; +using System.Text; public class QimeiService { diff --git a/Music/QQ/Internal/QuerSong/QuerySongData.cs b/Music/QQ/Internal/QuerSong/QuerySongData.cs index 2870995..a81bf2b 100644 --- a/Music/QQ/Internal/QuerSong/QuerySongData.cs +++ b/Music/QQ/Internal/QuerSong/QuerySongData.cs @@ -1,10 +1,5 @@ using Music.QQ.Internal.Search.Song; using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Music.QQ.Internal.QuerSong; diff --git a/Music/QQ/Internal/Req.cs b/Music/QQ/Internal/Req.cs index 76f8fe1..3b7d08e 100644 --- a/Music/QQ/Internal/Req.cs +++ b/Music/QQ/Internal/Req.cs @@ -1,10 +1,5 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Music.QQ.Internal; diff --git a/Music/QQ/Internal/Response.cs b/Music/QQ/Internal/Response.cs index bd09d31..38d7132 100644 --- a/Music/QQ/Internal/Response.cs +++ b/Music/QQ/Internal/Response.cs @@ -1,5 +1,4 @@ using Newtonsoft.Json; -using Newtonsoft.Json.Linq; namespace Music.QQ.Internal; diff --git a/Music/QQ/Internal/Search/Body.cs b/Music/QQ/Internal/Search/Body.cs index 858a526..51e9d48 100644 --- a/Music/QQ/Internal/Search/Body.cs +++ b/Music/QQ/Internal/Search/Body.cs @@ -1,10 +1,4 @@ using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; namespace Music.QQ.Internal.Search; diff --git a/Music/QQ/Internal/Search/ReqData.cs b/Music/QQ/Internal/Search/ReqData.cs index addcf01..f820395 100644 --- a/Music/QQ/Internal/Search/ReqData.cs +++ b/Music/QQ/Internal/Search/ReqData.cs @@ -1,10 +1,5 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Music.QQ.Internal.Search; diff --git a/Music/QQ/Internal/Search/Song/Hotness.cs b/Music/QQ/Internal/Search/Song/Hotness.cs index 28f7ea1..c054e8f 100644 --- a/Music/QQ/Internal/Search/Song/Hotness.cs +++ b/Music/QQ/Internal/Search/Song/Hotness.cs @@ -1,9 +1,4 @@ using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Music.QQ.Internal.Search.Song; diff --git a/Music/QQ/Internal/Search/Song/KSong.cs b/Music/QQ/Internal/Search/Song/KSong.cs index 4a4bdf2..b4322c5 100644 --- a/Music/QQ/Internal/Search/Song/KSong.cs +++ b/Music/QQ/Internal/Search/Song/KSong.cs @@ -1,9 +1,4 @@ using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Music.QQ.Internal.Search.Song; diff --git a/Music/QQ/Internal/Search/Song/MV.cs b/Music/QQ/Internal/Search/Song/MV.cs index e8a59a9..2c9d18a 100644 --- a/Music/QQ/Internal/Search/Song/MV.cs +++ b/Music/QQ/Internal/Search/Song/MV.cs @@ -1,9 +1,4 @@ using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Music.QQ.Internal.Search.Song; diff --git a/Music/QQ/Internal/Search/Song/Pay.cs b/Music/QQ/Internal/Search/Song/Pay.cs index 4e654ec..c39fd47 100644 --- a/Music/QQ/Internal/Search/Song/Pay.cs +++ b/Music/QQ/Internal/Search/Song/Pay.cs @@ -1,9 +1,4 @@ using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Music.QQ.Internal.Search.Song; diff --git a/Music/QQ/Internal/Search/Song/SingerItem.cs b/Music/QQ/Internal/Search/Song/SingerItem.cs index 2a9576f..d5f3363 100644 --- a/Music/QQ/Internal/Search/Song/SingerItem.cs +++ b/Music/QQ/Internal/Search/Song/SingerItem.cs @@ -1,9 +1,4 @@ using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Music.QQ.Internal.Search.Song; diff --git a/Music/QQ/Internal/Search/Song/SongData.cs b/Music/QQ/Internal/Search/Song/SongData.cs index a2191fb..bbfc4af 100644 --- a/Music/QQ/Internal/Search/Song/SongData.cs +++ b/Music/QQ/Internal/Search/Song/SongData.cs @@ -1,9 +1,4 @@ using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Music.QQ.Internal.Search.Song; diff --git a/Music/QQ/Internal/Search/Song/SongFile.cs b/Music/QQ/Internal/Search/Song/SongFile.cs index 4893c91..90e469d 100644 --- a/Music/QQ/Internal/Search/Song/SongFile.cs +++ b/Music/QQ/Internal/Search/Song/SongFile.cs @@ -1,9 +1,4 @@ using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Music.QQ.Internal.Search.Song; diff --git a/Music/QQ/Internal/Search/Song/Volume.cs b/Music/QQ/Internal/Search/Song/Volume.cs index 664c5f0..285e008 100644 --- a/Music/QQ/Internal/Search/Song/Volume.cs +++ b/Music/QQ/Internal/Search/Song/Volume.cs @@ -1,9 +1,4 @@ using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Music.QQ.Internal.Search.Song; diff --git a/Music/QQ/Internal/Search/Songs.cs b/Music/QQ/Internal/Search/Songs.cs index 0c38407..8f52dea 100644 --- a/Music/QQ/Internal/Search/Songs.cs +++ b/Music/QQ/Internal/Search/Songs.cs @@ -1,10 +1,5 @@ using Music.QQ.Internal.Search.Song; using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Music.QQ.Internal.Search; diff --git a/Music/QQ/Internal/User/HomePageData.cs b/Music/QQ/Internal/User/HomePageData.cs index af305b7..d4deeb5 100644 --- a/Music/QQ/Internal/User/HomePageData.cs +++ b/Music/QQ/Internal/User/HomePageData.cs @@ -1,8 +1,8 @@ #pragma warning disable CS8618 // Naming Styles namespace Music.QQ.Internal.User; -using System; using Newtonsoft.Json; +using System; public partial class HomePageData { [JsonProperty("Info")] diff --git a/Music/QQ/Login.cs b/Music/QQ/Login.cs index 4781ee9..9663249 100644 --- a/Music/QQ/Login.cs +++ b/Music/QQ/Login.cs @@ -26,7 +26,7 @@ public partial class Login UseCookies = true }; - private static readonly HttpClient client = new (httpClientHandler) + private static readonly HttpClient client = new(httpClientHandler) { DefaultRequestHeaders = { { "Referer", "https://xui.ptlogin2.qq.com/" } } }; @@ -49,7 +49,7 @@ public partial class Login { "daid", "383" }, { "pt_3rd_aid", "100497308" } }; - + var uri = new Uri(Utils.QueryUri(QrLoginApi, paramsDict)); var res = await client.GetAsync(uri); var bytes = await res.Content.ReadAsByteArrayAsync(); @@ -59,7 +59,7 @@ public partial class Login public static async Task GetQQMusicToken(string code, int gtk) - { + { // 创建匿名类表示JSON对象 var request = new { @@ -76,7 +76,7 @@ public static async Task GetQQMusicToken(string code, int gtk) method = "QQLogin", param = new { - code + code } } }; @@ -151,7 +151,7 @@ public static async Task CheckLoginQrcode(string qrsig, int timeOut, Action CheckLoginQrcode(string qrsig, Action }; var url = Utils.QueryUri(CheckQrLoginApi, parameters); var res = await client.GetAsync(url); - if(!res.IsSuccessStatusCode) throw new HttpRequestException("CheckLoginQrcode failed!"); + if (!res.IsSuccessStatusCode) throw new HttpRequestException("CheckLoginQrcode failed!"); var results = await res.Content.ReadAsStringAsync(); var match = RegexHelper.RegexState().Match(results); - if(!match.Success) throw new Exception("CheckLoginQrcode failed!"); + if (!match.Success) throw new Exception("CheckLoginQrcode failed!"); var val = match.Groups[1].Value; var data = val.Split(",").Select(s => s.Trim('\'')).ToArray(); var cookieuri = new Uri(QQMusicApi); - var state = data[0] switch - { + var state = data[0] switch + { "0" => QrcodeLoginType.DONE, "65" => QrcodeLoginType.TIMEOUT, "66" => QrcodeLoginType.SCAN, @@ -213,12 +213,12 @@ private static async Task CheckLoginQrcode(string qrsig, Action _ => QrcodeLoginType.OTHER }; if (state == QrcodeLoginType.DONE) - { + { var sigx = RegexHelper.RegexSigx().Match(data[2]).Groups[1].Value; var uin = RegexHelper.RegexUin().Match(data[2]).Groups[1].Value; var pskey = await CheckSig(uin, sigx); var code = await Authorize(pskey); - var token = await GetQQMusicToken(code, Utils.Hash33(pskey)); + var token = await GetQQMusicToken(code, Utils.Hash33(pskey)); token.Cookie = cookie.GetCookies(cookieuri).ToDictionary(c => c.Name, c => c.Value); token.P_Skey = pskey; action(state, token); diff --git a/Music/QQ/Music_QQ.cs b/Music/QQ/Music_QQ.cs index e998f09..e8db2c7 100644 --- a/Music/QQ/Music_QQ.cs +++ b/Music/QQ/Music_QQ.cs @@ -1,14 +1,14 @@ -using System.Net; -using Newtonsoft.Json.Linq; +using Lagrange.XocMat.Extensions; +using Music.QQ.Enums; using Music.QQ.Internal; using Music.QQ.Internal.MusicToken; using Music.QQ.Internal.Playlists; using Music.QQ.Internal.QuerSong; using Music.QQ.Internal.Search; -using Music.QQ.Internal.User; using Music.QQ.Internal.Search.Song; -using Music.QQ.Enums; -using Lagrange.XocMat.Extensions; +using Music.QQ.Internal.User; +using Newtonsoft.Json.Linq; +using System.Net; namespace Music.QQ; @@ -95,7 +95,7 @@ public async Task CheckExpired() public async Task Send(object req) { - if(Token.ExpiredAt < DateTimeOffset.UtcNow.ToUnixTimeSeconds()) + if (Token.ExpiredAt < DateTimeOffset.UtcNow.ToUnixTimeSeconds()) { var (token, success) = await RefreshToken(); if (success) @@ -163,7 +163,7 @@ public async Task DownloadPlaylists(long id) foreach (var item in SpitMid) { var url = await GetSongData(SongFileType.MP3_128, [.. item]); - songurls = songurls.Concat(url.ToDictionary(i => i.Key, i => i.Value.PlayUrl)).ToDictionary(k => k.Key, v => v.Value); + songurls = songurls.Concat(url.ToDictionary(i => i.Mid, i => i.PlayUrl)).ToDictionary(k => k.Key, v => v.Value); } SemaphoreSlim SemaphoreSlim = new(7); var tasks = list.Select(async i => @@ -259,7 +259,7 @@ public async Task> QuerySongData(params string[] mid) return res.Tracks; } - public async Task> GetSongData(SongFileType type, params string[] mid) + public async Task> GetSongData(SongFileType type, params string[] mid) { var domain = "https://ws.stream.qqmusic.qq.com/"; var songs = await QuerySongData(mid); @@ -270,15 +270,15 @@ public async Task> GetSongData(SongFileType type, p method = "UrlGetVkey", param = new { - filename = mid.Select(s => $"{type.GetSongFormat()}{type}{type.GetSongExtension()}"), - songmid = mid, + filename = mid.Select(s => $"{type.GetSongFormat()}{type}{type.GetSongExtension()}").Concat(songs.Select(s => $"RS02{s.Vs[0]}.mp3")), + songmid = mid.Concat(songs.Select(s => s.Mid)), guid = Guid.NewGuid().ToString("N"), - songtype = mid.Select(_ => 0), + songtype = mid.Select(_ => 0).Concat(songs.Select(s => 0)), } }; var res = await Send(req); var array = res.Req.Data?["midurlinfo"]?.Value(); - var trysong = new List(); + if (array is not null) { foreach (var item in array) @@ -287,22 +287,13 @@ public async Task> GetSongData(SongFileType type, p var url = item["wifiurl"]!.Value()!; if (string.IsNullOrEmpty(url)) { - trysong.Add(songmid); continue; } - songDic[songmid].PlayUrl = domain + url; - } - } - if (trysong.Count > 0) - { - var trysongs = songDic.Where(i => trysong.Contains(i.Key)); - var tryUrls = await GetTrySongUrl(trysongs.ToDictionary(i => i.Key, i => i.Value.Vs[0])); - foreach (var (key, value) in tryUrls) - { - songDic[key].PlayUrl = value; + if (string.IsNullOrEmpty(songDic[songmid].PlayUrl)) + songDic[songmid].PlayUrl = domain + url; } } - return songDic; + return [.. songDic.Values]; } public async Task GetSong(string musicName, int index) @@ -310,6 +301,6 @@ public async Task GetSong(string musicName, int index) var data = await SearchSong(musicName); if (index < 1 || index > data.Count) index = 1; - return (await GetSongData(SongFileType.MP3_128, data[index - 1].Mid)).First().Value; + return (await GetSongData(SongFileType.MP3_128, data[index - 1].Mid)).First(); } } \ No newline at end of file diff --git a/Music/QQ/Utils.cs b/Music/QQ/Utils.cs index 2121828..e70561c 100644 --- a/Music/QQ/Utils.cs +++ b/Music/QQ/Utils.cs @@ -11,7 +11,7 @@ public static byte[] GenerateCompressed(Dictionary data) using var zip = new ZipArchive(ms, ZipArchiveMode.Create); foreach (var (filename, buffer) in data) { - if(buffer is null || buffer.Length == 0) + if (buffer is null || buffer.Length == 0) continue; var entry = zip.CreateEntry(filename, CompressionLevel.Fastest); using var stream = entry.Open(); diff --git a/Music/WangYi/Music_163.cs b/Music/WangYi/Music_163.cs index 90bc687..8c05686 100644 --- a/Music/WangYi/Music_163.cs +++ b/Music/WangYi/Music_163.cs @@ -138,7 +138,7 @@ public static async Task> GetMusicUrlByid(IEnumerable()!; var url = songs[i]!["url"]!.GetValue()!; res.Add(id, url); diff --git a/Music/WangYi/PlaylistsData.cs b/Music/WangYi/PlaylistsData.cs index 4c811f1..f0a5753 100644 --- a/Music/WangYi/PlaylistsData.cs +++ b/Music/WangYi/PlaylistsData.cs @@ -3,8 +3,8 @@ namespace Music.WangYi; -using System; using Newtonsoft.Json; +using System; public partial class PlaylistsData { diff --git a/OnlineReward/Config.cs b/OnlineReward/Config.cs index 913e2b8..ba9d3d8 100644 --- a/OnlineReward/Config.cs +++ b/OnlineReward/Config.cs @@ -1,6 +1,6 @@ -using Newtonsoft.Json; -using Lagrange.XocMat.Attributes; +using Lagrange.XocMat.Attributes; using Lagrange.XocMat.Configuration; +using Newtonsoft.Json; namespace OnlineReward;