Skip to content

Commit

Permalink
change exception handling in main & setup method
Browse files Browse the repository at this point in the history
  • Loading branch information
awidesky committed Jul 22, 2021
1 parent a11a8a2 commit 0c4aa4d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
26 changes: 14 additions & 12 deletions src/com/awidesky/YoutubeClipboardAutoDownloader/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,14 @@ public class Main {

public static void main(String[] args) {

try {
setup(args);
} catch (Exception e) {
GUI.error("Error when initializing application!", (e == null) ? "" : e.getClass() + " : %e%", e);
kill(1);
}
if(!setup(args)) kill(1);

}


private static void setup(String[] args) throws Exception {
/**
* @return Whether the procedure went fine
* */
private static boolean setup(String[] args) {

prepareLogFile();

Expand All @@ -74,13 +71,13 @@ private static void setup(String[] args) throws Exception {

log("[init] failed wating EDT!");
log(e2);

return false;
}

gui.setLoadingStat(LoadingStatus.CHECKING_YDL);
YoutubeAudioDownloader.checkYoutubedl();
if (!YoutubeAudioDownloader.checkYoutubedl()) return false;
gui.setLoadingStat(LoadingStatus.CHECKING_FFMPEG);
YoutubeAudioDownloader.checkFfmpeg();
if (!YoutubeAudioDownloader.checkFfmpeg()) return false;

gui.setLoadingStat(LoadingStatus.READING_PROPERTIES);
readProperties();
Expand Down Expand Up @@ -112,7 +109,8 @@ private static void setup(String[] args) throws Exception {
});

log("\nistening clipboard...\n");

return true;

}

private static void checkClipBoard() {
Expand Down Expand Up @@ -245,6 +243,10 @@ private static void prepareLogFile() {

}


/**
* note - this method should be Exception-proof.
* */
private static void readProperties() {

String p = YoutubeAudioDownloader.getProjectpath(); //Default path for save is project root path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public int getTotalNumVideo() {
public void setNowVideoNum(int now) {
videoNum = now;
}
}


public int getVideoNum() {
return videoNum;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -64,7 +65,10 @@ public static void setArgsOptions(String options) {
}


public static void checkYoutubedl() {
/**
* @return Whether the procedure went fine
* */
public static boolean checkYoutubedl() {

Main.log("\n");
/* check youtube-dl */
Expand All @@ -79,9 +83,9 @@ public static void checkYoutubedl() {

if (!checkYoutubedlPath(youtubedlpath + "youtube-dl")) {

GUI.error("Error!", "youtube-dl does not exist in\n\t" + youtubedlpath + "\tor system %path%!", null);
GUI.error("Error!", "youtube-dl does not exist in\n\t" + youtubedlpath + "\nor system %path%!", null);
if (GUI.confirm("Open link in browser?", "Move to download page of youtube-dl?")) Main.webBrowse("http://ytdl-org.github.io/youtube-dl/download.html");
Main.kill(1);
return false;

}

Expand All @@ -91,9 +95,14 @@ public static void checkYoutubedl() {
Main.log("[init] youtubedlpath = " + (youtubedlpath.equals("") ? "system %path%" : youtubedlpath));
Main.log("\n");

return true;

}

public static void checkFfmpeg() {
/**
* @return Whether the procedure went fine
* */
public static boolean checkFfmpeg() {

/* check ffmpeg */
//ffmpeg -version
Expand All @@ -118,13 +127,14 @@ public static void checkFfmpeg() {

} catch (Exception e) {

GUI.error("Error!", "ffmpeg does not exist in\n\t" + youtubedlpath + "\tor system %path%!", null);
GUI.error("Error!", "ffmpeg does not exist in\n\t" + youtubedlpath + "\nor system %path%!", null);
if (GUI.confirm("Open link in browser?", "Move to download page of ffmpeg?")) Main.webBrowse("https://ffmpeg.org/download.html");
Main.kill(1);
return false;

}

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

}

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

if(line.matches("\\[download\\] Downloading video [\\d]+ of [\\d]+"))) {
if(line.matches("\\[download\\] Downloading video [\\d]+ of [\\d]+")) {
Scanner sc = new Scanner(line);
sc.useDelimiter("[^\\d]+");
task.setNowVideoNum(sc.nextInt());
task.setTotalNumVideo(sc.nextInt());
sc.close();
}

if (line.startsWith("[download]")) {
if(line.contains(" 0.0%")) task.increaseVideoNum();

Matcher m = percentPtn.matcher(line);
if (m.find()) task.setProgress((int)Math.round(Double.parseDouble(m.group().replace("%", ""))));
}

Matcher m = percentPtn.matcher(line);
if (m.find()) task.setProgress((int)Math.round(Double.parseDouble(m.group().replace("%", ""))));


Main.log("[Task" + task.getTaskNum() + "|downloading] youtube-dl stdout : " + line);

Expand Down

0 comments on commit 0c4aa4d

Please sign in to comment.