From 499fe2a90287c5a84a4b32ebc43fc7a609e8871c Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Tue, 15 Aug 2023 14:56:16 -0700 Subject: fix: LOAD_FAILED error due to rate limitation --- pom.xml | 10 +++--- src/main/java/audio/Music.java | 5 +-- src/main/java/utility/YouTubeAPI.java | 58 ++++++++++++++++------------------- 3 files changed, 35 insertions(+), 38 deletions(-) diff --git a/pom.xml b/pom.xml index f2e211a..f47c925 100644 --- a/pom.xml +++ b/pom.xml @@ -32,6 +32,11 @@ + + dev.arbjerg + lavaplayer + 2.0.1 + net.dv8tion JDA @@ -42,11 +47,6 @@ jsoup 1.15.4 - - com.github.walkyst - lavaplayer-fork - 1.4.1 - com.googlecode.json-simple json-simple diff --git a/src/main/java/audio/Music.java b/src/main/java/audio/Music.java index 65e3564..cf30ee3 100644 --- a/src/main/java/audio/Music.java +++ b/src/main/java/audio/Music.java @@ -239,8 +239,9 @@ public class Music extends ListenerAdapter { loadAndPlay((TextChannel) event.getChannel(), youtubeAPI.returnTopVideoURL(spotifyAPI.getSearchTerm_sync(randomSong)), true); } else { - event.reply("Found Video: " + youtubeAPI.returnTopVideoURL(userQuery)).queue(); - loadAndPlay((TextChannel) event.getChannel(), youtubeAPI.returnTopVideoURL(userQuery), true); + String top_video = youtubeAPI.returnTopVideoURL(userQuery); + event.reply("Found Video: " + top_video).queue(); + loadAndPlay((TextChannel) event.getChannel(), top_video, true); } } catch (IOException e) { e.printStackTrace(); diff --git a/src/main/java/utility/YouTubeAPI.java b/src/main/java/utility/YouTubeAPI.java index 4acecf5..c50897a 100644 --- a/src/main/java/utility/YouTubeAPI.java +++ b/src/main/java/utility/YouTubeAPI.java @@ -1,50 +1,46 @@ package utility; +import com.google.gson.stream.JsonReader; import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; -import org.jsoup.Jsoup; -import java.io.IOException; +import java.io.*; +import java.net.URL; import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; +import java.nio.charset.Charset; public class YouTubeAPI { private String ytapiKey = ""; public YouTubeAPI(String ytapiKey){ this.ytapiKey = ytapiKey; } - public String returnTopVideoURL(String keyword )throws IOException { - String url = "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q="+ URLEncoder.encode(keyword, StandardCharsets.UTF_8)+"&type=video&key="+ytapiKey; - System.out.println(url); - String data = Jsoup.connect(url).ignoreContentType(true).execute().body(); - JSONObject obj = new JSONObject(data); - JSONArray arr = obj.getJSONArray("items"); - String videoID = ""; - for (int i = 0; i < arr.length(); i++) - { - videoID = arr.getJSONObject(i).getJSONObject("id").getString("videoId"); - System.out.println("Parsed ID "+ videoID); + + public static String convertToURLCompatible(String inputText) { + try { + String encodedText = URLEncoder.encode(inputText, "UTF-8"); + return encodedText.replace("+", "%20"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + return ""; } - return "https://www.youtube.com/watch?v="+videoID; } - public void getAllURLPlaylist(String playlistID){ - try { - String url = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId="+playlistID+"&key="+ytapiKey; - System.out.println(url); - url = url.replaceAll(" ", "%20"); - String data = Jsoup.connect(url).ignoreContentType(true).execute().body(); - JSONObject obj = new JSONObject(data); - JSONArray arr = obj.getJSONArray("items"); - //print arr - for (int i = 0; i < arr.length(); i++) { - String videoID = arr.getJSONObject(i).getJSONObject("id").getString("videoId"); - System.out.println("Parsed ID " + videoID); - } + public String returnTopVideoURL(String keyword) throws IOException { + String url = "https://www.googleapis.com/youtube/v3/search?q=" + convertToURLCompatible(keyword) + "&key=" + ytapiKey; + try (InputStream is = new URL(url).openStream(); + BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")))) { + StringBuilder sb = new StringBuilder(); + int cb; + while ((cb = br.read()) != -1) { + sb.append((char) cb); } - catch (IOException e) { - e.printStackTrace(); + JSONObject jsonObject = new JSONObject(sb.toString()); + String result = jsonObject.getJSONArray("items").getJSONObject(0).getJSONObject("id").getString("videoId"); + return "https://www.youtube.com/watch?v=" + result; + } catch (IOException e) { + throw new RuntimeException(e); } } + } -- cgit v1.2.3