Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add command line URL queue #414

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
7801a02
feat: add command line URL queue
Femi-lawal Jan 15, 2024
4f64a99
Merge branch 'master' into master
SaptarshiSarkar12 Jan 15, 2024
cfb6117
Merge branch 'master' into master
SaptarshiSarkar12 Jan 18, 2024
11685dd
Merge branch 'master' into master
SaptarshiSarkar12 Jan 18, 2024
ccafad7
Merge branch 'master' into master
SaptarshiSarkar12 Jan 20, 2024
4f95d6c
Merge branch 'master' into master
SaptarshiSarkar12 Jan 20, 2024
ca952bd
Merge branch 'master' into master
SaptarshiSarkar12 Jan 20, 2024
c10082c
Merge branch 'master' into master
SaptarshiSarkar12 Jan 20, 2024
ac5bd97
removed accidental increments
Femi-lawal Jan 20, 2024
998f176
update code to use yaml file and existing batchDownload function
Femi-lawal Jan 20, 2024
db10607
Merge branch 'master' of github.com:SaptarshiSarkar12/Drifty into Fem…
SaptarshiSarkar12 Jan 25, 2024
98b2773
fix parsing of yaml directory, clean up add and remove flags
Femi-lawal Jan 26, 2024
cc33c7a
Merge branch 'master' of https://github.com/Femi-lawal/Drifty
Femi-lawal Jan 26, 2024
fc9710a
fixed linting error
Femi-lawal Jan 26, 2024
02c0026
implement functionality to add multiple urls and remove all urls
Femi-lawal Jan 28, 2024
96a2de3
update error message
Femi-lawal Jan 28, 2024
550f581
Merge branch 'master' into master
SaptarshiSarkar12 Feb 13, 2024
8b56335
Merge branch 'master' into master
SaptarshiSarkar12 Feb 16, 2024
c2f7433
style: Formatted Java files
github-actions[bot] Feb 16, 2024
d3ef696
Merge branch 'master' into master
SaptarshiSarkar12 Feb 16, 2024
bd622d4
Merge branch 'SaptarshiSarkar12:master' into master
Femi-lawal Feb 23, 2024
6bad18b
feat: add command line URL queue
Femi-lawal Jan 15, 2024
78b35dd
removed accidental increments
Femi-lawal Jan 20, 2024
df634f0
update code to use yaml file and existing batchDownload function
Femi-lawal Jan 20, 2024
77c0239
fix parsing of yaml directory, clean up add and remove flags
Femi-lawal Jan 26, 2024
e7e2a2b
fixed linting error
Femi-lawal Jan 26, 2024
3366e0c
implement functionality to add multiple urls and remove all urls
Femi-lawal Jan 28, 2024
1d3be13
update error message
Femi-lawal Jan 28, 2024
81831dc
style: Formatted Java files
github-actions[bot] Feb 16, 2024
9ae1cdc
Merge branch 'master' of https://github.com/Femi-lawal/Drifty
Femi-lawal Feb 23, 2024
1c9b85d
extract repeated code into helper functions
Femi-lawal Feb 23, 2024
c095ad9
style: Formatted Java files
github-actions[bot] Feb 23, 2024
59f8388
fixed linting issues
Femi-lawal Feb 23, 2024
e03f5e2
Merge branch 'master' of https://github.com/Femi-lawal/Drifty
Femi-lawal Feb 23, 2024
b9d8a0a
add yaml file backup
Femi-lawal Feb 23, 2024
68dcaf1
Merge branch 'master' of https://github.com/Femi-lawal/Drifty
Femi-lawal Feb 23, 2024
fafb0af
add invalid YAML format exception for loadYaml
Femi-lawal Feb 24, 2024
b55fc7a
feat: Added code to delete yml file for removing all URLs
SaptarshiSarkar12 Feb 25, 2024
94b1e26
Merge branch 'master' into master
SaptarshiSarkar12 Feb 25, 2024
124ee88
Update CLI/src/main/java/main/Drifty_CLI.java
Femi-lawal Feb 28, 2024
e6835a8
add normaliseUrl method
Femi-lawal Mar 3, 2024
c499ba3
resolve merge conflict
Femi-lawal Mar 4, 2024
c66cb0b
Merge branch 'master' of https://github.com/SaptarshiSarkar12/Drifty
Femi-lawal Mar 4, 2024
f9401a6
style: Formatted Java files
github-actions[bot] Mar 4, 2024
138b519
use safeloader for yaml
Femi-lawal Mar 4, 2024
152589a
use safeloader for yaml
Femi-lawal Mar 4, 2024
9a183a8
use safeloader for yaml
Femi-lawal Mar 4, 2024
72a78d7
Update CLI/src/main/java/main/Drifty_CLI.java
Femi-lawal Mar 11, 2024
1fe8f1d
Update CLI/src/main/java/main/Drifty_CLI.java
Femi-lawal Mar 11, 2024
b86604d
fix omission of get flag
Femi-lawal Mar 11, 2024
0f3e9f2
remove port removal
Femi-lawal Mar 11, 2024
a24c45e
Merge branch 'master' into chore/resolve-conflicts
Femi-lawal Mar 11, 2024
afd8de8
added exit code 1 for errors
Femi-lawal Mar 16, 2024
7394139
added confirmation message for remove all
Femi-lawal Mar 16, 2024
1f43343
Merge branch 'master' into chore/resolve-conflicts
Femi-lawal Mar 16, 2024
f366ac9
Merge branch 'master' into chore/resolve-conflicts
Femi-lawal Apr 21, 2024
251f900
Merge branch 'master' into chore/resolve-conflicts
SaptarshiSarkar12 Apr 27, 2024
d81c90d
Merge remote-tracking branch 'origin/master' into chore/resolve-confl…
SaptarshiSarkar12 May 4, 2024
f17ac92
fix: Fixed minor issues of logging and output statements
SaptarshiSarkar12 May 4, 2024
3fb9f73
fix: Fixed all linter and AI refactor suggestions
SaptarshiSarkar12 May 6, 2024
ba045a6
Merge branch 'master' of github.com:SaptarshiSarkar12/Drifty into cho…
SaptarshiSarkar12 May 6, 2024
80b376e
feat: Changed the add, get, list and remove flags to proper flag form…
SaptarshiSarkar12 May 6, 2024
c9c5bad
fix: Refactored the code block which sets links queue file path to ru…
SaptarshiSarkar12 May 6, 2024
50ee655
fix: Fixed Coderabbitai suggestions
SaptarshiSarkar12 May 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CLI/src/main/java/cli/support/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ public class Constants extends support.Constants {
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String CLI_APPLICATION_STARTED = "Drifty CLI (Command Line Interface) Application Started !";
public static final String CLI_APPLICATION_TERMINATED = "Drifty CLI (Command Line Interface) Application Terminated!";
public static final String ADD_FLAG = "add";
public static final String REMOVE_FLAG = "remove";
public static final String LIST_FLAG = "list";
public static final String GET_FLAG = "get";
public static final String HELP_FLAG = "--help";
public static final String NAME_FLAG = "--name";
public static final String VERSION_FLAG = "--version";
Expand Down
175 changes: 175 additions & 0 deletions CLI/src/main/java/main/Drifty_CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class Drifty_CLI {
private static String batchDownloadingFile;
private static final String MSG_FILE_EXISTS_NO_HISTORY = "\"%s\" exists in \"%s\" folder. It will be renamed to \"%s\".";
private static final String MSG_FILE_EXISTS_HAS_HISTORY = "You have previously downloaded \"%s\" and it exists in \"%s\" folder.\nDo you want to download it again? ";
private static final String URL_LIST_FILE = "drifty_urls.txt";
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved

public static void main(String[] args) {
LOGGER.log(MessageType.INFO, CLI_APPLICATION_STARTED);
Expand All @@ -61,6 +62,53 @@ public static void main(String[] args) {
help();
System.exit(0);
}
case ADD_FLAG -> {
if (i + 1 < args.length && Utility.isURL(args[i + 1])) {
boolean isUrlValid;
if (Utility.isURL(args[i + 1])) {
isUrlValid = Utility.isLinkValid(args[i + 1]);
} else {
isUrlValid = false;
messageBroker.msgLinkError(INVALID_LINK);
}
if (!isUrlValid) {
LOGGER.log(MessageType.INFO, CLI_APPLICATION_TERMINATED);
System.exit(0);
}
addUrlToFile(args[i + 1]);
System.out.println("URL added: " + args[i + 1]);
i++;
listUrls();
} else {
messageBroker.msgInitError("No URL provided or invalid URL format.");
}
System.exit(0);
}
case LIST_FLAG -> {
listUrls();
System.exit(0);
}
case REMOVE_FLAG -> {
if (i + 1 < args.length) {
try {
int index = Integer.parseInt(args[i + 1]);
removeUrl(index);
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
i++;
listUrls();
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
} catch (NumberFormatException e) {
messageBroker.msgInitError("Invalid format. Please provide a numeric input.");
}
} else {
messageBroker.msgInitError("No line number provided for removal.");
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
}
System.exit(0);
}
case GET_FLAG -> {
// Logic to download URLs goes here
System.out.println("Downloading URLs...");
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
batchTextDownloader();
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
System.exit(0);
}
case NAME_FLAG, NAME_FLAG_SHORT -> name = args[i + 1];
case LOCATION_FLAG, LOCATION_FLAG_SHORT -> location = args[i + 1];
case VERSION_FLAG, VERSION_FLAG_SHORT -> {
Expand Down Expand Up @@ -189,6 +237,129 @@ public static void main(String[] args) {
}
}

private static void listUrls() {
Path path = Paths.get(URL_LIST_FILE);
if (Files.exists(path)) {
try {
List<String> lines = Files.readAllLines(path);
if (lines.isEmpty()) {
System.out.println("No URLs found in the list.");
} else {
System.out.println("List of URLs:");
for (int i = 0; i < lines.size(); i++) {
System.out.println((i + 1) + ". " + lines.get(i));
}
}
} catch (IOException e) {
messageBroker.msgInitError("Error reading file: " + e.getMessage());
}
} else {
System.out.println("URL list file does not exist.");
}
}

private static void removeUrl(int index) {
Path path = Paths.get(URL_LIST_FILE);
if (!Files.exists(path)) {
System.out.println("URL list file does not exist.");
return;
}

try {
List<String> lines = new ArrayList<>(Files.readAllLines(path));
if (index < 1 || index > lines.size()) {
System.out.println("Line number not found. Please provide a valid line number.");
} else {
// Store the URL before removing it
String removedUrl = lines.get(index - 1);
// Remove the URL
lines.remove(index - 1);
// Update the file
Files.write(path, lines);
// Print the removed URL
System.out.println("Removed URL at line number " + index + ": " + removedUrl);
}
} catch (IOException e) {
messageBroker.msgInitError("Error updating file: " + e.getMessage());
}
}

private static void addUrlToFile(String url) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(URL_LIST_FILE, true))) {
writer.write(url);
writer.newLine();
} catch (IOException e) {
messageBroker.msgInitError("Error writing URL to file: " + e.getMessage());
}
}

