aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/utility/YouTubeAPI.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/utility/YouTubeAPI.java')
-rw-r--r--src/main/java/utility/YouTubeAPI.java58
1 files changed, 27 insertions, 31 deletions
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);
}
}
+
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage