Skip to content

Commit

Permalink
Updated
Browse files Browse the repository at this point in the history
Signed-off-by: Saptarshi Sarkar <saptarshi.programmer@gmail.com>
  • Loading branch information
SaptarshiSarkar12 committed Oct 23, 2022
1 parent 92e95c6 commit 216f0c0
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 31 deletions.
8 changes: 4 additions & 4 deletions Drifty.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
<runtimeBits>64/32</runtimeBits>
</jre>
<versionInfo>
<fileVersion>1.2.1.0</fileVersion>
<txtFileVersion>1.2.0.0</txtFileVersion>
<fileVersion>1.2.2.0</fileVersion>
<txtFileVersion>1.2.1.0</txtFileVersion>
<fileDescription>Drifty is an open-source interactive File Downloader system built with java.</fileDescription>
<copyright>Copyright © 2022 - 32 Saptarshi Sarkar</copyright>
<productVersion>1.2.1.0</productVersion>
<txtProductVersion>1.2.0.0</txtProductVersion>
<productVersion>1.2.2.0</productVersion>
<txtProductVersion>1.2.1.0</txtProductVersion>
<productName>Drifty</productName>
<companyName></companyName>
<internalName>Drifty CLI</internalName>
Expand Down
Binary file modified Drifty_CLI.exe
Binary file not shown.
Binary file modified out/artifacts/Drifty_CLI_jar/Drifty.jar
Binary file not shown.
Binary file not shown.
Binary file modified out/production/Drifty/DefaultDownloadFolderLocationFinder.class
Binary file not shown.
Binary file modified out/production/Drifty/Drifty_CLI.class
Binary file not shown.
Binary file modified out/production/Drifty/FileDownloader.class
Binary file not shown.
Binary file added out/production/Drifty/copyYt_dlp.class
Binary file not shown.
2 changes: 0 additions & 2 deletions src/DefaultDownloadFolderLocationFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* This class deals with finding the path of the default downloads folder.
*/
class DefaultDownloadFolderLocationFinder {
// private static final String REG_NAME = "reg query \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders\" /v {374DE290-123F-4565-9164-39C4925E467B}";
private static final String REG_TOKEN = "REG_EXPAND_SZ";

/**
Expand All @@ -13,7 +12,6 @@ class DefaultDownloadFolderLocationFinder {
*/
public static String findPath() {
try {
// Process process = Runtime.getRuntime().exec(REG_NAME); // Deprecated
Process process = new ProcessBuilder("reg", "query", "\"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders\"", "/v", "{374DE290-123F-4565-9164-39C4925E467B}").start();
StreamReader reader = new StreamReader(process.getInputStream());
reader.start();
Expand Down
12 changes: 10 additions & 2 deletions src/Drifty_CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

/**
* This is the main class for the CLI version of Drifty.
* @author Saptarshi Sarkar, AndrexUni, Anurag-Bharati, Naachiket Pant, Fonta22
* @version 1.2.2
*/
public class Drifty_CLI {
private static String downloadsFolder;
Expand All @@ -12,7 +14,6 @@ public class Drifty_CLI {
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_CYAN = "\u001B[36m";
public static final String ANSI_PURPLE = "\u001B[35m";
private static boolean flag = false;
private static String fName = null;
protected static boolean isYoutubeURL;

Expand All @@ -23,7 +24,6 @@ public class Drifty_CLI {
public static void main(String[] args) {
logger.log("INFO", "Application Started !");
initialPrintBanner();
flag = true;
if (args.length > 0){
String URL = args[0];
String name = null;
Expand Down Expand Up @@ -187,6 +187,11 @@ private static boolean isURLValid(String link){
}
}

/**
* This method checks whether the link provided is of YouTube or not and returns the resultant boolean value accordingly.
* @param url link to the file to be downloaded.
* @return true if the url is of YouTube and false if it is not.
*/
public static boolean isYoutubeLink(String url) {
String pattern = "^(http(s)?:\\/\\/)?((w){3}.)?youtu(be|.be)?(\\.com)?\\/.+";
return url.matches(pattern);
Expand Down Expand Up @@ -249,6 +254,9 @@ private static void printBanner(){
System.out.println(ANSI_PURPLE+"===================================================================="+ANSI_RESET);
}

/**
* This method prints the banner without any colour of text except white.
*/
private static void initialPrintBanner(){
System.out.println("====================================================================");
System.out.println(" _____ _____ _____ ______ _______ __ __");
Expand Down
70 changes: 47 additions & 23 deletions src/FileDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,32 @@ public void run() {
}
}
try {
//If link is from YouTube
// If link is of an YouTube video, then the following block of code will execute.
if (Drifty_CLI.isYoutubeLink(link)) {
//download youtube video
System.out.println("Trying to download the file ...");
Drifty_CLI.logger.log("INFO", "Trying to download the file ...");

try {
ProcessBuilder processBuilder = new ProcessBuilder("yt-dlp", "--quiet", "--progress", "-P", dir, link);
processBuilder.inheritIO();
Process yt_dlp = processBuilder.start();

yt_dlp.waitFor();
int exitValueOfYt_Dlp = yt_dlp.exitValue();
if (exitValueOfYt_Dlp == 0){
System.out.println("Successfully downloaded the file!");
Drifty_CLI.logger.log("INFO", "Successfully downloaded the file!");
} else if (exitValueOfYt_Dlp == 1) {
System.out.println("Failed to download the file!");
Drifty_CLI.logger.log("INFO", "Failed to download the file!");
downloadFromYouTube("");
} catch (IOException e) {
try {
System.out.println("Getting ready to download the file...");
Drifty_CLI.logger.log("INFO", "Getting ready to download the file...");
copyYt_dlp cy = new copyYt_dlp();
cy.copyToTemp();
try {
downloadFromYouTube(copyYt_dlp.tempDir);
} catch (InterruptedException ie) {
System.out.println("User interrupted while downloading the file!");
Drifty_CLI.logger.log("ERROR", "User interrupted while downloading the file! " + ie.getMessage());
} catch (IOException io1){
System.out.println("Failed to download YouTube video!");
Drifty_CLI.logger.log("ERROR", "Failed to download YouTube video! " + io1.getMessage());
}
} catch (IOException io){
System.out.println("Failed to initialise YouTube video downloader!");
Drifty_CLI.logger.log("ERROR", "Failed to initialise YouTube video downloader! " + io.getMessage());
}

} catch (Exception e) {
e.printStackTrace();
Drifty_CLI.logger.log("ERROR", e.getMessage());
} catch (InterruptedException e) {
System.out.println("User interrupted while downloading the file!");
Drifty_CLI.logger.log("ERROR", "User interrupted while downloading the file! " + e.getMessage());
}
}
else {
Expand Down Expand Up @@ -134,8 +136,7 @@ private static void downloadFile(){
// keep main thread from closing the IO for short amt. of time so UI thread can finish and output
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
}
} catch (InterruptedException ignored) {}

} catch (SecurityException e) {
System.out.println("Write access to " + dir + fileName + " denied !");
Expand All @@ -158,4 +159,27 @@ private static void downloadFile(){
dir = dir + System.getProperty("file.separator");
}
}

/**
* This method deals with downloading videos from YouTube in mp4 format.
* @param dirOfYt_dlp The directory of yt-dlp file. Default - "". If Drifty is run from its jar file, this argument will have the directory where yt-dlp has been extracted to.
* @throws InterruptedException When the I/O operation is interrupted using keyboard or such type of inputs.
* @throws IOException When an I/O problem appears while downloading the YouTube video.
*/
private static void downloadFromYouTube(String dirOfYt_dlp) throws InterruptedException, IOException {
System.out.println("Trying to download the file ...");
Drifty_CLI.logger.log("INFO", "Trying to download the file ...");
ProcessBuilder processBuilder = new ProcessBuilder(dirOfYt_dlp + "yt-dlp", "--quiet", "--progress", "-P", dir, link);
processBuilder.inheritIO();
Process yt_dlp = processBuilder.start();
yt_dlp.waitFor();
int exitValueOfYt_Dlp = yt_dlp.exitValue();
if (exitValueOfYt_Dlp == 0){
System.out.println("Successfully downloaded the file!");
Drifty_CLI.logger.log("INFO", "Successfully downloaded the file!");
} else if (exitValueOfYt_Dlp == 1) {
System.out.println("Failed to download the file!");
Drifty_CLI.logger.log("INFO", "Failed to download the file!");
}
}
}
26 changes: 26 additions & 0 deletions src/copyYt_dlp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class copyYt_dlp {
public static final String tempDir = System.getProperty("java.io.tmpdir");
public void copyToTemp() throws IOException{
File file = new File(tempDir + "yt-dlp.exe");
if (file.exists()){
Drifty_CLI.logger.log("INFO", "Skipping copying yt-dlp to " + tempDir + " folder as it is already present!");
return;
}
InputStream is = getClass().getResource("yt-dlp.exe").openStream();
// sets the output stream to a system folder
OutputStream os = new FileOutputStream(System.getProperty("java.io.tmpdir") + "yt-dlp.exe");
byte[] b = new byte[2048]; // length of the byte array doesn't matter in copying the file to the temp folder!
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.close();
}
}

0 comments on commit 216f0c0

Please sign in to comment.