8000 Too much work · DuncteBot/skybot-source-managers@d1aa852 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Nov 26, 2023. It is now read-only.

Commit d1aa852

Browse files
committed
Too much work
1 parent b210388 commit d1aa852

File tree

5 files changed

+193
-25
lines changed

5 files changed

+193
-25
lines changed

build.gradle.kts

+4-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ dependencies {
4343
api(group = "io.sentry", name = "sentry-logback", version = "1.7.17")
4444
api(group = "com.google.apis", name = "google-api-services-youtube", version = "v3-rev212-1.25.0")
4545
api(group = "me.duncte123", name = "botCommons", version = "1.0.65")
46+
47+
48+
implementation(group = "se.michaelthelin.spotify", name = "spotify-web-api-java", version = "4.2.1")
4649
}
4750

4851
configure<JavaPluginConvention> {
@@ -52,4 +55,4 @@ configure<JavaPluginConvention> {
5255
tasks.withType<Wrapper> {
5356
distributionType = DistributionType.ALL
5457
gradleVersion = "5.6.3"
55-
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2020 Duncan "duncte123" Sterken
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.dunctebot.sourcemanagers;
18+
19+
import com.dunctebot.sourcemanagers.clypit.ClypitAudioSourceManager;
20+
import com.dunctebot.sourcemanagers.extra.YoutubeContextFilterOverride;
21+
import com.dunctebot.sourcemanagers.pornhub.PornHubAudioSourceManager;
22+
import com.dunctebot.sourcemanagers.speech.SpeechAudioSourceManager;
23+
import com.dunctebot.sourcemanagers.spotify.SpotifyAudioSourceManager;
24+
import com.dunctebot.sourcemanagers.youtube.YoutubeAudioSourceManagerOverride;
25+
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
26+
import net.notfab.caching.client.CacheClient;
27+
28+
public class DuncteBotSources {
29+
public static void registerCustom(AudioPlayerManager playerManager, String speechLanguage,
30+
int playlistPageCount, boolean updateYoutubeData,
31+
String youtubeApiKey, CacheClient cacheClient,
32+
String spotifyClientId, String spotifyClientSecret, int playlistLimit) {
33+
34+
final YoutubeAudioSourceManagerOverride youtubeAudioSourceManager = new YoutubeAudioSourceManagerOverride(
35+
cacheClient,
36+
youtubeApiKey
37+
);
38+
39+
youtubeAudioSourceManager.setPlaylistPageCount(playlistPageCount);
40+
youtubeAudioSourceManager.getMainHttpConfiguration().setHttpContextFilter(new YoutubeContextFilterOverride(updateYoutubeData));
41+
42+
playerManager.registerSourceManager(
43+
new SpotifyAudioSourceManager(
44+
youtubeAudioSourceManager,
45+
spotifyClientId,
46+
spotifyClientSecret,
47+
youtubeApiKey,
48+
playlistLimit
49+
)
50+
);
51+
playerManager.registerSourceManager(new ClypitAudioSourceManager());
52+
playerManager.registerSourceManager(new SpeechAudioSourceManager(speechLanguage));
53+
playerManager.registerSourceManager(new PornHubAudioSourceManager());
54+
playerManager.registerSourceManager(youtubeAudioSourceManager);
55+
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2020 Duncan "duncte123" Sterken
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.dunctebot.sourcemanagers.extra;
18+
19+
public class LimitReachedException extends RuntimeException {
20+
private final int size;
21+
22+
public LimitReachedException(String message, int size) {
23+
super(message);
24+
25+
this.size = size;
26+
}
27+
28+
public int getSize() {
29+
return size;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2020 Duncan "duncte123" Sterken
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.dunctebot.sourcemanagers.extra;
18+
19+
import com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeHttpContextFilter;
20+
import io.sentry.Sentry;
21+
import org.apache.http.client.methods.HttpUriRequest;
22+
import org.apache.http.client.protocol.HttpClientContext;
23+
24+
import java.io.Closeable;
25+
import java.io.IOException;
26+
import java.util.concurrent.Executors;
27+
import java.util.concurrent.ScheduledExecutorService;
28+
import java.util.concurrent.TimeUnit;
29+
30+
import static com.dunctebot.sourcemanagers.extra.YoutubeUtils.getYoutubeHeaderDetails;
31+
32+
public class YoutubeContextFilterOverride extends YoutubeHttpContextFilter implements Closeable {
33+
private YoutubeVersionData youtubeVersionData = null;
34+
private final ScheduledExecutorService dataUpdateThread = Executors.newSingleThreadScheduledExecutor((r) -> {
35+
final Thread t = new Thread();
36+
t.setName("YouTube-data-updater");
37+
t.setDaemon(true);
38+
return t;
39+
});
40+
41+
public YoutubeContextFilterOverride() {
42+
this(true);
43+
}
44+
45+
public YoutubeContextFilterOverride(boolean shouldUpdate) {
46+
if (shouldUpdate) {
47+
dataUpdateThread.scheduleAtFixedRate(this::updateYoutubeData, 0L, 1L, TimeUnit.DAYS);
48+
}
49+
}
50+
51+
private void updateYoutubeData() {
52+
try {
53+
this.youtubeVersionData = getYoutubeHeaderDetails();
54+
} catch (IOException e) {
55+
Sentry.capture(e);
56+
}
57+
}
58+
59+
@Override
60+
public void onRequest(HttpClientContext context, HttpUriRequest request, boolean isRepetition) {
61+
super.onRequest(context, request, isRepetition);
62+
63+
if (youtubeVersionData == null) {
64+
return;
65+
}
66+
67+
request.setHeader("x-youtube-client-version", youtubeVersionData.getVersion());
68+
request.setHeader("x-youtube-page-cl", youtubeVersionData.getPageCl());
69+
request.setHeader("x-youtube-page-label", youtubeVersionData.getLabel());
70+
request.setHeader("x-youtube-variants-checksum", youtubeVersionData.getChecksum());
71+
}
72+
73+
@Override
74+
public void close() throws IOException {
75+
dataUpdateThread.shutdown();
76+
}
77+
}

src/main/java/com/dunctebot/sourcemanagers/spotify/SpotifyAudioSourceManager.java

+24-24
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.dunctebot.sourcemanagers.spotify;
1818

19+
import com.dunctebot.sourcemanagers.AudioTrackInfoWithImage;
20+
import com.dunctebot.sourcemanagers.extra.LimitReachedException;
1921
import com.google.api.services.youtube.model.SearchResult;
2022
import com.google.api.services.youtube.model.Video;
2123
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
@@ -29,11 +31,6 @@
2931
import com.wrapper.spotify.model_objects.credentials.ClientCredentials;
3032
import com.wrapper.spotify.model_objects.specification.*;
3133
import com.wrapper.spotify.requests.authorization.client_credentials.ClientCredentialsRequest;
32-
import ml.duncte123.skybot.Author;
33-
import ml.duncte123.skybot.audio.TrackScheduler;
34-
import ml.duncte123.skybot.audio.sourcemanagers.AudioTrackInfoWithImage;
35-
import ml.duncte123.skybot.exceptions.LimitReachedException;
36-
import ml.duncte123.skybot.objects.config.DunctebotConfig;
3734
import org.slf4j.Logger;
3835
import org.slf4j.LoggerFactory;
3936

@@ -51,9 +48,8 @@
5148
import java.util.regex.Matcher;
5249
import java.util.regex.Pattern;
5350

54-
import static ml.duncte123.skybot.utils.YoutubeUtils.*;
51+
import static com.dunctebot.sourcemanagers.extra.YoutubeUtils.*;
5552

56-
@Author(nickname = "duncte123", author = "Duncan Sterken")
5753
public class SpotifyAudioSourceManager implements AudioSourceManager {
5854

5955
private static final Logger logger = LoggerFactory.getLogger(SpotifyAudioSourceManager.class);
@@ -76,25 +72,29 @@ public class SpotifyAudioSourceManager implements AudioSourceManager {
7672
private final SpotifyApi spotifyApi;
7773
private final YoutubeAudioSourceManager youtubeAudioSourceManager;
7874
private final ScheduledExecutorService service;
79-
private final DunctebotConfig.Apis config;
80-
81-
public SpotifyAudioSourceManager(YoutubeAudioSourceManager youtubeAudioSourceManager, DunctebotConfig.Apis config) {
82-
this.config = config;
83-
84-
final String clientId = config.spotify.clientId;
85-
final String clientSecret = config.spotify.clientSecret;
86-
final String youtubeApiKey = config.googl;
87-
88-
if (clientId == null || clientSecret == null || youtubeApiKey == null) {
75+
/*private final String spotifyClientId;
76+
private final String spotifyClientSecret;*/
77+
private final String youtubeApiKey;
78+
private final int playlistLimit;
79+
80+
public SpotifyAudioSourceManager(YoutubeAudioSourceManager youtubeAudioSourceManager,
81+
String spotifyClientId, String spotifyClientSecret, String youtubeApiKey,
82+
int playlistLimit) {
83+
/*this.spotifyClientId = spotifyClientId;
84+
this.spotifyClientSecret = spotifyClientSecret;*/
85+
this.youtubeApiKey = youtubeApiKey;
86+
this.playlistLimit = playlistLimit;
87+
88+
if (spotifyClientId == null || spotifyClientSecret == null || youtubeApiKey == null) {
8989
logger.error("Could not load Spotify keys");
9090
this.spotifyApi = null;
9191
this.service = null;
9292
this.youtubeAudioSourceManager = null;
9393
} else {
9494
this.youtubeAudioSourceManager = youtubeAudioSourceManager;
9595
this.spotifyApi = new SpotifyApi.Builder()
96-
.setClientId(clientId)
97-
.setClientSecret(clientSecret)
96+
.setClientId(spotifyClientId)
97+
.setClientSecret(spotifyClientSecret)
9898
.build();
9999

100100
this.service = Executors.newScheduledThreadPool(2, (r) -> new Thread(r, "Spotify-Token-Update-Thread"));
@@ -177,8 +177,8 @@ private AudioItem getSpotifyPlaylist(AudioReference reference, boolean isPatron)
177177
return null;
178178
}
179179

180-
if (playlistTracks.length > TrackScheduler.QUEUE_SIZE && !isPatron) {
181-
throw new LimitReachedException("The playlist is too big", TrackScheduler.QUEUE_SIZE);
180+
if (playlistTracks.length > this.playlistLimit && !isPatron) {
181+
throw new LimitReachedException("The playlist is too big", this.playlistLimit);
182182
}
183183

184184
for (final PlaylistTrack playlistTrack : playlistTracks) {
@@ -227,7 +227,7 @@ private AudioItem getSpotifyTrack(AudioReference reference) {
227227
return null;
228228
}
229229

230-
final Video v = getVideoById(videoId, this.config.googl);
230+
final Video v = getVideoById(videoId, this.youtubeApiKey);
231231

232232
return audioTrackFromVideo(v, track.getAlbum().getImages());
233233
}
@@ -298,7 +298,7 @@ private Matcher getSpotifyPlaylistFromString(String input) {
298298

299299
private List<AudioTrack> getTrackListFromVideoIds(List<String> videoIds, Image[] images) throws Exception {
300300
final String videoIdsJoined = String.join(",", videoIds);
301-
final List<Video> videosByIds = getVideosByIds(videoIdsJoined, this.config.googl);
301+
final List<Video> videosByIds = getVideosByIds(videoIdsJoined, this.youtubeApiKey);
302302
final List<AudioTrack> playList = new ArrayList<>();
303303

304304
videosByIds.forEach((video) -> playList.add(audioTrackFromVideo(video, images)));
@@ -308,7 +308,7 @@ private List<AudioTrack> getTrackListFromVideoIds(List<String> videoIds, Image[]
308308

309309
@Nullable
310310
private String searchYoutube(String title, String author) throws IOException {
311-
final List<SearchResult> results = searchYoutubeIdOnly(title + " " + author, this.config.googl, 1L);
311+
final List<SearchResult> results = searchYoutubeIdOnly(title + " " + author, this.youtubeApiKey, 1L);
312312

313313
if (!results.isEmpty()) {
314314
return results.get(0).getId().getVideoId();

0 commit comments

Comments
 (0)
0