Skip to content

Commit

Permalink
add YTDLPFallbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
awidesky committed Feb 28, 2023
1 parent 28727c1 commit 71565f7
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.Callable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand All @@ -20,6 +18,7 @@
import com.awidesky.YoutubeClipboardAutoDownloader.util.SwingDialogs;
import com.awidesky.YoutubeClipboardAutoDownloader.util.exec.BinaryInstaller;
import com.awidesky.YoutubeClipboardAutoDownloader.util.exec.ProcessExecutor;
import com.awidesky.YoutubeClipboardAutoDownloader.util.exec.YTDLPFallbacks;

public class YoutubeAudioDownloader {

Expand All @@ -29,9 +28,6 @@ public class YoutubeAudioDownloader {
private static Pattern percentPtn = Pattern.compile("[0-9]+\\.*[0-9]+%");
private static Pattern versionPtn = Pattern.compile("\\d{4}\\.\\d{2}\\.\\d{2}");

private static Map<String, Callable<Boolean>> fallBackFix = new HashMap<>(); //TODO : use Predicate<String>


/**
* returns String represents the path to the running jar.
* */
Expand All @@ -43,27 +39,6 @@ private static String getProjectRootPath() {
return ret;
}

private static boolean runFixCommand(String error, String... command) {

Logger log = Main.getLogger("[runFixCommand] ");

log.log("\nFound known error : \"" + error + "\"");
log.log("Trying to fix error automatically by executing \"" + Arrays.stream(command).collect(Collectors.joining(" ")) + "\"");

// start process
try {
log.log("Executing ended with exit code : " + ProcessExecutor.runNow(log, null, command));
} catch (Exception e) {
SwingDialogs.error("Error!", "Error when fixing youtube-dl problem!\n%e%", e, false);
return false;
}
return true;

}




/**
* @return Whether the procedure went fine
* */
Expand Down Expand Up @@ -139,10 +114,6 @@ public static boolean checkYoutubedl() {
log.log("projectpath = " + projectpath);
log.log("youtubedlpath = " + (youtubedlpath.equals("") ? "system %PATH%" : youtubedlpath) + "\n");

fallBackFix.put("ERROR: unable to download video data: HTTP Error 403: Forbidden", () -> {
return runFixCommand("ERROR: unable to download video data: HTTP Error 403: Forbidden", youtubedlpath + "youtube-dl", "--rm-cache-dir");
});

return true;

}
Expand Down Expand Up @@ -202,9 +173,12 @@ private static boolean checkYoutubedlPath(String ydlfile, Logger log) {
public static String getProjectpath() {
return projectpath;
}
public static String getYoutubedlpath() {
public static String getResourcePath() {
return projectpath + File.separator + "YoutubeAudioAutoDownloader-resources";
}
public static String getYoutubedlPath() {
return youtubedlpath;
}



Expand Down Expand Up @@ -350,22 +324,19 @@ public static void download(String url, TaskData task, PlayListOption playListOp
try {

String line = null;
StringBuilder sb1 = new StringBuilder("");
Callable<Boolean> fix = null;
List<String> lines = new ArrayList<>();

while ((line = br.readLine()) != null) {

task.failed();
sb1.append(line);
task.logger.log("[downloading] youtube-dl stderr : " + line);
fix = fallBackFix.get(line);
lines.add(line);

}

if (!sb1.toString().equals("")) {
if (!lines.isEmpty()) {

if (fix != null && fix.call()) {
task.setVideoName(task.getVideoName() + " (an error occurred but fixed, continuing download)");
if (lines.stream().map(YTDLPFallbacks::runFixCommand).allMatch(b -> b)) {
task.setVideoName(task.getVideoName() + " (error occurred but fixed, continuing download)");
if (task.getTotalNumVideo() == 1) { // not a PlayList?
download(url, task, playListOption);
} else {
Expand All @@ -374,8 +345,10 @@ public static void download(String url, TaskData task, PlayListOption playListOp
return;
}

task.failed();
SwingDialogs.error("Error [" + task.getVideoName() + "]",
"[Task" + task.getTaskNum() + "|downloading] There's Error(s) in youtube-dl proccess!\n" + sb1.toString(), null, true);
"[Task" + task.getTaskNum() + "|downloading] There's Error(s) in youtube-dl proccess!\n"
+ lines.stream().collect(Collectors.joining("\n")), null, true);
}

} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class BinaryInstaller {

public static final String FFMPEG_URL = "https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip";
public static final String YTDLP_URL = "https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp.exe";
private static final String root = YoutubeAudioDownloader.getYoutubedlpath();
private static final String root = YoutubeAudioDownloader.getResourcePath();

private static JLabel loadingStatus;
private static JProgressBar progress;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.awidesky.YoutubeClipboardAutoDownloader.util.exec;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.awidesky.YoutubeClipboardAutoDownloader.Main;
import com.awidesky.YoutubeClipboardAutoDownloader.YoutubeAudioDownloader;
import com.awidesky.YoutubeClipboardAutoDownloader.util.Logger;
import com.awidesky.YoutubeClipboardAutoDownloader.util.SwingDialogs;

public class YTDLPFallbacks {

private static Logger log = Main.getLogger("[runFixCommand] ");
private static FixCommand[] fixArr = new FixCommand[] {
new FixCommand("ERROR: unable to download video data: HTTP Error 403: Forbidden", YoutubeAudioDownloader.getYoutubedlPath() + "youtube-dl", "--rm-cache-dir")
};

public static boolean runFixCommand(String err) {

List<FixCommand> list = Arrays.stream(fixArr).filter(f -> err.startsWith(f.error)).toList();

if(list.isEmpty()) {
log.log("no available fix for : " + err);
return false;
}
if (list.size() > 1) {
return list.stream().filter(f -> err.equals(f.error)).map(FixCommand::runFixCommand).allMatch(b-> b);
} else {
return list.get(0).runFixCommand();
}
}


static class FixCommand {

private final String error;
private final String[] command;

FixCommand(String error, String... command) {
this.error = error;
this.command = command;
}

public boolean runFixCommand() {

log.newLine();
log.log("Found known error : \"" + error + "\"");
log.log("Trying to fix error automatically by executing \"" + Arrays.stream(command).collect(Collectors.joining(" ")) + "\"");

// start process
try {
log.log("Executing ended with exit code : " + ProcessExecutor.runNow(log, null, command));
} catch (Exception e) {
SwingDialogs.error("Error!", "Error when fixing youtube-dl problem!\n%e%", e, false);
return false;
}
return true;
}
}
}

0 comments on commit 71565f7

Please sign in to comment.