From 45b654c5ecc015cecc0e4b634bf49e4c2dec9d76 Mon Sep 17 00:00:00 2001 From: YT-GameWorks Date: Sun, 18 Apr 2021 14:54:56 +0530 Subject: [PATCH] added initial code for api wrapper --- .../java/com/disbots/commands/music/Play.java | 18 +----------- .../java/com/disbots/core/api/IRequests.java | 15 ++++++++++ .../java/com/disbots/core/api/YoutubeApi.java | 28 +++++++++++++++++++ .../core/{ => api}/YoutubeApiService.java | 2 +- .../disbots/core/YoutubeApiServiceTest.java | 1 + 5 files changed, 46 insertions(+), 18 deletions(-) create mode 100644 src/main/java/com/disbots/core/api/IRequests.java create mode 100644 src/main/java/com/disbots/core/api/YoutubeApi.java rename src/main/java/com/disbots/core/{ => api}/YoutubeApiService.java (97%) diff --git a/src/main/java/com/disbots/commands/music/Play.java b/src/main/java/com/disbots/commands/music/Play.java index a609829..c98f4ce 100644 --- a/src/main/java/com/disbots/commands/music/Play.java +++ b/src/main/java/com/disbots/commands/music/Play.java @@ -5,6 +5,7 @@ import com.disbots.util.logging.Log; import com.disbots.util.logging.LogTypes; import com.disbots.util.music.LavaPlayerAudioSource; +import com.google.api.services.youtube.YouTube; import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler; import com.sedmelluq.discord.lavaplayer.player.AudioPlayer; import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager; @@ -36,23 +37,6 @@ public void OnPlayCommand(MessageCreateEvent message, String[] args) try { - /* - TODO: Enable youtube api. - - YouTube youtubeService = getService(); - // Define and execute the API request - YouTube.Search.List request = youtubeService.search() - .list("snippet"); - SearchListResponse response = request.setMaxResults(1L).setKey(DEVELOPER_KEY) - .setOrder("viewCount") - .setQ(args[0]) - .execute(); - - JSONObject httpResponse = new JSONObject(response); - - System.out.println(httpResponse.get("videoId")); - */ - voiceChannel = message.getMessageAuthor().getConnectedVoiceChannel().get(); EmbedBuilder LoadingEmbed = new EmbedBuilder() diff --git a/src/main/java/com/disbots/core/api/IRequests.java b/src/main/java/com/disbots/core/api/IRequests.java new file mode 100644 index 0000000..40c80e0 --- /dev/null +++ b/src/main/java/com/disbots/core/api/IRequests.java @@ -0,0 +1,15 @@ +package com.disbots.core.api; + +import com.google.api.services.youtube.model.SearchListResponse; + +import java.io.IOException; +import java.security.GeneralSecurityException; + +/** + * @apiNote Interface, that contains all requests to + * youtube api. + */ +public interface IRequests +{ + SearchListResponse SearchVideoByQuery(Long MaxResults, String setOrder, String Query) throws GeneralSecurityException, IOException; +} diff --git a/src/main/java/com/disbots/core/api/YoutubeApi.java b/src/main/java/com/disbots/core/api/YoutubeApi.java new file mode 100644 index 0000000..b877ec8 --- /dev/null +++ b/src/main/java/com/disbots/core/api/YoutubeApi.java @@ -0,0 +1,28 @@ +package com.disbots.core.api; + +import com.disbots.core.Main; +import com.google.api.services.youtube.YouTube; +import com.google.api.services.youtube.model.SearchListResponse; + +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.Collections; + +import static com.disbots.core.api.YoutubeApiService.getService; + +public class YoutubeApi implements IRequests +{ + @Override + public SearchListResponse SearchVideoByQuery(Long MaxResults, String Order, String Query) throws GeneralSecurityException, IOException + { + YouTube youtubeService = getService(); + YouTube.Search.List request = youtubeService.search() + .list(Collections.singletonList("snippet")); + SearchListResponse response = request.setMaxResults(MaxResults).setKey(Main.DEVELOPER_KEY) + .setOrder(Order) + .setQ(Query) + .execute(); + + return response; + } +} diff --git a/src/main/java/com/disbots/core/YoutubeApiService.java b/src/main/java/com/disbots/core/api/YoutubeApiService.java similarity index 97% rename from src/main/java/com/disbots/core/YoutubeApiService.java rename to src/main/java/com/disbots/core/api/YoutubeApiService.java index 105fdce..d08c2c4 100644 --- a/src/main/java/com/disbots/core/YoutubeApiService.java +++ b/src/main/java/com/disbots/core/api/YoutubeApiService.java @@ -1,4 +1,4 @@ -package com.disbots.core; +package com.disbots.core.api; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; diff --git a/src/test/java/com/disbots/core/YoutubeApiServiceTest.java b/src/test/java/com/disbots/core/YoutubeApiServiceTest.java index a23e851..0dfdc2f 100644 --- a/src/test/java/com/disbots/core/YoutubeApiServiceTest.java +++ b/src/test/java/com/disbots/core/YoutubeApiServiceTest.java @@ -1,5 +1,6 @@ package com.disbots.core; +import com.disbots.core.api.YoutubeApiService; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test;