private static void batchTextDownloader() {
Path path = Paths.get(URL_LIST_FILE);
if (!Files.exists(path)) {
messageBroker.msgDownloadError("Data file (" + URL_LIST_FILE + ") not found!");
return;
}

List<String> urls;
try {
urls = Files.readAllLines(path);
} catch (IOException e) {
messageBroker.msgDownloadError("Error reading from file: " + e.getMessage());
return;
}
try {
if (urls.isEmpty()) {
messageBroker.msgLinkInfo("No links specified. Exiting...");
return;
}

messageBroker.msgBatchInfo("Processing " + urls.size() + " link(s)");
messageBroker.msgInputInfo("Download directory (\".\" for default or \"L\" for " + AppSettings.GET.lastDownloadFolder() + ") : ", false);
downloadsFolder = SC.next();
downloadsFolder = getProperDownloadsFolder(downloadsFolder);

for (int i = 0; i < urls.size(); i++) {
String url = urls.get(i);
messageBroker.msgStyleInfo(BANNER_BORDER);
messageBroker.msgLinkInfo("[" + (i + 1) + "/" + urls.size() + "] Processing link: " + url);

// Setting global 'link' variable, assuming other methods depend on it
link = url;
isYoutubeURL = isYoutube(link);
isInstagramLink = isInstagram(link);
isSpotifyLink = isSpotify(link);
if (".".equals(downloadsFolder)) {
downloadsFolder = Utility.getHomeDownloadFolder();
} else if ("L".equalsIgnoreCase(downloadsFolder)) {
downloadsFolder = AppSettings.GET.lastDownloadFolder();
}

if (isSpotifyLink && link.contains("playlist")) {
fileName = null;
} else {
if (isInstagram(link) && !link.contains("?utm_source=ig_embed")) {
if (link.contains("?")) {
link = link.substring(0, link.indexOf("?")) + "?utm_source=ig_embed";
} else {
link = link + "?utm_source=ig_embed";
}
}
messageBroker.msgFilenameInfo("Retrieving filename from link...");
fileName = findFilenameInLink(link);

}

if (isSpotifyLink && link.contains("playlist")) {
handleSpotifyPlaylist();
}
Job job = new Job(link, downloadsFolder, fileName, false);
checkHistoryAndDownload(job, false);
}
} catch (Exception e) {
messageBroker.msgDownloadError("Error reading from URL list file (" + URL_LIST_FILE + "): " + e.getMessage());
}
}

private static void printVersion() {
System.out.println("\033[1m" + APPLICATION_NAME + " " + VERSION_NUMBER + ANSI_RESET);
if (AppSettings.GET.ytDlpVersion().isEmpty()) {
Expand Down Expand Up @@ -405,6 +576,10 @@ public static void help() {
System.out.println("--name -n Source Filename of the downloaded file");
System.out.println("--help -h N/A Prints this help menu");
System.out.println("--version -v Current Version Displays version number of Drifty");
System.out.println("add [URL] Add URL Adds a new URL to the download queue");
System.out.println("remove [line number] Remove URL Removes a URL from the download queue at the specified line number");
System.out.println("list List URLs Lists all URLs currently in the download queue");
System.out.println("get Download URLs Downloads all URLs in the download queue");
System.out.println("\033[97;1mSee full documentation at https://github.com/SaptarshiSarkar12/Drifty#readme" + ANSI_RESET);
System.out.println("For more information visit: ");
System.out.println("\tProject Link - https://github.com/SaptarshiSarkar12/Drifty/");
Expand Down