diff options
| -rw-r--r-- | src/main/java/Configuration.java | 83 | ||||
| -rw-r--r-- | src/main/java/DownloadConfigPane.java | 31 | ||||
| -rw-r--r-- | src/main/java/Downloader.java | 2 | ||||
| -rw-r--r-- | src/main/java/FileUtility.java | 40 | ||||
| -rw-r--r-- | src/main/java/Main.java | 87 | ||||
| -rw-r--r-- | src/main/java/TagEditorScreen.java | 13 | ||||
| -rw-r--r-- | src/main/java/UI/Modal.java | 38 |
7 files changed, 190 insertions, 104 deletions
diff --git a/src/main/java/Configuration.java b/src/main/java/Configuration.java new file mode 100644 index 0000000..90b30fd --- /dev/null +++ b/src/main/java/Configuration.java @@ -0,0 +1,83 @@ +import org.json.JSONObject; +import org.json.JSONTokener; + +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.HashMap; + +public class Configuration { + /** + * Reads the configuration json file and returns a hashmap with the kv pairs + * @return HashMap of config data json + */ + public HashMap<String, String> readConfigurationData() { + HashMap<String, String> configData = new HashMap<>(); + File configFile = new File(System.getProperty("user.dir") +"/config/configuration.json"); + try (FileReader reader = new FileReader(configFile)) { + JSONTokener tokener = new JSONTokener(reader); + JSONObject jsonObject = new JSONObject(tokener); + + for (String key : jsonObject.keySet()) { + configData.put(key, jsonObject.getString(key)); + } + } catch (IOException e) { + System.out.println(e); + System.out.println("Failed to read the configuration file."); + } + + return configData; + } + + /** + * Creates a configuration file if one doesn't already exist + * @return true/false if a file was created + */ + public boolean createConfigurationFile(){ + if(new File(System.getProperty("user.dir") +"/config").mkdir()) + System.out.println("Created new config directory"); + File configFile = new File(System.getProperty("user.dir") +"/config/configuration.json"); + if(configFile.exists()) + return false; // File already exists + System.out.println("Creating new Configuration File"); + JSONObject configurationData = new JSONObject(); + configurationData.put("lastFile","" ); + configurationData.put("outputPath", "completed"); + configurationData.put("blacklistFile", ""); + try (FileWriter file = new FileWriter(configFile)) { + file.write(configurationData.toString(4)); + System.out.println("Successfully created a stub config file"); + return true; + } catch (IOException e) { + System.out.println("Failed to create a stub config file"); + return false; + } + } + + public boolean modifyConfigurationValue(String key, String newValue) { + File configFile = new File(System.getProperty("user.dir") +"/config/configuration.json"); + try (FileReader reader = new FileReader(configFile)) { + JSONTokener tokener = new JSONTokener(reader); + JSONObject jsonObject = new JSONObject(tokener); + + if (!jsonObject.has(key)) { + System.out.println("Key does not exist in the configuration."); + return false; + } + + jsonObject.put(key, newValue); + + try (FileWriter writer = new FileWriter(configFile)) { + writer.write(jsonObject.toString(4)); // Indent with 4 spaces for readability + return true; + } catch (IOException e) { + System.out.println("Failed to write to the configuration file."); + return false; + } + } catch (IOException e) { + System.out.println("Failed to read the configuration file."); + return false; + } + } +} diff --git a/src/main/java/DownloadConfigPane.java b/src/main/java/DownloadConfigPane.java index 7206d7e..8de09bb 100644 --- a/src/main/java/DownloadConfigPane.java +++ b/src/main/java/DownloadConfigPane.java @@ -167,35 +167,34 @@ public class DownloadConfigPane extends JFrame{ } private void saveConfigToFile() throws IOException { - if(loadedPath == null){ - File selectedDirectory = selectDirectoryFileChooser(); - if (selectedDirectory == null){ + if (loadedPath == null) { + JFileChooser fileChooser = new JFileChooser(); + fileChooser.setDialogTitle("Specify a file to save"); + int userSelection = fileChooser.showSaveDialog(this); + if (userSelection == JFileChooser.APPROVE_OPTION) { + File fileToSave = fileChooser.getSelectedFile(); + loadedPath = fileToSave.getAbsolutePath(); + if (!loadedPath.endsWith(".txt")) { + loadedPath += ".txt"; + } + } else { return; } - loadedPath = selectedDirectory.getAbsolutePath() + "/download_config.txt"; } + File saveFile = new File(loadedPath); FileWriter writer = new FileWriter(saveFile); - if(!saveFile.exists()){ - System.out.println("File does not exist"); - return; - } for (int i = 0; i < outputTable.getRowCount(); i++) { String url = (String) outputTable.getValueAt(i, 0); String from = (String) outputTable.getValueAt(i, 1); String to = (String) outputTable.getValueAt(i, 2); String line = ""; - if (from.equals("00:00:00") && to.equals("00:00:00")){ + if (from.equals("00:00:00") && to.equals("00:00:00")) { line = url; - } - else{ + } else { line = url + "," + from + "-" + to; } - try{ - writer.write(line + System.lineSeparator()); - } - catch (Exception ignored){ - } + writer.write(line + System.lineSeparator()); System.out.println(line); } writer.close(); diff --git a/src/main/java/Downloader.java b/src/main/java/Downloader.java index cf4178c..828d276 100644 --- a/src/main/java/Downloader.java +++ b/src/main/java/Downloader.java @@ -7,7 +7,6 @@ import org.jaudiotagger.tag.datatype.Artwork; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; -import java.text.SimpleDateFormat; import javax.swing.*; import java.io.File; @@ -15,7 +14,6 @@ import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Arrays; -import java.util.Date; public class Downloader { private final String outputDirectory; diff --git a/src/main/java/FileUtility.java b/src/main/java/FileUtility.java index c29d21b..a964ea8 100644 --- a/src/main/java/FileUtility.java +++ b/src/main/java/FileUtility.java @@ -1,7 +1,5 @@ import org.json.JSONObject;
-import javax.swing.*;
-import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.*;
import java.net.URL;
import java.nio.file.Files;
@@ -24,7 +22,7 @@ public class FileUtility { public static void deleteALlFileOfType(String path, String fileExt){
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
- for(int i=0;i<listOfFiles.length;i++){
+ for(int i = 0; i< Objects.requireNonNull(listOfFiles).length; i++){
if(listOfFiles[i].isFile()){
if(listOfFiles[i].getName().endsWith(fileExt)){
listOfFiles[i].delete();
@@ -69,6 +67,7 @@ public class FileUtility { System.out.println("Searching for file with extension: " + fileExt + " in directory: " + directory);
File dir = new File(directory);
File[] files = dir.listFiles();
+ assert files != null;
for(File file : files){
if(file.getName().endsWith(fileExt)){
System.out.println("Found file: " + file.getName());
@@ -112,40 +111,12 @@ public class FileUtility { }
json = sb.toString();
br.close();
- } catch (Exception e) {
- e.printStackTrace();
+ } catch (Exception ignored) {
}
return json;
}
- public static String showImageFileChooser() {
- javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
- FileNameExtensionFilter filter = new FileNameExtensionFilter("JPEG Image File", "jpg", "jpeg");
- chooser.setFileFilter(filter);
- chooser.setDialogTitle("Select a image file");
- chooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY);
- chooser.setAcceptAllFileFilterUsed(false);
- if (chooser.showOpenDialog(null) == javax.swing.JFileChooser.APPROVE_OPTION) {
- return chooser.getSelectedFile().getAbsolutePath();
- } else {
- return null;
- }
- }
- public static String showDirectoryChooser(){
- try {
- JFileChooser fileChooser = new JFileChooser();
- fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- int result = fileChooser.showOpenDialog(null);
- if (result == JFileChooser.APPROVE_OPTION) {
- return fileChooser.getSelectedFile().getAbsolutePath();
- }
- }
- catch(Exception e){
- JOptionPane.showMessageDialog(null,"An unexpected error has occured");
- }
- return "";
- }
public static ArrayList<String> txtToList(String fileName) {
ArrayList<String> lines = new ArrayList<String>();
@@ -158,7 +129,7 @@ public class FileUtility { }
br.close();
} catch (Exception e) {
- e.printStackTrace();
+ System.out.println("Error reading file: " + fileName);
}
return lines;
}
@@ -169,7 +140,7 @@ public class FileUtility { File folder = new File(path);
File[] listOfFiles = folder.listFiles();
- for(int i=0;i<listOfFiles.length;i++){
+ for(int i = 0; i< Objects.requireNonNull(listOfFiles).length; i++){
if(listOfFiles[i].isFile()){
if(listOfFiles[i].getName().endsWith(".mp3")){
mp3Files.add(listOfFiles[i]);
@@ -178,4 +149,5 @@ public class FileUtility { }
return mp3Files;
}
+
}
diff --git a/src/main/java/Main.java b/src/main/java/Main.java index f01ef3a..73f5c05 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -4,6 +4,7 @@ import java.nio.file.Files; import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.Scanner;
import com.formdev.flatlaf.FlatIntelliJLaf;
@@ -15,8 +16,7 @@ import static UI.Modal.showTextFileChooser; public class Main extends JFrame {
- final static String BLACKLIST = "blacklist.txt";
- private static String COMPLETED_DIR = "completed";
+ private static String COMPLETED_DIR = System.getProperty("user.dir") + "/completed";
String textPath = "";
@@ -30,21 +30,27 @@ public class Main extends JFrame { JButton configureDownloadButton = new JButton("Configure Download File Interactively");
JButton setOutputDirButton = new JButton("Set MP3 Output Directory");
JCheckBox defaultFileBox = new JCheckBox("Use location of last file");
- JCheckBox useBlacklistBox = new JCheckBox("Use Blacklist.txt");
JProgressBar progressBar = new JProgressBar();
- JLabel title = new JLabel("YouTube to MP3 Auto Tagging [CrossPlatform]");
- Boolean useBlacklist = false;
+ JLabel title = new JLabel("YouTube to MP3 Auto Tagging [v1.5]");
Boolean readyState = false;
+ Configuration config = new Configuration();
+ HashMap<String, String> configuration;
public Main() {
initializeComponents();
initializeActionsListeners();
createDirectories();
+ config.createConfigurationFile();
+ configuration = config.readConfigurationData();
}
public static void main(String[] args) {
- FlatIntelliJLaf.setup();
- new Main().setVisible(true);
+ // Launch GUI when no args provided
+ if(args.length == 0) {
+ FlatIntelliJLaf.setup();
+ new Main().setVisible(true);
+ }
+ //TODO: Pass to Command Handler to run job otherwise...
}
@@ -106,7 +112,7 @@ public class Main extends JFrame { startButton.setSize(new Dimension(300, 20));
title.setAlignmentX(Component.CENTER_ALIGNMENT);
defaultFileBox.setAlignmentX(Component.CENTER_ALIGNMENT);
- useBlacklistBox.setAlignmentX(Component.CENTER_ALIGNMENT);
+ setOutputDirButton.setAlignmentX(Component.CENTER_ALIGNMENT);
editButton.setAlignmentX(Component.CENTER_ALIGNMENT);
songsGen.setAlignmentX(Component.CENTER_ALIGNMENT);
progressBar.setStringPainted(true);
@@ -118,7 +124,7 @@ public class Main extends JFrame { panel.add(Box.createVerticalStrut(10));
panel.add(startButton);
panel.add(defaultFileBox);
- panel.add(useBlacklistBox);
+ panel.add(setOutputDirButton);
panel.add(Box.createVerticalStrut(8));
panel.add(scrollPane);
panel.add(Box.createVerticalStrut(5));
@@ -138,10 +144,10 @@ public class Main extends JFrame { */
private void initializeActionsListeners() { //Add all actionlisteners for buttons
defaultFileBox.addActionListener(e -> useLastInputTextFileLocation());
- useBlacklistBox.addActionListener(e -> useBlacklist = useBlacklistBox.isSelected());
startButton.addActionListener(e -> startDownloadTagJobs());
editButton.addActionListener(e -> new TagEditorScreen().setVisible(true));
configureDownloadButton.addActionListener(e -> new DownloadConfigPane().setVisible(true));
+ setOutputDirButton.addActionListener(e -> chooseOutputDirectory());
}
private void writeFileContentsToOutputArea(String path){
@@ -175,33 +181,20 @@ public class Main extends JFrame { */
private void useLastInputTextFileLocation(){
if (defaultFileBox.isSelected()) {
- File file = new File("lastFile.txt");
+ File file = new File(configuration.get("lastFile"));
if (!file.exists()) {
defaultFileBox.setSelected(false);
- JOptionPane.showMessageDialog(null,
- "Unable to find the location of your previous file, please select a new one");
+ UI.Modal.showError("Unable to find the location of your previous input file " +
+ "(Is this your first time using the app?)" +
+ "\nPlease select a new file using the \"Set .txt File\" button");
return;
}
- BufferedReader br;
- try {
- br = new BufferedReader(new FileReader(file));
- String line = br.readLine();
- if (line == null) {
- defaultFileBox.setSelected(false);
- JOptionPane.showMessageDialog(null,
- "Unable to find the location of your previous file, please select a new one");
- return;
- }
- textPath = line;
- COMPLETED_DIR = textPath.substring(0, textPath.lastIndexOf(File.separator));
- readyState = true;
- startButton.setText("Start Download");
- outputArea.setText(outputArea.getText() + "\n" + "Ready to begin downloading. Press the button");
- writeFileContentsToOutputArea(textPath);
- System.out.println("Ready to begin downloading. Press the button");
- } catch (Exception ex) {
- UI.Modal.showError("Unable to read the last file path. Please select a new file instead");
- }
+ textPath = configuration.get("lastFile");
+ readyState = true;
+ startButton.setText("Start Download");
+ outputArea.setText(outputArea.getText() + "\n" + "Ready to begin downloading. Press the button");
+ writeFileContentsToOutputArea(configuration.get("lastFile"));
+ System.out.println("Ready to begin downloading. Press the button");
} else {
readyState = false;
startButton.setText("Set .txt File");
@@ -230,10 +223,12 @@ public class Main extends JFrame { String path = showTextFileChooser();
textPath = path;
if(path == null){
- UI.Modal.showWarning("No file has been selected... How did we get here?");
+ UI.Modal.showWarning("No text file was selected. Aborting operation");
return;
}
- COMPLETED_DIR = path.substring(0, path.lastIndexOf(File.separator));
+ config.modifyConfigurationValue("lastFile", path);
+ configuration = config.readConfigurationData();
+
try {
if (!textPath.isEmpty()) {
UI.Modal.showWarning("File has been set.\nMake sure you add a new line for each URL");
@@ -241,21 +236,13 @@ public class Main extends JFrame { writeFileContentsToOutputArea(textPath);
startButton.setText("Start Download");
outputArea.setText(outputArea.getText() + "\n" + "Ready to begin downloading. Press the button");
- File file = new File("lastFile.txt");
- if (!file.exists()) {
- file.createNewFile();
- }
- FileWriter fw = new FileWriter(file);
- fw.write(textPath);
- fw.close();
-
-
System.out.println("Ready to begin downloading. Press the button");
}
} catch (Exception ex) {
}
} else {
+ outputArea.setText(outputArea.getText() + "\n\n" + "Files will be saved to: " + COMPLETED_DIR);
Runnable runnable = () -> {
outputArea.setText("");
startButton.setEnabled(false);
@@ -268,5 +255,17 @@ public class Main extends JFrame { }
}
+ public void chooseOutputDirectory(){
+ COMPLETED_DIR = UI.Modal.showDirectoryChooser(configuration.get("outputPath"));
+ if (COMPLETED_DIR == null) {
+ outputArea.setText(outputArea.getText() + "\n" + "No directory was selected. No changes were made.");
+ }
+ else{
+ config.modifyConfigurationValue("outputPath", COMPLETED_DIR);
+ configuration = config.readConfigurationData();
+ outputArea.setText(outputArea.getText() + "\n" + "Output directory set to: " + COMPLETED_DIR);
+ }
+ }
+
}
diff --git a/src/main/java/TagEditorScreen.java b/src/main/java/TagEditorScreen.java index 26f2e29..5fe6fd0 100644 --- a/src/main/java/TagEditorScreen.java +++ b/src/main/java/TagEditorScreen.java @@ -127,7 +127,7 @@ public class TagEditorScreen extends JFrame { private void initalizeListeners() { chooseAudioDirectoryButton.addActionListener(e -> { clearSongTable(); - setDirPath = FileUtility.showDirectoryChooser(); + setDirPath = UI.Modal.showDirectoryChooser(); populateSongList(); }); @@ -190,7 +190,7 @@ public class TagEditorScreen extends JFrame { }); imageChooseButton.addActionListener(e -> { try { - selectedAlbumArt = FileUtility.showImageFileChooser(); + selectedAlbumArt = UI.Modal.showImageFileChooser(); if(selectedAlbumArt == null){ return; } @@ -207,14 +207,11 @@ public class TagEditorScreen extends JFrame { } }); - listenButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - System.out.println("CURRENT PATH " + currPath); - playMP3(currPath); + listenButton.addActionListener(e -> { + System.out.println("CURRENT PATH " + currPath); + playMP3(currPath); - } }); } diff --git a/src/main/java/UI/Modal.java b/src/main/java/UI/Modal.java index 5a48aa2..c3fd045 100644 --- a/src/main/java/UI/Modal.java +++ b/src/main/java/UI/Modal.java @@ -17,6 +17,43 @@ public class Modal { return null; } } + public static String showDirectoryChooser() { + JFileChooser chooser = new JFileChooser(); + chooser.setDialogTitle("Select a directory"); + chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); + chooser.setAcceptAllFileFilterUsed(false); + if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { + return chooser.getSelectedFile().getAbsolutePath(); + } else { + return null; + } + } + + public static String showDirectoryChooser(String startDir) { + JFileChooser chooser = new JFileChooser(startDir); + chooser.setDialogTitle("Select a directory"); + chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); + chooser.setAcceptAllFileFilterUsed(false); + if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { + return chooser.getSelectedFile().getAbsolutePath(); + } else { + return null; + } + } + + public static String showImageFileChooser() { + javax.swing.JFileChooser chooser = new javax.swing.JFileChooser(); + FileNameExtensionFilter filter = new FileNameExtensionFilter("JPEG Image File", "jpg", "jpeg"); + chooser.setFileFilter(filter); + chooser.setDialogTitle("Select a image file"); + chooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY); + chooser.setAcceptAllFileFilterUsed(false); + if (chooser.showOpenDialog(null) == javax.swing.JFileChooser.APPROVE_OPTION) { + return chooser.getSelectedFile().getAbsolutePath(); + } else { + return null; + } + } /** * Show warning message @@ -31,4 +68,5 @@ public class Modal { public static void showError(String message) { JOptionPane.showMessageDialog(null, message, "ERROR", JOptionPane.ERROR_MESSAGE); } + } |
