Skip to content

Commit

Permalink
error fixing transaction is fixed to work
Browse files Browse the repository at this point in the history
  • Loading branch information
awidesky committed Nov 17, 2021
1 parent 86ee2a8 commit ab66086
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
2 changes: 1 addition & 1 deletion ant.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<!--ANT 1.7 is required-->
<property name="dir.buildfile" value="."/>

<property name="version" value="v1.6.1"/>
<property name="version" value="v1.6.5"/>
<property name="target" value="1.8"/>
<property name="source" value="1.8"/>
<property name="debuglevel" value="source,lines,vars"/>
Expand Down
2 changes: 1 addition & 1 deletion src/com/awidesky/YoutubeClipboardAutoDownloader/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class Main {

private static volatile int taskNum = 0;

public static final String version = "v1.6.1";
public static final String version = "v1.6.5";

public static void main(String[] args) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void setNowVideoNum(int now) {
}


public int getVideoNum() {
public int getNowVideoNum() {
return videoNum;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
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 @@ -24,34 +27,39 @@ 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, Runnable> fallBackFix = new HashMap<>();
private static Map<String, Callable<Boolean>> fallBackFix = new HashMap<>();


private static void runFixCommand(String error, String... command) {
private static int runFixCommand(String error, String... command) {

int ret;
ProcessBuilder pb = new ProcessBuilder(command);

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

// start process
try {

Process p = pb.directory(null).start();
ret = p.waitFor();

try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
Main.log(br.readLine());
} catch (IOException e1) { throw e1; }

Main.log("Executing ended with exit code : " + p.waitFor());
Main.log("Executing ended with exit code : " + ret);

} catch (Exception e) {

ret = -1;
GUI.error("Error!", "Error when fixing youtube-dl problem!\n%e%", e);

}

Main.log("\n");
return ret;

}


Expand Down Expand Up @@ -91,7 +99,7 @@ public static boolean checkYoutubedl() {
Main.log("\n");

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

return true;
Expand Down Expand Up @@ -192,7 +200,7 @@ public static String getProjectpath() {
}


public static void download(String url, TaskData task, PlayListOption playListOption) {
public static void download(String url, TaskData task, PlayListOption playListOption, String... additionalArgument) {

Main.log("\n"); Main.log("\n");
Main.logProperties("[Task" + task.getTaskNum() + "|preparing] Current");
Expand All @@ -201,11 +209,18 @@ public static void download(String url, TaskData task, PlayListOption playListOp
/* download video */
Main.log("\n");
long startTime = System.nanoTime();
ProcessBuilder pb = new ProcessBuilder(youtubedlpath + "youtube-dl" + options, "--newline",

ArrayList<String> arguments = new ArrayList<>(Arrays.asList(
youtubedlpath + "youtube-dl" + options, "--newline",
"--extract-audio", playListOption.toCommandArgm(), "--audio-format",
Config.getFormat(), "--output", "\"" + Config.getFileNameFormat() + "\"", "--audio-quality",
Config.getQuality(), url);

Config.getQuality()
));
arguments.addAll(Arrays.asList(additionalArgument));
arguments.add(url);

ProcessBuilder pb = new ProcessBuilder(arguments);

// retrieve command line argument
Main.log("[Task" + task.getTaskNum() + "|downloading] Donwloading video by \"" + pb.command().stream().collect(Collectors.joining(" ")) + "\"");

Expand Down Expand Up @@ -235,6 +250,7 @@ public static void download(String url, TaskData task, PlayListOption playListOp
while ((line = br.readLine()) != null) {

if(line.matches("\\[download\\] Downloading video [\\d]+ of [\\d]+")) {
Main.log("\n");
Scanner sc = new Scanner(line);
sc.useDelimiter("[^\\d]+");
task.setNowVideoNum(sc.nextInt());
Expand Down Expand Up @@ -263,7 +279,7 @@ public static void download(String url, TaskData task, PlayListOption playListOp

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

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

Expand All @@ -276,13 +292,22 @@ public static void download(String url, TaskData task, PlayListOption playListOp

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

GUI.error("Error in youtube-dl", "[Task" + task.getTaskNum()
+ "|downloading] There's Error(s) in youtube-dl proccess!", null);
if(fix != null && fix.call()) {
task.setVideoName(task.getVideoName() + " (an error occurred but fixed, continuing download)");
if(task.getTotalNumVideo() == 1) { //not a PlayList?
download(url, task, playListOption);
} else {
download(url, task, playListOption, "--playlist-start", String.valueOf(task.getNowVideoNum()));
}
return;

}

if(fix != null) fix.run();
GUI.error("Error in youtube-dl", "[Task" + task.getTaskNum()
+ "|downloading] There's Error(s) in youtube-dl proccess!", null);
}

} catch (IOException e) {
} catch (Exception e) {

task.setStatus("ERROR");
GUI.error("[Task" + task.getTaskNum() + "|downloading] Error when redirecting error output of youtube-dl", "%e%", e);
Expand Down

0 comments on commit ab66086

Please sign in to comment.