7
7
import com .github .topi314 .lavasearch .result .AudioText ;
8
8
import com .github .topi314 .lavasearch .result .BasicAudioSearchResult ;
9
9
import com .github .topi314 .lavasearch .result .BasicAudioText ;
10
- import com .github .topi314 .lavasrc .extended .ExtendedAudioPlaylist ;
11
10
import com .github .topi314 .lavasrc .LavaSrcTools ;
12
- import com .github .topi314 .lavasrc .mirror .DefaultMirrorResolver ;
11
+ import com .github .topi314 .lavasrc .extended .ExtendedAudioPlaylist ;
12
+ import com .github .topi314 .lavasrc .extended .ExtendedAudioSourceManager ;
13
+ import com .github .topi314 .lavasrc .extended .ExtendedAudioTrackInfo ;
14
+ import com .github .topi314 .lavasrc .http .BaseHttpConfigurable ;
15
+ import com .github .topi314 .lavasrc .mirror .MirrorResolver ;
13
16
import com .github .topi314 .lavasrc .mirror .MirroringAudioSourceManager ;
14
- import com .github .topi314 .lavasrc .mirror .MirroringAudioTrackResolver ;
15
17
import com .sedmelluq .discord .lavaplayer .player .AudioPlayerManager ;
16
18
import com .sedmelluq .discord .lavaplayer .tools .JsonBrowser ;
19
+ import com .sedmelluq .discord .lavaplayer .tools .io .HttpClientTools ;
20
+ import com .sedmelluq .discord .lavaplayer .tools .io .HttpInterfaceManager ;
17
21
import com .sedmelluq .discord .lavaplayer .track .*;
18
22
import org .apache .http .client .methods .HttpGet ;
19
23
import org .apache .http .client .utils .URIBuilder ;
34
38
import java .time .Duration ;
35
39
import java .time .Instant ;
36
40
import java .util .*;
37
- import java .util .function .Function ;
38
41
import java .util .function .Predicate ;
39
42
import java .util .regex .Pattern ;
40
43
import java .util .stream .Collectors ;
41
44
42
- public class AppleMusicSourceManager extends MirroringAudioSourceManager implements AudioSearchManager {
45
+ public class AppleMusicSourceManager implements MirroringAudioSourceManager , ExtendedAudioSourceManager , AudioSearchManager , BaseHttpConfigurable {
43
46
44
47
public static final Pattern URL_PATTERN = Pattern .compile ("(https?://)?(www\\ .)?music\\ .apple\\ .com/((?<countrycode>[a-zA-Z]{2})/)?(?<type>album|playlist|artist|song)(/[a-zA-Z\\ p{L}\\ d\\ -%]+)?/(?<identifier>[a-zA-Z\\ d\\ -.]+)(\\ ?i=(?<identifier2>\\ d+))?" );
45
48
public static final String SEARCH_PREFIX = "amsearch:" ;
46
49
public static final String PREVIEW_PREFIX = "amprev:" ;
47
50
public static final long PREVIEW_LENGTH = 30000 ;
48
51
public static final int MAX_PAGE_ITEMS = 300 ;
49
- public static final String API_BASE = "https://api.music.apple.com/v1/" ;
52
+ private static final String API_BASE = "https://api.music.apple.com/v1/" ;
50
53
public static final Set <AudioSearchResult .Type > SEARCH_TYPES = Set .of (AudioSearchResult .Type .TRACK , AudioSearchResult .Type .ALBUM , AudioSearchResult .Type .PLAYLIST , AudioSearchResult .Type .ARTIST , AudioSearchResult .Type .TEXT );
51
54
public static final Set <AudioSearchResult .Type > TOP_RESULT_SEARCH_TYPES = Set .of (AudioSearchResult .Type .TRACK , AudioSearchResult .Type .ALBUM , AudioSearchResult .Type .PLAYLIST , AudioSearchResult .Type .ARTIST );
52
55
53
- private final String countryCode ;
56
+ private final HttpInterfaceManager httpInterfaceManager = HttpClientTools .createDefaultThreadLocalManager ();
57
+ private final AppleMusicTokenManager tokenManager ;
58
+ private final MirrorResolver mirrorResolver ;
59
+ private final String countryCode = "US" ;
54
60
private int playlistPageLimit ;
55
61
private int albumPageLimit ;
56
- private final AppleMusicTokenManager tokenManager ;
57
-
58
- public AppleMusicSourceManager (String [] providers , String mediaAPIToken , String countryCode , Function <Void , AudioPlayerManager > audioPlayerManager ) {
59
- this (mediaAPIToken , countryCode , audioPlayerManager , new DefaultMirrorResolver (providers ));
60
- }
61
-
62
- public AppleMusicSourceManager (String mediaAPIToken , String countryCode , AudioPlayerManager audioPlayerManager , MirroringAudioTrackResolver mirroringAudioTrackResolver ) {
63
- this (mediaAPIToken , countryCode , unused -> audioPlayerManager , mirroringAudioTrackResolver );
64
- }
65
-
66
- public AppleMusicSourceManager (String mediaAPIToken , String countryCode , Function <Void , AudioPlayerManager > audioPlayerManager , MirroringAudioTrackResolver mirroringAudioTrackResolver ) {
67
- super (audioPlayerManager , mirroringAudioTrackResolver );
68
- this .countryCode = (countryCode == null || countryCode .isEmpty ()) ? "US" : countryCode ;
69
62
63
+ public AppleMusicSourceManager (String mediaAPIToken , MirrorResolver mirrorResolver ) {
70
64
try {
71
65
this .tokenManager = new AppleMusicTokenManager (mediaAPIToken );
72
66
} catch (IOException e ) {
73
67
throw new RuntimeException ("Failed to initialize token manager" , e );
74
68
}
69
+ this .mirrorResolver = mirrorResolver ;
70
+ }
71
+
72
+ @ NotNull
73
+ @ Override
74
+ public String getSourceName () {
75
+ return "applemusic" ;
76
+ }
77
+
78
+ @ NotNull
79
+ @ Override
80
+ public MirrorResolver getMirrorResolver () {
81
+ return this .mirrorResolver ;
82
+ }
83
+
84
+ @ NotNull
85
+ @ Override
86
+ public HttpInterfaceManager getHttpInterfaceManager () {
87
+ return this .httpInterfaceManager ;
88
+ }
89
+
90
+ @ Override
91
+ public AudioTrack decodeTrack (AudioTrackInfo trackInfo , DataInput input ) throws IOException {
92
+ return new AppleMusicAudioTrack (trackInfo ,
93
+ decodeTrack (input ),
94
+ this
95
+ );
75
96
}
76
97
77
98
public void setPlaylistPageLimit (int playlistPageLimit ) {
@@ -90,25 +111,6 @@ public void setMediaAPIToken(String mediaAPIToken) {
90
111
}
91
112
}
92
113
93
- @ NotNull
94
- @ Override
95
- public String getSourceName () {
96
- return "applemusic" ;
97
- }
98
-
99
- @ Override
100
- public AudioTrack decodeTrack (AudioTrackInfo trackInfo , DataInput input ) throws IOException {
101
- var extendedAudioTrackInfo = super .decodeTrack (input );
102
- return new AppleMusicAudioTrack (trackInfo ,
103
- extendedAudioTrackInfo .albumName ,
104
- extendedAudioTrackInfo .albumUrl ,
105
- extendedAudioTrackInfo .artistUrl ,
106
- extendedAudioTrackInfo .artistArtworkUrl ,
107
- extendedAudioTrackInfo .previewUrl ,
108
- extendedAudioTrackInfo .isPreview ,
109
- this
110
- );
111
- }
112
114
113
115
@ Override
114
116
public @ Nullable AudioSearchResult loadSearch (@ NotNull String query , @ NotNull Set <AudioSearchResult .Type > types ) {
@@ -254,7 +256,14 @@ public AudioSearchResult getSearchSuggestions(String query, Set<AudioSearchResul
254
256
artworkUrl ,
255
257
isrc
256
258
);
257
- var track = new AppleMusicAudioTrack (info , albumName , albumUrl , artistUrl , null , previewUrl , false , this );
259
+ var extendedInfo = new ExtendedAudioTrackInfo (
260
+ albumName ,
261
+ albumUrl ,
262
+ artistUrl ,
263
+ artworkUrl ,
264
+ false
265
+ );
266
+ var track = new AppleMusicAudioTrack (info , extendedInfo , this );
258
267
tracks .add (track );
259
268
break ;
260
269
}
@@ -463,7 +472,7 @@ private String parseArtistId(JsonBrowser json) {
463
472
}
464
473
465
474
466
- public static AppleMusicSourceManager fromMusicKitKey (String musicKitKey , String keyId , String teamId , String countryCode , AudioPlayerManager audioPlayerManager , MirroringAudioTrackResolver mirroringAudioTrackResolver ) throws NoSuchAlgorithmException , InvalidKeySpecException {
475
+ public static AppleMusicSourceManager fromMusicKitKey (String musicKitKey , String keyId , String teamId , MirrorResolver mirrorResolver ) throws NoSuchAlgorithmException , InvalidKeySpecException {
467
476
var base64 = musicKitKey .replaceAll ("-----BEGIN PRIVATE KEY-----\n " , "" )
468
477
.replaceAll ("-----END PRIVATE KEY-----" , "" )
469
478
.replaceAll ("\\ s" , "" );
@@ -477,6 +486,6 @@ public static AppleMusicSourceManager fromMusicKitKey(String musicKitKey, String
477
486
.withExpiresAt (Instant .now ().plus (Duration .ofSeconds (15777000 )))
478
487
.withKeyId (keyId )
479
488
.sign (Algorithm .ECDSA256 (key ));
480
- return new AppleMusicSourceManager (jwt , countryCode , audioPlayerManager , mirroringAudioTrackResolver );
489
+ return new AppleMusicSourceManager (jwt , mirrorResolver );
481
490
}
482
491
}
0 commit comments