diff --git a/build.gradle.kts b/build.gradle.kts index c8e1f34..4f63b47 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -28,10 +28,6 @@ version = "1.0-SNAPSHOT" repositories { jcenter() - maven { - url = uri("https://maven.notfab.net/Hosted") - } - maven { url = uri("https://jitpack.io") } @@ -39,17 +35,12 @@ repositories { dependencies { api(group = "com.github.duncte123", name = "lavaplayer", version = "1dff250") - api(group = "net.notfab.cache", name = "cache-client", version = "2.2") api(group = "io.sentry", name = "sentry-logback", version = "1.7.17") - api(group = "com.google.apis", name = "google-api-services-youtube", version = "v3-rev212-1.25.0") api(group = "me.duncte123", name = "botCommons", version = "1.0.65") - - - implementation(group = "se.michaelthelin.spotify", name = "spotify-web-api-java", version = "4.2.1") } configure { - sourceCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = JavaVersion.VERSION_11 } tasks.withType { diff --git a/src/main/java/com/dunctebot/sourcemanagers/DuncteBotSources.java b/src/main/java/com/dunctebot/sourcemanagers/DuncteBotSources.java index 31fd7b4..0a27877 100644 --- a/src/main/java/com/dunctebot/sourcemanagers/DuncteBotSources.java +++ b/src/main/java/com/dunctebot/sourcemanagers/DuncteBotSources.java @@ -20,38 +20,23 @@ import com.dunctebot.sourcemanagers.extra.YoutubeContextFilterOverride; import com.dunctebot.sourcemanagers.pornhub.PornHubAudioSourceManager; import com.dunctebot.sourcemanagers.speech.SpeechAudioSourceManager; -import com.dunctebot.sourcemanagers.spotify.SpotifyAudioSourceManager; -import com.dunctebot.sourcemanagers.youtube.YoutubeAudioSourceManagerOverride; import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager; -import net.notfab.caching.client.CacheClient; +import com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioSourceManager; public class DuncteBotSources { public static void registerCustom(AudioPlayerManager playerManager, String speechLanguage, - int playlistPageCount, boolean updateYoutubeData, - String youtubeApiKey, CacheClient cacheClient, - String spotifyClientId, String spotifyClientSecret, int playlistLimit) { + int playlistPageCount, boolean updateYoutubeData) { - final YoutubeAudioSourceManagerOverride youtubeAudioSourceManager = new YoutubeAudioSourceManagerOverride( - cacheClient, - youtubeApiKey - ); + final YoutubeAudioSourceManager youtubeSource = playerManager.source(YoutubeAudioSourceManager.class); + youtubeSource.setPlaylistPageCount(playlistPageCount); + youtubeSource.getMainHttpConfiguration() + .setHttpContextFilter( + new YoutubeContextFilterOverride(updateYoutubeData, youtubeSource.getHttpInterface()) + ); - youtubeAudioSourceManager.setPlaylistPageCount(playlistPageCount); - youtubeAudioSourceManager.getMainHttpConfiguration().setHttpContextFilter(new YoutubeContextFilterOverride(updateYoutubeData)); - - playerManager.registerSourceManager( - new SpotifyAudioSourceManager( - youtubeAudioSourceManager, - spotifyClientId, - spotifyClientSecret, - youtubeApiKey, - playlistLimit - ) - ); playerManager.registerSourceManager(new ClypitAudioSourceManager()); playerManager.registerSourceManager(new SpeechAudioSourceManager(speechLanguage)); playerManager.registerSourceManager(new PornHubAudioSourceManager()); - playerManager.registerSourceManager(youtubeAudioSourceManager); } } diff --git a/src/main/java/com/dunctebot/sourcemanagers/extra/LimitReachedException.java b/src/main/java/com/dunctebot/sourcemanagers/extra/LimitReachedException.java deleted file mode 100644 index aa3dd9d..0000000 --- a/src/main/java/com/dunctebot/sourcemanagers/extra/LimitReachedException.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2020 Duncan "duncte123" Sterken - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.dunctebot.sourcemanagers.extra; - -public class LimitReachedException extends RuntimeException { - private final int size; - - public LimitReachedException(String message, int size) { - super(message); - - this.size = size; - } - - public int getSize() { - return size; - } -} diff --git a/src/main/java/com/dunctebot/sourcemanagers/extra/YoutubeContextFilterOverride.java b/src/main/java/com/dunctebot/sourcemanagers/extra/YoutubeContextFilterOverride.java index e96ef04..ed5888e 100644 --- a/src/main/java/com/dunctebot/sourcemanagers/extra/YoutubeContextFilterOverride.java +++ b/src/main/java/com/dunctebot/sourcemanagers/extra/YoutubeContextFilterOverride.java @@ -17,20 +17,26 @@ package com.dunctebot.sourcemanagers.extra; import com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeHttpContextFilter; +import com.sedmelluq.discord.lavaplayer.tools.DataFormatTools; +import com.sedmelluq.discord.lavaplayer.tools.JsonBrowser; +import com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface; import io.sentry.Sentry; +import org.apache.commons.io.IOUtils; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.protocol.HttpClientContext; import java.io.Closeable; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -import static com.dunctebot.sourcemanagers.extra.YoutubeUtils.getYoutubeHeaderDetails; - public class YoutubeContextFilterOverride extends YoutubeHttpContextFilter implements Closeable { private YoutubeVersionData youtubeVersionData = null; + private final HttpInterface httpInterface; private final ScheduledExecutorService dataUpdateThread = Executors.newSingleThreadScheduledExecutor((r) -> { final Thread t = new Thread(); t.setName("YouTube-data-updater"); @@ -38,11 +44,13 @@ public class YoutubeContextFilterOverride extends YoutubeHttpContextFilter imple return t; }); - public YoutubeContextFilterOverride() { - this(true); + public YoutubeContextFilterOverride(HttpInterface httpInterface) { + this(true, httpInterface); } - public YoutubeContextFilterOverride(boolean shouldUpdate) { + public YoutubeContextFilterOverride(boolean shouldUpdate, HttpInterface httpInterface) { + this.httpInterface = httpInterface; + if (shouldUpdate) { dataUpdateThread.scheduleAtFixedRate(this::updateYoutubeData, 0L, 1L, TimeUnit.DAYS); } @@ -56,6 +64,22 @@ private void updateYoutubeData() { } } + private YoutubeVersionData getYoutubeHeaderDetails() throws IOException { + final HttpGet httpGet = new HttpGet("https://www.youtube.com/"); + httpGet.setHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"); + + try (final CloseableHttpResponse response = this.httpInterface.execute(httpGet)) { + final String html = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8); + + final String extracted = DataFormatTools.extractBetween(html, + "window.ytplayer = {};ytcfg.set(", + ");ytcfg.set("); + final JsonBrowser json = JsonBrowser.parse(extracted); + + return YoutubeVersionData.fromBrowser(json); + } + } + @Override public void onRequest(HttpClientContext context, HttpUriRequest request, boolean isRepetition) { super.onRequest(context, request, isRepetition); @@ -71,7 +95,7 @@ public void onRequest(HttpClientContext context, HttpUriRequest request, boolean } @Override - public void close() throws IOException { + public void close() { dataUpdateThread.shutdown(); } } diff --git a/src/main/java/com/dunctebot/sourcemanagers/extra/YoutubePlaylistMetadata.java b/src/main/java/com/dunctebot/sourcemanagers/extra/YoutubePlaylistMetadata.java deleted file mode 100644 index a965dee..0000000 --- a/src/main/java/com/dunctebot/sourcemanagers/extra/YoutubePlaylistMetadata.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2020 Duncan "duncte123" Sterken - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.dunctebot.sourcemanagers.extra; - -import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo; -import jdk.internal.jline.internal.Nullable; - -import java.util.List; - -public class YoutubePlaylistMetadata { - private final String id; - private final String title; - private final String nextPageKey; - private final List tracks; - - public YoutubePlaylistMetadata(String id, String title, @Nullable String nextPageKey, List tracks) { - this.id = id; - this.title = title; - this.nextPageKey = nextPageKey; - this.tracks = tracks; - } - - public String getId() { - return id; - } - - public String getTitle() { - return title; - } - - @Nullable - public String getNextPageKey() { - return nextPageKey; - } - - public List getTracks() { - return tracks; - } - - @Override - public String toString() { - return "YoutubePlaylistMetadata{" + - "id='" + id + '\'' + - ", title='" + title + '\'' + - '}'; - } -} diff --git a/src/main/java/com/dunctebot/sourcemanagers/extra/YoutubeUtils.java b/src/main/java/com/dunctebot/sourcemanagers/extra/YoutubeUtils.java deleted file mode 100644 index 06164fb..0000000 --- a/src/main/java/com/dunctebot/sourcemanagers/extra/YoutubeUtils.java +++ /dev/null @@ -1,240 +0,0 @@ -/* - * Copyright 2020 Duncan "duncte123" Sterken - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.dunctebot.sourcemanagers.extra; - -import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; -import com.google.api.client.json.jackson2.JacksonFactory; -import com.google.api.services.youtube.YouTube; -import com.google.api.services.youtube.model.*; -import com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioSourceManager; -import com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioTrack; -import com.sedmelluq.discord.lavaplayer.tools.DataFormatTools; -import com.sedmelluq.discord.lavaplayer.tools.JsonBrowser; -import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo; -import me.duncte123.botcommons.web.WebUtils; -import okhttp3.Request; - -import javax.annotation.Nullable; -import java.io.IOException; -import java.security.GeneralSecurityException; -import java.time.Duration; -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; - -public class YoutubeUtils { - - private static YouTube youtube; - - static { - try { - youtube = new YouTube.Builder( - GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), null) - .setApplicationName("SkyBot-youtube-search") - .build(); - } - catch (GeneralSecurityException | IOException e) { - e.printStackTrace(); - } - } - - public static Video getVideoById(String videoID, String apiKey) throws IOException { - return getVideosByIdBase(videoID, apiKey) - .setMaxResults(1L) - .execute() - .getItems() - .get(0); - } - - public static List