diff options
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/java/audio/Music.java | 5 | ||||
| -rw-r--r-- | src/main/java/utility/YouTubeAPI.java | 58 |
2 files changed, 30 insertions, 33 deletions
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);
}
}
+
}
|
