aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPinapelz <yukais@pinapelz.com>2025-10-15 17:19:05 -0700
committerPinapelz <yukais@pinapelz.com>2025-10-15 17:19:05 -0700
commit4138c41ff23e6d0e61a13646c3e4115522323588 (patch)
treef99021fb1ab874c6f746f2cfe5ef4fbad2f8669b
parent5f0c90afdd30f97a2f941053e62527c362babd60 (diff)
add default 3 retry attempts to any download
-rw-r--r--src/main/java/Downloader.java8
-rw-r--r--src/main/java/Main.java62
2 files changed, 38 insertions, 32 deletions
diff --git a/src/main/java/Downloader.java b/src/main/java/Downloader.java
index 6442605..05363f1 100644
--- a/src/main/java/Downloader.java
+++ b/src/main/java/Downloader.java
@@ -108,7 +108,6 @@ public class Downloader {
}
File downloadedWebm = FileUtility.findFileWithType(System.getProperty("user.dir"), "webm");
if(downloadedWebm == null){
- UI.Modal.showError("Error finding downloaded webm file. Ensure that yt-dlp is able to download");
return false;
}
@@ -138,7 +137,6 @@ public class Downloader {
String imageUrl = "https://img.youtube.com/vi/" + urlID+"/";
File downloadedMp3 = FileUtility.findFileWithType(System.getProperty("user.dir"), "mp3");
if(downloadedMp3 == null){
- UI.Modal.showError("Error finding downloaded mp3 file. Ensure that yt-dlp is able to download");
return false;
}
String savedNonAlphaNumName;
@@ -146,7 +144,6 @@ public class Downloader {
savedNonAlphaNumName = downloadedMp3.getName();
}
catch(NullPointerException ex){
- UI.Modal.showError("Error finding downloaded mp3 file. Ensure that yt-dlp is able to download");
return false;
}
String tempRemoveAlphaNumeric = savedNonAlphaNumName.replaceAll("[^a-zA-Z0-9]", "") + ".mp3";
@@ -157,7 +154,6 @@ public class Downloader {
FileUtility.deleteFile(FileUtility.findJsonFile(System.getProperty("user.dir")));
downloadedMp3 = FileUtility.findFileWithType(System.getProperty("user.dir"), "mp3");
if(downloadedMp3 == null){
- UI.Modal.showError("Error finding downloaded mp3 file. Ensure that yt-dlp is able to download");
return false;
}
if(!downloadedMp3.renameTo(new File(outputDirectory+"/"+savedNonAlphaNumName+"["+startTime+"-"+endTime+"].mp3"))){
@@ -170,7 +166,7 @@ public class Downloader {
public boolean download(String url, String browser){
String ytDlpExecutable = "yt-dlp" + (System.getProperty("os.name").startsWith("Windows") ? ".exe" : "");
try {
- String[] command = {ytDlpExecutable, "--min-sleep-interval","2", "--max-sleep-interval", "7","--cookies-from-browser",browser,"-f", "bestaudio[ext=webm]", "-x",
+ String[] command = {ytDlpExecutable, "-4","--min-sleep-interval","2", "--max-sleep-interval", "7","--cookies-from-browser",browser,"-f", "bestaudio[ext=webm]", "-x",
"--audio-format", "mp3", "--write-info-json", url, "-o", "%(title)s[%(id)s].%(ext)s"};
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.directory(new File(System.getProperty("user.dir")));
@@ -188,7 +184,6 @@ public class Downloader {
String imageUrl = "https://img.youtube.com/vi/" + urlID + "/";
File downloadedMp3 = FileUtility.findFileWithType(System.getProperty("user.dir"), "mp3");
if(downloadedMp3 == null){
- UI.Modal.showError("Error finding downloaded mp3 file. Ensure that yt-dlp is able to download");
return false;
}
String savedNonAlphaNumName = downloadedMp3.getName();
@@ -202,7 +197,6 @@ public class Downloader {
tagMp3InDir(uploader, title, imageUrl, System.getProperty("user.dir"));
downloadedMp3 = FileUtility.findFileWithType(System.getProperty("user.dir"), "mp3");
if(downloadedMp3 == null){
- UI.Modal.showError("Error finding downloaded mp3 file. Ensure that yt-dlp is able to download");
return false;
}
if(!downloadedMp3.renameTo(new File(outputDirectory+"/"+savedNonAlphaNumName))){
diff --git a/src/main/java/Main.java b/src/main/java/Main.java
index 229d752..19452e7 100644
--- a/src/main/java/Main.java
+++ b/src/main/java/Main.java
@@ -73,43 +73,55 @@ public class Main extends JFrame {
return (int) (((double) current / (double) total) * 100);
}
- public void downloadAndTag(){
+ public void downloadAndTag() {
ArrayList<String> songs = FileUtility.txtToList(textPath);
String browser = configuration.get("browser");
int totalSongs = songs.size();
int songsProcessed = 0;
- for(String line: songs){
+
+ for (String line : songs) {
System.out.println(line);
- if(line.contains(",")){
- String[] parts = line.split(",");
- String url = parts[0];
- String stamp = parts[1];
- Downloader downloader = new Downloader(completedDir, outputArea);
- try{
- if(!downloader.download(url, stamp, browser)){
- UI.Modal.showError("Error downloading song: " + url + " at timestamp: " + stamp);
- }
- }
- catch (Exception e){
- UI.Modal.showError("We were unable to download a song, please check to logs " + e);
- return;
- }
+ Downloader downloader = new Downloader(completedDir, outputArea);
+ boolean success = false;
- }
- else{
+ for (int attempt = 1; attempt <= 3; attempt++) {
+ try {
+ if (line.contains(",")) {
+ String[] parts = line.split(",");
+ String url = parts[0];
+ String stamp = parts[1];
- Downloader downloader = new Downloader(completedDir, outputArea);
- try{
- if(!downloader.download(line, browser)){
- UI.Modal.showError("Error downloading song: " + line);
+ if (downloader.download(url, stamp, browser)) {
+ success = true;
+ break;
+ } else {
+ System.out.println("Attempt " + attempt + " failed for " + url);
+ }
+ } else {
+ if (downloader.download(line, browser)) {
+ success = true;
+ break;
+ } else {
+ System.out.println("Attempt " + attempt + " failed for " + line);
+ }
}
+
+ } catch (Exception e) {
+ System.out.println("Error on attempt " + attempt + " for line: " + line + " -> " + e);
}
- catch (Exception e){
- UI.Modal.showError("We were unable to download a song, please check to logs " + e);
- return;
+
+ // wait before retrying (except after last attempt)
+ if (attempt < 3) {
+ try {
+ Thread.sleep(5000);
+ } catch (InterruptedException ignored) {}
}
+ }
+ if (!success) {
+ UI.Modal.showError("Failed to download after 3 attempts: " + line);
}
+
songsProcessed++;
progressBar.setValue(calculatePercentage(songsProcessed, totalSongs));
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage