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 38 commits
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
224 changes: 212 additions & 12 deletions CLI/src/main/java/main/Drifty_CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import cli.utils.MessageBroker;
import cli.init.Environment;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.error.YAMLException;
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
import preferences.AppSettings;
import properties.MessageType;
import properties.Program;
import properties.OS;
import support.Job;
import support.JobHistory;
Expand All @@ -17,6 +19,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -40,6 +43,8 @@
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 YAML_FILENAME = "links.yml";
private static String yamlFilePath;

public static void main(String[] args) {
LOGGER.log(MessageType.INFO, CLI_APPLICATION_STARTED);
Expand All @@ -53,20 +58,44 @@
printBanner();
if (args.length > 0) {
link = null;
String name = null;
String location = null;
yamlFilePath = Paths.get(Program.get(Program.DRIFTY_PATH), YAML_FILENAME).toAbsolutePath().toString();
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 0; i < args.length; i++) {
switch (args[i]) {
case HELP_FLAG, HELP_FLAG_SHORT -> {
help();
System.exit(0);
case HELP_FLAG, HELP_FLAG_SHORT -> help();
case ADD_FLAG -> {
if (i + 1 >= args.length) {
messageBroker.msgBatchError("No URL provided.");
System.exit(1);
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
}
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
for (int j = i + 1; j < args.length; j++) {
addUrlToFile(args[j]);
}
}
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 -> {
printVersion();
System.exit(0);
case LIST_FLAG -> listUrls();
case REMOVE_FLAG -> {
if (i + 1 >= args.length) {
messageBroker.msgBatchError("No line number provided for removal.");
System.exit(1);
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
}
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
for (int j = i + 1; j < args.length; j++) {
if ("all".equalsIgnoreCase(args[j])) {
removeAllUrls();
} else {
removeUrl(args[j]);
}
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
}
}
case GET_FLAG -> {
batchDownloading = true;
// Logic to download URLs goes here
System.out.println("Downloading URLs...");
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
batchDownloadingFile = yamlFilePath;
batchDownloader();
removeAllUrls();
}
case NAME_FLAG, NAME_FLAG_SHORT -> fileName = args[i + 1];
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved
case LOCATION_FLAG, LOCATION_FLAG_SHORT -> downloadsFolder = args[i + 1];
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved
case VERSION_FLAG, VERSION_FLAG_SHORT -> printVersion();
case BATCH_FLAG, BATCH_FLAG_SHORT -> {
batchDownloading = true;
batchDownloadingFile = args[i + 1];
Expand All @@ -75,12 +104,14 @@
default -> {
if (isURL(args[i])) {
link = args[i];
continue;
} else {
messageBroker.msgInitError("Invalid argument(s) passed!");
System.exit(1);
}
}
}
System.exit(0);
}
if (!batchDownloading) {
boolean isUrlValid;
Expand All @@ -94,9 +125,8 @@
isYoutubeURL = isYoutube(link);
isInstagramLink = isInstagram(link);
isSpotifyLink = isSpotify(link);
downloadsFolder = location;
downloadsFolder = getProperDownloadsFolder(downloadsFolder);
if ((name == null) && (fileName == null || fileName.isEmpty())) {
if (fileName == null || fileName.isEmpty()) {
if (isSpotifyLink && link.contains("playlist")) {
handleSpotifyPlaylist();
} else {
Expand Down Expand Up @@ -189,6 +219,172 @@
}
}

private static void ensureYamlFileExists() {
// Check if the YAML file exists, create it if it does not
File yamlFile = new File(yamlFilePath);
if (!yamlFile.exists()) {
try {
boolean isNewFileCreated = yamlFile.createNewFile();
if (isNewFileCreated) {
messageBroker.msgFilenameInfo("New YAML file created: " + yamlFilePath);
Femi-lawal marked this conversation as resolved.
Show resolved Hide resolved
} else {
messageBroker.msgLogError("Failed to create new YAML file.");
}
} catch (IOException e) {
messageBroker.msgLogError("Error while creating YAML file: " + e.getMessage());
}
}
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
}

private static boolean isEmptyYaml(Map<String, List<String>> data) {
if (data == null || !data.containsKey("links") || data.get("links").isEmpty()) {
messageBroker.msgLinkError("No URL is present in the links queue!\n" +
"Please run with `add <link>` to add the link to the list.");
return true;
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
}
return false;
}

private static Map<String, List<String>> loadYamlData() {
Yaml yaml = new Yaml();
Map<String, List<String>> data = null;
ensureYamlFileExists(); // Ensure the YAML file exists before trying to read

try (InputStreamReader reader = new InputStreamReader(new FileInputStream(yamlFilePath))) {
data = yaml.load(reader);
if (data == null) {
data = new HashMap<>();
}
data.computeIfAbsent("links", k -> new ArrayList<>()); // Ensure 'links' list exists
} catch (YAMLException e) {
messageBroker.msgLogError("Invalid YAML format in file: " + e.getMessage());
} catch (IOException e) {
messageBroker.msgLogError("Error reading YAML file: " + e.getMessage());
}
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
return data;
}

private static void saveYamlData(Map<String, List<String>> data) {
Yaml yaml = new Yaml();
File originalFile = new File(yamlFilePath);
File backupFile = new File(yamlFilePath + ".bak");

// Create a backup of the original file
try {
Files.copy(originalFile.toPath(), backupFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
messageBroker.msgLogError("Failed to create a backup of the YAML file: " + e.getMessage());
return; // Abort the operation if backup fails to ensure data integrity
}

// Proceed with writing the updated data to the YAML file
try (BufferedWriter writer = new BufferedWriter(new FileWriter(originalFile))) {
yaml.dump(data, writer);
} catch (IOException e) {
messageBroker.msgLogError("Error writing to YAML file: " + e.getMessage());
// Attempt to restore from backup in case of a write error
try {
Files.copy(backupFile.toPath(), originalFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
messageBroker.msgLogError("Restored the original YAML file from backup due to write error.");
} catch (IOException restoreException) {
messageBroker.msgLogError("Failed to restore the original YAML file from backup: " + restoreException.getMessage());
}
} finally {
// Clean up: Delete the backup file if everything went smoothly
if (!backupFile.delete()) {
messageBroker.msgLogError("Failed to delete the backup file: " + backupFile.getName());
}
}
}

private static void listUrls() {
try {
Map<String, List<String>> data = loadYamlData();
if (isEmptyYaml(data)) {
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
return;
}

List<String> urls = data.get("links");
messageBroker.msgDownloadInfo("List of URLs:");
urls.forEach(url -> messageBroker.msgLinkInfo(url));
} catch (Exception e) {
messageBroker.msgLogError("An error occurred while listing URLs: " + e.getMessage());
}
}

private static void removeUrl(String indexStr) {
int index;
try {
index = Integer.parseInt(indexStr);
} catch (NumberFormatException e) {
messageBroker.msgInputError("Invalid format. Please provide a numeric input.", true);
return;
}

try {
Map<String, List<String>> data = loadYamlData();
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
if (isEmptyYaml(data)) {
return;
}

List<String> urls = data.get("links");
if (index < 1 || index > urls.size()) {
messageBroker.msgInputError("Invalid line number '" + index + "'. Please provide a number between 1 and " + urls.size() + ".", true);
return;
}

String removedUrl = urls.remove(index - 1);
saveYamlData(data); // Save updated YAML data
messageBroker.msgLinkInfo("Removed URL: " + removedUrl);
} catch (Exception e) {
messageBroker.msgLogError("An error occurred while removing a URL: " + e.getMessage());
}
}

private static void removeAllUrls() {
try {
// Delete the YAML file to remove all URLs
File yamlFile = new File(yamlFilePath);
if (yamlFile.exists()) {
if (yamlFile.delete()) {
if (batchDownloading) {
messageBroker.msgLogInfo("All URLs removed successfully from the YAML file.");
} else {
messageBroker.msgLinkInfo("All URLs removed successfully.");
}
} else {
messageBroker.msgLinkError("Failed to remove all URLs.");
}
} else {
messageBroker.msgLinkError("No URLs to remove.");
}
} catch (Exception e) {
messageBroker.msgLogError("An error occurred while removing all URLs: " + e.getMessage());
}
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
}

private static void addUrlToFile(String urlString) {
if (!Utility.isURL(urlString)) {
messageBroker.msgInputError("Error: '" + urlString + "' is not a valid URL.", true);
return;
}

try {
Map<String, List<String>> data = loadYamlData();

List<String> urls = data.get("links");
if (!urls.contains(urlString)) {
urls.add(urlString); // Add the URL if it doesn't exist
saveYamlData(data); // Save the updated data back to the YAML file
messageBroker.msgLinkInfo("URL added: " + urlString);
} else {
messageBroker.msgInputError("URL already exists: " + urlString, true);
}
} catch (Exception e) {
messageBroker.msgLogError("An error occurred while adding a URL: " + 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 +601,10 @@
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> <URL> ... Add URLs Adds new URLs to the download queue. Multiple URLs can be added by separating them with spaces.");
System.out.println("remove <number(s)/all> Remove URLs Removes one or more URLs from the download queue. Specify line numbers separated by spaces or use 'all' to remove all URLs.");
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
Loading