From b8e9549304761b624e1b92c1245054a960c6297b Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Wed, 12 Nov 2025 19:54:51 -0800 Subject: add confirmation before downloading for duplicate urls checks the output dir to see if there is already a file with that videoId if it is, then show a confirmation prompt --- src/main/java/Downloader.java | 6 ++++-- src/main/java/FileUtility.java | 13 +++++++++++++ src/main/java/Main.java | 30 ++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/Downloader.java b/src/main/java/Downloader.java index aed0bb4..c480a85 100644 --- a/src/main/java/Downloader.java +++ b/src/main/java/Downloader.java @@ -254,8 +254,6 @@ public class Downloader { return true; } - - public static int timestampToSeconds(String timestamp) { int totalSeconds = 0; try { @@ -271,4 +269,8 @@ public class Downloader { } return totalSeconds; } + + public boolean videoIdAlreadyDownloaded(String videoId){ + return FileUtility.findFileContainingString(this.outputDirectory, videoId); + } } diff --git a/src/main/java/FileUtility.java b/src/main/java/FileUtility.java index 1c532e4..936cecd 100644 --- a/src/main/java/FileUtility.java +++ b/src/main/java/FileUtility.java @@ -84,6 +84,19 @@ public class FileUtility { return null; } + public static boolean findFileContainingString(String directory, String searchString){ + File dir = new File(directory); + File[] files = dir.listFiles(); + assert files != null; + for(File file : files){ + if(file.getName().contains(searchString)){ + return true; + } + } + return false; + } + + public static String findJsonFile(String folderName) { File folder = new File(folderName); File[] listOfFiles = folder.listFiles(); diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 474d244..7ab6572 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -6,6 +6,8 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import com.formdev.flatlaf.FlatIntelliJLaf; @@ -74,6 +76,21 @@ public class Main extends JFrame { return (int) (((double) current / (double) total) * 100); } + public static String extractVideoId(String youtubeUrl) { + if (youtubeUrl == null || youtubeUrl.isEmpty()) { + return null; + } + String pattern = "(?:youtube\\.com\\/(?:[^\\/]+\\/.+\\/|(?:v|e(?:mbed)?)\\/|.*[?&]v=)|youtu\\.be\\/)([^\"&?\\/ ]{11})"; + Pattern compiledPattern = Pattern.compile(pattern); + Matcher matcher = compiledPattern.matcher(youtubeUrl); + + if (matcher.find()) { + return matcher.group(1); + } + + return null; + } + public void downloadAndTag() { ArrayList songs = FileUtility.txtToList(textPath); String browser = configuration.get("browser"); @@ -83,6 +100,19 @@ public class Main extends JFrame { for (String line : songs) { System.out.println(line); Downloader downloader = new Downloader(completedDir, outputArea); + String videoId = extractVideoId(line); + if(downloader.videoIdAlreadyDownloaded(videoId)){ + int continueConfirm = JOptionPane.showConfirmDialog( + null, + "A file with the same video ID (" + videoId + ") already exists in the output directoy. Download anyways?", + "Potential duplicate detected", + JOptionPane.YES_NO_OPTION); + if(continueConfirm != JOptionPane.YES_OPTION){ + System.out.println("Skipping this URL as requested by user"); + songsProcessed++; + continue; + } + } boolean success = false; for (int attempt = 1; attempt <= 3; attempt++) { -- cgit v1.2.3