diff options
Diffstat (limited to 'src/main/java/utility')
| -rw-r--r-- | src/main/java/utility/URLChecker.java | 12 | ||||
| -rw-r--r-- | src/main/java/utility/YouTubeAPI.java | 22 |
2 files changed, 32 insertions, 2 deletions
diff --git a/src/main/java/utility/URLChecker.java b/src/main/java/utility/URLChecker.java index ffea53b..499cca9 100644 --- a/src/main/java/utility/URLChecker.java +++ b/src/main/java/utility/URLChecker.java @@ -5,9 +5,14 @@ public class URLChecker { return term.matches("^(http|https)://.*");
}
public String getURLType(String url) {
- if (url.matches("^((?:https?:)?\\/\\/)?((?:www|m)\\.)?((?:youtube(-nocookie)?\\.com|youtu.be))(\\/(?:[\\w\\-]+\\?v=|embed\\/|v\\/)?)([\\w\\-]+)(\\S+)?$")) {
+ if(url.matches("^((?:https?:)?\\/\\/)?((?:www|m)\\.)?youtube\\.com\\/playlist\\?list=([\\w\\-]+)$")){
+ System.out.println("yt-playlist");
+ return "yt-playlist";
+ }
+ else if (url.matches("^((?:https?:)?\\/\\/)?((?:www|m)\\.)?((?:youtube(-nocookie)?\\.com|youtu.be))(\\/(?:[\\w\\-]+\\?v=|embed\\/|v\\/)?)([\\w\\-]+)(\\S+)?$")) {
return "yt"; //Youtube
- } else if (url.matches("^(https?:\\/\\/)?(www.)?(m\\.)?soundcloud\\.com\\/[\\w\\-\\.]+(\\/)+[\\w\\-\\.]+/?$")) {
+ }
+ else if (url.matches("^(https?:\\/\\/)?(www.)?(m\\.)?soundcloud\\.com\\/[\\w\\-\\.]+(\\/)+[\\w\\-\\.]+/?$")) {
return "snd";
} else if (url.matches("^(?:https?:\\/\\/)?(?:www\\.|go\\.)?twitch\\.tv\\/([a-z0-9_]+)($|\\?)")) {
return "twitch";
@@ -28,4 +33,7 @@ public class URLChecker { return uriParts[0].replaceAll("https://open.spotify.com/playlist/","");
}
+ public String getYouTubePlaylistID(String url){
+ return url.split("list=")[1];
+ }
}
diff --git a/src/main/java/utility/YouTubeAPI.java b/src/main/java/utility/YouTubeAPI.java index c50897a..982a1d3 100644 --- a/src/main/java/utility/YouTubeAPI.java +++ b/src/main/java/utility/YouTubeAPI.java @@ -9,6 +9,7 @@ import java.io.*; import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
public class YouTubeAPI {
private String ytapiKey = "";
@@ -43,4 +44,25 @@ public class YouTubeAPI { }
}
+ public String[] getPlaylistVideoURLs(String playlistID, int maxResults) throws IOException{
+ String url = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults="+ maxResults +"&playlistId=" + playlistID + "&key=" + ytapiKey;
+ try (InputStream is = new URL(url).openStream();
+ BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
+ StringBuilder sb = new StringBuilder();
+ int cb;
+ while ((cb = br.read()) != -1) {
+ sb.append((char) cb);
+ }
+ JSONObject jsonObject = new JSONObject(sb.toString());
+ JSONArray jsonArray = jsonObject.getJSONArray("items");
+ String[] result = new String[jsonArray.length()];
+ for(int i = 0; i < jsonArray.length(); i++){
+ result[i] = "https://www.youtube.com/watch?v=" + jsonArray.getJSONObject(i).getJSONObject("snippet").getJSONObject("resourceId").getString("videoId");
+ }
+ return result;
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
}
|
