From 85e3f8e47554b8901fb1a9f52508edb1a21fbee3 Mon Sep 17 00:00:00 2001 From: Tarun Kumar Kanakam Date: Mon, 2 Sep 2024 08:33:50 +0530 Subject: [PATCH 1/4] Fix: Added code to Standardized YouTube link format for issue #601 --- CLI/src/main/java/main/Drifty_CLI.java | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/CLI/src/main/java/main/Drifty_CLI.java b/CLI/src/main/java/main/Drifty_CLI.java index c5609029..8328488b 100644 --- a/CLI/src/main/java/main/Drifty_CLI.java +++ b/CLI/src/main/java/main/Drifty_CLI.java @@ -294,6 +294,38 @@ public static void main(String[] args) { if (isInstagram(link)) { link = formatInstagramLink(link); } + else if (isYoutube(link)) { + String videoId = null; + + try { + URI uri = URI.create(link); + String domain = uri.getHost(); + + // checking if the domain is youtu.be + if ("youtu.be".equals(domain)) { + String path = uri.getPath(); + if (path != null && path.length() > 1) { + videoId = path.substring(1); // removing the leading "/" + } + } + // checking if the domain is youtube.com + else if ("www.youtube.com".equals(domain) || "youtube.com".equals(domain)) { + Map queryParams = extractQueryParams(link, "v"); + videoId = queryParams.get("v"); + } + + if (videoId != null) { + // constructing link in youtube.com/watch?v={videoID} + uri = new URI("https", "www.youtube.com", "/watch", "v=" + videoId, null); + link = uri.toString(); + } else { + messageBroker.msgLinkError("YouTube video ID not found in the link."); + } + + } catch (IllegalArgumentException | URISyntaxException e) { + messageBroker.msgLinkError("Failed to process the YouTube link: " + e.getMessage()); + } + } messageBroker.msgFilenameInfo("Retrieving filename from link..."); fileName = findFilenameInLink(link); if (fileName != null && !fileName.isEmpty()) { From bfb3051865a8417daeee6a6bcd6acbb670b462ce Mon Sep 17 00:00:00 2001 From: Tarun Kumar Kanakam Date: Mon, 2 Sep 2024 08:41:49 +0530 Subject: [PATCH 2/4] Add: Added extractQueryParams function for issue #601 --- Core/src/main/java/utils/Utility.java | 62 +++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/Core/src/main/java/utils/Utility.java b/Core/src/main/java/utils/Utility.java index 78066a68..039c8e3a 100644 --- a/Core/src/main/java/utils/Utility.java +++ b/Core/src/main/java/utils/Utility.java @@ -860,4 +860,66 @@ public static int parseStringToInt(String string, String errorMessage, MessageCa return 0; } } + + /** + * Extracts the specified query parameters from the given URL. If no parameter names are provided (null or empty), all parameters are returned. + * + * @param urlLink The URL string from which to extract parameters. + * @param paramNames The names of the query parameters to extract. If null or empty, all parameters will be returned. + * @return A map containing the query parameter names and their corresponding values. + */ + public static Map extractQueryParams(String urlLink, String... paramNames) { + Map paramMap = new HashMap<>(); + + URL url = null; + try { + url = URI.create(urlLink).toURL(); + } catch (MalformedURLException e) { + M.msgLinkError("Connection to the link timed out! Please check your internet connection. " + e.getMessage()); + } + String query = url != null ? url.getQuery() : null; + + // query is null or empty, return an empty map (no query parameters) + if (query == null || query.isEmpty()) { + return paramMap; + } + + // splitting query string into individual parameters + String[] params = query.split("&"); + + // check if specific parameters are requested or if all should be returned + boolean returnAllParams = (paramNames == null || paramNames.length == 0); + + for (String param : params) { + String[] pair = param.split("="); + if (pair.length == 2) { + String paramName = pair[0]; + String paramValue = pair[1]; + + // add parameter to the map if it's requested or if all parameters should be returned + if (returnAllParams || contains(paramNames, paramName)) { + paramMap.put(paramName, paramValue); + } + } + } + + return paramMap; + } + + /** + * Helper method to check if an array contains a specific value. + * + * @param array The array to check. + * @param value The value to search for. + * @return True if the array contains the value, false otherwise. + */ + private static boolean contains(String[] array, String value) { + if (array == null) return false; + for (String item : array) { + if (item.equals(value)) { + return true; + } + } + return false; + } } From 17a956c829485bf41ca458014c4b7997799778b5 Mon Sep 17 00:00:00 2001 From: Tarun Kumar Kanakam Date: Mon, 9 Sep 2024 09:53:39 +0530 Subject: [PATCH 3/4] Minor tweaks for issue 601 --- CLI/src/main/java/main/Drifty_CLI.java | 9 ++++++++- Core/src/main/java/utils/Utility.java | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CLI/src/main/java/main/Drifty_CLI.java b/CLI/src/main/java/main/Drifty_CLI.java index 8328488b..835edfd5 100644 --- a/CLI/src/main/java/main/Drifty_CLI.java +++ b/CLI/src/main/java/main/Drifty_CLI.java @@ -296,7 +296,6 @@ public static void main(String[] args) { } else if (isYoutube(link)) { String videoId = null; - try { URI uri = URI.create(link); String domain = uri.getHost(); @@ -320,6 +319,14 @@ else if ("www.youtube.com".equals(domain) || "youtube.com".equals(domain)) { link = uri.toString(); } else { messageBroker.msgLinkError("YouTube video ID not found in the link."); + messageBroker.msgInputInfo(QUIT_OR_CONTINUE, true); + String choice = SC.next().toLowerCase().strip(); + if ("q".equals(choice)) { + LOGGER.log(MessageType.INFO, CLI_APPLICATION_TERMINATED); + break; + } + printBanner(); + continue; } } catch (IllegalArgumentException | URISyntaxException e) { diff --git a/Core/src/main/java/utils/Utility.java b/Core/src/main/java/utils/Utility.java index 039c8e3a..73e56584 100644 --- a/Core/src/main/java/utils/Utility.java +++ b/Core/src/main/java/utils/Utility.java @@ -875,7 +875,7 @@ public static Map extractQueryParams(String urlLink, String... p try { url = URI.create(urlLink).toURL(); } catch (MalformedURLException e) { - M.msgLinkError("Connection to the link timed out! Please check your internet connection. " + e.getMessage()); + msgBroker.msgLinkError("Connection to the link timed out! Please check your internet connection. " + e.getMessage()); } String query = url != null ? url.getQuery() : null; From 609b8526e9fb14706681c98424087a131ffc1de5 Mon Sep 17 00:00:00 2001 From: Tarun Kumar Kanakam Date: Mon, 30 Sep 2024 11:42:52 +0530 Subject: [PATCH 4/4] Moved Youtube URL formatting to Utility file --- CLI/src/main/java/main/Drifty_CLI.java | 45 +++++++------------------- Core/src/main/java/utils/Utility.java | 33 ++++++++++++++++--- 2 files changed, 40 insertions(+), 38 deletions(-) diff --git a/CLI/src/main/java/main/Drifty_CLI.java b/CLI/src/main/java/main/Drifty_CLI.java index 835edfd5..fa0004a8 100644 --- a/CLI/src/main/java/main/Drifty_CLI.java +++ b/CLI/src/main/java/main/Drifty_CLI.java @@ -21,6 +21,7 @@ import utils.Logger; import java.io.*; +import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.nio.charset.Charset; @@ -295,42 +296,18 @@ public static void main(String[] args) { link = formatInstagramLink(link); } else if (isYoutube(link)) { - String videoId = null; try { - URI uri = URI.create(link); - String domain = uri.getHost(); - - // checking if the domain is youtu.be - if ("youtu.be".equals(domain)) { - String path = uri.getPath(); - if (path != null && path.length() > 1) { - videoId = path.substring(1); // removing the leading "/" - } - } - // checking if the domain is youtube.com - else if ("www.youtube.com".equals(domain) || "youtube.com".equals(domain)) { - Map queryParams = extractQueryParams(link, "v"); - videoId = queryParams.get("v"); - } - - if (videoId != null) { - // constructing link in youtube.com/watch?v={videoID} - uri = new URI("https", "www.youtube.com", "/watch", "v=" + videoId, null); - link = uri.toString(); - } else { - messageBroker.msgLinkError("YouTube video ID not found in the link."); - messageBroker.msgInputInfo(QUIT_OR_CONTINUE, true); - String choice = SC.next().toLowerCase().strip(); - if ("q".equals(choice)) { - LOGGER.log(MessageType.INFO, CLI_APPLICATION_TERMINATED); - break; - } - printBanner(); - continue; - } - - } catch (IllegalArgumentException | URISyntaxException e) { + link = formatYoutubeLink(link); + } catch (IllegalArgumentException | URISyntaxException | MalformedURLException e) { messageBroker.msgLinkError("Failed to process the YouTube link: " + e.getMessage()); + messageBroker.msgInputInfo(QUIT_OR_CONTINUE, true); + String choice = SC.next().toLowerCase().strip(); + if ("q".equals(choice)) { + LOGGER.log(MessageType.INFO, CLI_APPLICATION_TERMINATED); + break; + } + printBanner(); + continue; } } messageBroker.msgFilenameInfo("Retrieving filename from link..."); diff --git a/Core/src/main/java/utils/Utility.java b/Core/src/main/java/utils/Utility.java index 73e56584..e1fb6ab4 100644 --- a/Core/src/main/java/utils/Utility.java +++ b/Core/src/main/java/utils/Utility.java @@ -7,10 +7,7 @@ import org.apache.commons.text.StringEscapeUtils; import org.hildan.fxgson.FxGson; import preferences.AppSettings; -import properties.MessageCategory; -import properties.Mode; -import properties.OS; -import properties.Program; +import properties.*; import java.io.*; import java.net.*; @@ -33,6 +30,7 @@ import static properties.Program.YT_DLP; import static support.Constants.*; +import static utils.Utility.extractQueryParams; public class Utility { private static final Random RANDOM_GENERATOR = new Random(System.currentTimeMillis()); @@ -861,6 +859,33 @@ public static int parseStringToInt(String string, String errorMessage, MessageCa } } + public static String formatYoutubeLink(String link) throws MalformedURLException, URISyntaxException { + String videoId = null; + URI uri = URI.create(link); + String domain = uri.getHost(); + + if ("youtu.be".equals(domain)) { + String path = uri.getPath(); + if (path != null && path.length() > 1) { + videoId = path.substring(1); // removing the leading "/" + } + } + + else if ("www.youtube.com".equals(domain) || "youtube.com".equals(domain)) { + Map queryParams = extractQueryParams(link, "v"); + videoId = queryParams.get("v"); + } + + if (videoId != null) { + uri = new URI("https", "www.youtube.com", "/watch", "v=" + videoId, null); + link = uri.toString(); + } else { + throw new MalformedURLException(link + " is not a valid youtube link!"); + } + + return link; + } + /** * Extracts the specified query parameters from the given URL. If no parameter names are provided (null or empty), all parameters are returned. *