99using NLyric . Lyrics ;
1010
1111namespace NLyric . Ncm {
12- public static class CloudMusic {
13- private static readonly CloudMusicApi _api = new CloudMusicApi ( ) ;
12+ public sealed class CloudMusic {
13+ private readonly CloudMusicApi _api = new CloudMusicApi ( ) ;
1414
15- public static CloudMusicApi Api => _api ;
15+ public CloudMusicApi Api => _api ;
1616
17- public static async Task < bool > LoginAsync ( string account , string password ) {
18- var queries = new Dictionary < string , string > ( ) ;
17+ public async Task < bool > LoginAsync ( string account , string password ) {
18+ var queries = new Dictionary < string , object > ( ) ;
1919 bool isPhone = Regex . Match ( account , "^[0-9]+$" ) . Success ;
2020 queries [ isPhone ? "phone" : "email" ] = account ;
2121 queries [ "password" ] = password ;
2222 var ( result , _) = await _api . RequestAsync ( isPhone ? CloudMusicApiProviders . LoginCellphone : CloudMusicApiProviders . Login , queries ) ;
2323 return result ;
2424 }
2525
26- public static async Task < NcmTrack [ ] > SearchTrackAsync ( Track track , int limit , bool withArtists ) {
26+ public async Task < NcmTrack [ ] > SearchTrackAsync ( Track track , int limit , bool withArtists ) {
2727 var keywords = new List < string > ( ) ;
2828 if ( track . Name . Length != 0 )
2929 keywords . Add ( track . Name ) ;
@@ -33,10 +33,10 @@ public static async Task<NcmTrack[]> SearchTrackAsync(Track track, int limit, bo
3333 throw new ArgumentException ( "歌曲信息无效" ) ;
3434 for ( int i = 0 ; i < keywords . Count ; i ++ )
3535 keywords [ i ] = keywords [ i ] . WholeWordReplace ( ) ;
36- var ( isOk , json ) = await _api . RequestAsync ( CloudMusicApiProviders . Search , new Dictionary < string , string > {
36+ var ( isOk , json ) = await _api . RequestAsync ( CloudMusicApiProviders . Search , new Dictionary < string , object > {
3737 { "keywords" , string . Join ( " " , keywords ) } ,
38- { "type" , "1" } ,
39- { "limit" , limit . ToString ( ) }
38+ { "type" , 1 } ,
39+ { "limit" , limit }
4040 } ) ;
4141 if ( ! isOk )
4242 throw new ApplicationException ( nameof ( CloudMusicApiProviders . Search ) + " API错误" ) ;
@@ -45,14 +45,14 @@ public static async Task<NcmTrack[]> SearchTrackAsync(Track track, int limit, bo
4545 return ParseSearchTracks ( json ) ;
4646 }
4747
48- public static NcmTrack [ ] ParseSearchTracks ( JObject json ) {
48+ public NcmTrack [ ] ParseSearchTracks ( JObject json ) {
4949 json = ( JObject ) json [ "result" ] ;
5050 if ( ! ( json [ "songs" ] is JArray songs ) )
5151 return Array . Empty < NcmTrack > ( ) ;
5252 return songs . Select ( t => ParseTrack ( t , false ) ) . ToArray ( ) ;
5353 }
5454
55- public static async Task < NcmAlbum [ ] > SearchAlbumAsync ( Album album , int limit , bool withArtists ) {
55+ public async Task < NcmAlbum [ ] > SearchAlbumAsync ( Album album , int limit , bool withArtists ) {
5656 var keywords = new List < string > ( ) ;
5757 if ( album . Name . Length != 0 )
5858 keywords . Add ( album . Name ) ;
@@ -62,10 +62,10 @@ public static async Task<NcmAlbum[]> SearchAlbumAsync(Album album, int limit, bo
6262 throw new ArgumentException ( "专辑信息无效" ) ;
6363 for ( int i = 0 ; i < keywords . Count ; i ++ )
6464 keywords [ i ] = keywords [ i ] . WholeWordReplace ( ) ;
65- var ( isOk , json ) = await _api . RequestAsync ( CloudMusicApiProviders . Search , new Dictionary < string , string > {
65+ var ( isOk , json ) = await _api . RequestAsync ( CloudMusicApiProviders . Search , new Dictionary < string , object > {
6666 { "keywords" , string . Join ( " " , keywords ) } ,
67- { "type" , "10" } ,
68- { "limit" , limit . ToString ( ) }
67+ { "type" , 10 } ,
68+ { "limit" , limit }
6969 } ) ;
7070 if ( ! isOk )
7171 throw new ApplicationException ( nameof ( CloudMusicApiProviders . Search ) + " API错误" ) ;
@@ -74,37 +74,37 @@ public static async Task<NcmAlbum[]> SearchAlbumAsync(Album album, int limit, bo
7474 return ParseSearchAlbums ( json ) ;
7575 }
7676
77- public static NcmAlbum [ ] ParseSearchAlbums ( JObject json ) {
77+ public NcmAlbum [ ] ParseSearchAlbums ( JObject json ) {
7878 json = ( JObject ) json [ "result" ] ;
7979 if ( ! ( json [ "albums" ] is JArray albums ) )
8080 return Array . Empty < NcmAlbum > ( ) ;
8181 // albumCount不可信,搜索"U-87 陈奕迅"返回albums有内容,但是albumCount为0
8282 return albums . Select ( t => ParseAlbum ( t ) ) . ToArray ( ) ;
8383 }
8484
85- public static async Task < NcmTrack [ ] > GetTracksAsync ( int albumId ) {
86- var ( isOk , json ) = await _api . RequestAsync ( CloudMusicApiProviders . Album , new Dictionary < string , string > {
87- { "id" , albumId . ToString ( ) }
85+ public async Task < NcmTrack [ ] > GetTracksAsync ( int albumId ) {
86+ var ( isOk , json ) = await _api . RequestAsync ( CloudMusicApiProviders . Album , new Dictionary < string , object > {
87+ { "id" , albumId }
8888 } ) ;
8989 if ( ! isOk )
9090 throw new ApplicationException ( nameof ( CloudMusicApiProviders . Album ) + " API错误" ) ;
9191 return ParseTracks ( json ) ;
9292 }
9393
94- public static NcmTrack [ ] ParseTracks ( JObject json ) {
94+ public NcmTrack [ ] ParseTracks ( JObject json ) {
9595 return json [ "songs" ] . Select ( t => ParseTrack ( t , true ) ) . ToArray ( ) ;
9696 }
9797
98- public static async Task < NcmLyric > GetLyricAsync ( int trackId ) {
99- var ( isOk , json ) = await _api . RequestAsync ( CloudMusicApiProviders . Lyric , new Dictionary < string , string > {
100- { "id" , trackId . ToString ( ) }
98+ public async Task < NcmLyric > GetLyricAsync ( int trackId ) {
99+ var ( isOk , json ) = await _api . RequestAsync ( CloudMusicApiProviders . Lyric , new Dictionary < string , object > {
100+ { "id" , trackId }
101101 } ) ;
102102 if ( ! isOk )
103103 throw new ApplicationException ( nameof ( CloudMusicApiProviders . Lyric ) + " API错误" ) ;
104104 return ParseLyric ( trackId , json ) ;
105105 }
106106
107- public static NcmLyric ParseLyric ( int trackId , JObject json ) {
107+ public NcmLyric ParseLyric ( int trackId , JObject json ) {
108108 if ( json is null )
109109 throw new ArgumentNullException ( nameof ( json ) ) ;
110110
@@ -119,26 +119,26 @@ public static NcmLyric ParseLyric(int trackId, JObject json) {
119119 return new NcmLyric ( trackId , true , false , rawLrc , rawVersion , translatedLrc , translatedVersion ) ;
120120 }
121121
122- private static NcmAlbum ParseAlbum ( JToken json ) {
122+ private NcmAlbum ParseAlbum ( JToken json ) {
123123 var album = new Album ( ( string ) json [ "name" ] , ParseNames ( json [ "artists" ] ) ) ;
124124 var ncmAlbum = new NcmAlbum ( album , ( int ) json [ "id" ] ) ;
125125 return ncmAlbum ;
126126 }
127127
128- private static NcmTrack ParseTrack ( JToken json , bool isShortName ) {
128+ private NcmTrack ParseTrack ( JToken json , bool isShortName ) {
129129 var track = new Track ( ( string ) json [ "name" ] , ParseNames ( json [ isShortName ? "ar" : "artists" ] ) ) ;
130130 var ncmTrack = new NcmTrack ( track , ( int ) json [ "id" ] ) ;
131131 return ncmTrack ;
132132 }
133133
134- private static string [ ] ParseNames ( JToken json ) {
134+ private string [ ] ParseNames ( JToken json ) {
135135 return json . Select ( t => ( string ) t [ "name" ] ) . ToArray ( ) ;
136136 }
137137
138- private static ( Lrc , int ) ParseLyric ( JToken json ) {
138+ private ( Lrc , int ) ParseLyric ( JToken json ) {
139139 string lyric = ( string ) json [ "lyric" ] ;
140140 var lrc = string . IsNullOrEmpty ( lyric ) ? null : Lrc . UnsafeParse ( lyric ) ;
141- var version = ( int ) json [ "version" ] ;
141+ int version = ( int ) json [ "version" ] ;
142142 return ( lrc , version ) ;
143143 }
144144 }
0 commit comments