From de338a05e5fbbb3c707a69335677ca805f1a042f Mon Sep 17 00:00:00 2001 From: Donald Shan Date: Sun, 21 Aug 2022 16:11:18 -0700 Subject: Organized Code --- src/main/java/FileUtility.java | 134 +++++++++++++++++ src/main/java/Main.java | 192 ++++-------------------- src/main/java/org/example/Main.java | 282 ------------------------------------ 3 files changed, 165 insertions(+), 443 deletions(-) create mode 100644 src/main/java/FileUtility.java delete mode 100644 src/main/java/org/example/Main.java (limited to 'src/main') diff --git a/src/main/java/FileUtility.java b/src/main/java/FileUtility.java new file mode 100644 index 0000000..7f35c30 --- /dev/null +++ b/src/main/java/FileUtility.java @@ -0,0 +1,134 @@ +import javax.swing.filechooser.FileNameExtensionFilter; +import java.io.*; +import java.net.URL; +import java.util.ArrayList; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class FileUtility { + public void deleteFile(String fileName) { + File file = new File(fileName); + if (file.exists()) { + file.delete(); + } + } + + public void downloadImage(String url, String fileName) throws IOException { + FileOutputStream fos = new FileOutputStream(fileName); + URL urlObj = new URL(url); + InputStream is = urlObj.openStream(); + byte[] b = new byte[2048]; + int length; + while ((length = is.read(b)) != -1) { + fos.write(b, 0, length); + } + fos.close(); + is.close(); + } + public void moveFile(String source, String destination) { + File sourceFile = new File(source); + File destinationFile = new File(destination); + sourceFile.renameTo(destinationFile); + System.out.println("Moved file to Completed Folder"); + } + public String removeNonAlphaNumeric(String str) { + return str.replaceAll("[^a-zA-Z0-9]", ""); + } + public static String showTextFileChooser() { + javax.swing.JFileChooser chooser = new javax.swing.JFileChooser(); + FileNameExtensionFilter filter = new FileNameExtensionFilter("Text File", "txt", "text"); + chooser.setFileFilter(filter); + chooser.setDialogTitle("Select a text 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 File findMP3File(String directory){ + File dir = new File(directory); + File[] files = dir.listFiles(); + for(File file : files){ + if(file.getName().endsWith(".mp3")){ + return file; + } + } + return null; + } + + public static String findJsonFile(String folderName) { + File folder = new File(folderName); + File[] listOfFiles = folder.listFiles(); + for (int i = 0; i < listOfFiles.length; i++) { + if (listOfFiles[i].isFile()) { + if (listOfFiles[i].getName().endsWith(".json")) { + return listOfFiles[i].getAbsolutePath(); + } + } + } + return null; + } + public static String[] parseJson(String json) { + String title = ""; + String uploader = ""; + String id = ""; + Pattern titlePattern = Pattern.compile("\"fulltitle\": \"(.*?)\","); + Matcher titleMatcher = titlePattern.matcher(json); + Pattern uploaderPattern = Pattern.compile("\"uploader\": \"(.*?)\","); + Matcher uploaderMatcher = uploaderPattern.matcher(json); + Pattern idPattern = Pattern.compile("\"id\": \"(.*?)\","); + Matcher idMatcher = idPattern.matcher(json); + titleMatcher.find(); + idMatcher.find(); + uploaderMatcher.find(); + title = titleMatcher.group(1); + uploader = uploaderMatcher.group(1); + id = idMatcher.group(1); + String[] info = {title,uploader,id}; + return info; + + } + public static String jsonToString(String fileName) { + String json = ""; + try { + BufferedReader br = new BufferedReader(new FileReader(fileName)); + StringBuilder sb = new StringBuilder(); + String line = br.readLine(); + while (line != null) { + sb.append(line); + line = br.readLine(); + } + json = sb.toString(); + br.close(); + } catch (Exception e) { + e.printStackTrace(); + } + return json; + } + public static void deleteAllFilesDir(String path) { + File folder = new File(path); + File[] files = folder.listFiles(); + if (files != null) { + for (File f : files) { + f.delete(); + } + } + } + public static ArrayList txtToArrayList(String fileName) { + ArrayList lines = new ArrayList(); + try { + FileReader fr = new FileReader(fileName); + BufferedReader br = new BufferedReader(fr); + String line; + while ((line = br.readLine()) != null) { + lines.add(line); + } + br.close(); + } catch (Exception e) { + e.printStackTrace(); + } + return lines; + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a1fc151..ec95ad9 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -2,19 +2,14 @@ import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; -import java.net.URL; import java.util.ArrayList; -import java.util.regex.Matcher; import org.jaudiotagger.audio.AudioFile; import org.jaudiotagger.audio.AudioFileIO; import org.jaudiotagger.tag.FieldKey; import org.jaudiotagger.tag.Tag; import org.jaudiotagger.tag.datatype.Artwork; - import javax.swing.*; -import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.text.DefaultCaret; -import java.util.regex.Pattern; public class Main extends JFrame { String textPath = ""; @@ -23,11 +18,22 @@ public class Main extends JFrame { JScrollPane scrollPane; JButton editButton = new JButton("Edit Tags"); int progress = 0; + FileUtility fileUtil = new FileUtility(); JProgressBar progressBar = new JProgressBar(); JLabel title = new JLabel("SUPER JUICER DOWNLOAD MUSIC COVERS AND TAG NOW 100% SAFE"); JButton startButton = new JButton("Set .txt File"); static JTextArea outputArea = new JTextArea("this is bery bery bery safe no worries no virus malwar ur monies back granteed"); + public Main(){ + initializeComponents(); + initializeActionsListeners(); + } + + public static void main(String[] args) { + new Main().setVisible(true); + } + + private void initializeComponents(){ this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.add(panel); @@ -54,13 +60,17 @@ public class Main extends JFrame { panel.add(scrollPane); panel.add(Box.createVerticalStrut(5)); panel.add(editButton); + this.setSize(550,300); + } + + private void initializeActionsListeners(){ startButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(textPath.equals("")||readyState==false){ outputArea.setText(outputArea.getText()+"\n"+"txt path has not been set. Launching chooserPane"); System.out.println(".txt path has not been set. Launching chooserPane"); - textPath = showFileChooser(); + textPath = fileUtil.showTextFileChooser(); if(!textPath.equals("")){ showWarning("File has been set.\nMake sure you add a new line for each URL.\nOr else say bye bye to your system32"); readyState = true; @@ -73,7 +83,7 @@ public class Main extends JFrame { Runnable runnable = () -> { outputArea.setText(""); startButton.setEnabled(false); - downloadLoop(); + downloadAndTag(); startButton.setEnabled(true); }; @@ -88,32 +98,29 @@ public class Main extends JFrame { } }); - this.setSize(550,300); - - } - public static void main(String[] args) { - new Main().setVisible(true); } - private void downloadLoop(){ - ArrayList songs = readFile(textPath); + + private void downloadAndTag(){ + ArrayList songs = fileUtil.txtToArrayList(textPath); progress = 0; for(int i = 0;i readFile(String fileName) { - ArrayList lines = new ArrayList(); - try { - FileReader fr = new FileReader(fileName); - BufferedReader br = new BufferedReader(fr); - String line; - while ((line = br.readLine()) != null) { - lines.add(line); - } - br.close(); - } catch (Exception e) { - e.printStackTrace(); - } - return lines; - } + diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java deleted file mode 100644 index ba75319..0000000 --- a/src/main/java/org/example/Main.java +++ /dev/null @@ -1,282 +0,0 @@ -import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.io.*; -import java.net.URL; -import java.util.ArrayList; -import java.util.regex.Matcher; -import org.jaudiotagger.audio.AudioFile; -import org.jaudiotagger.audio.AudioFileIO; -import org.jaudiotagger.tag.FieldKey; -import org.jaudiotagger.tag.Tag; -import org.jaudiotagger.tag.datatype.Artwork; - -import javax.swing.*; -import javax.swing.filechooser.FileNameExtensionFilter; -import javax.swing.text.DefaultCaret; -import java.util.regex.Pattern; - -public class Main extends JFrame { - String textPath = ""; - JPanel panel = new JPanel(); - Boolean readyState = false; - JScrollPane scrollPane; - int progress = 0; - JProgressBar progressBar = new JProgressBar(); - JLabel title = new JLabel("SUPER JUICER DOWNLOAD MUSIC COVERS AND TAG NOW 100% SAFE"); - JButton startButton = new JButton("Set .txt File"); - static JTextArea outputArea = new JTextArea("this is bery bery bery safe no worries no virus malwar ur monies back granteed"); - public Main(){ - this.setDefaultCloseOperation(EXIT_ON_CLOSE); - this.setLocationRelativeTo(null); - this.add(panel); - panel.setLayout(new BoxLayout(panel,BoxLayout.PAGE_AXIS)); - scrollPane = new JScrollPane(outputArea); - scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); - outputArea.setEditable(false); - outputArea.setLineWrap(true); - DefaultCaret caret = (DefaultCaret)outputArea.getCaret(); - caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); - panel.add(Box.createRigidArea(new Dimension(0,5))); - panel.setBorder(BorderFactory.createEmptyBorder(25,10,20,10)); - startButton.setAlignmentX(CENTER_ALIGNMENT); - title.setAlignmentX(Component.CENTER_ALIGNMENT); - progressBar.setStringPainted(true); - title.setFont(new Font("Verdana", Font.PLAIN, 14)); - panel.add(title); - panel.add(Box.createVerticalStrut(10)); - panel.add(progressBar); - panel.add(Box.createVerticalStrut(10)); - panel.add(startButton); - panel.add(Box.createVerticalStrut(8)); - panel.add(scrollPane); - startButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - if(textPath.equals("")||readyState==false){ - outputArea.setText(outputArea.getText()+"\n"+"txt path has not been set. Launching chooserPane"); - System.out.println(".txt path has not been set. Launching chooserPane"); - textPath = showFileChooser(); - if(!textPath.equals("")){ - showWarning("File has been set.\nMake sure you add a new line for each URL.\nOr else say bye bye to your system32"); - readyState = true; - startButton.setText("Start Download"); - outputArea.setText(outputArea.getText()+"\n"+"Ready to begin downloading. Press the button"); - System.out.println("Ready to begin downloading. Press the button"); - } - } - else{ - Runnable runnable = () -> { - outputArea.setText(""); - startButton.setEnabled(false); - downloadLoop(); - startButton.setEnabled(true); - - }; - Thread thread = new Thread(runnable); - thread.start(); - } - } - }); - this.setSize(550,300); - - } - public static void main(String[] args) { - new Main().setVisible(true); - } - private void downloadLoop(){ - ArrayList songs = readFile(textPath); - progress = 0; - for(int i = 0;i readFile(String fileName) { - ArrayList lines = new ArrayList(); - try { - FileReader fr = new FileReader(fileName); - BufferedReader br = new BufferedReader(fr); - String line; - while ((line = br.readLine()) != null) { - lines.add(line); - } - br.close(); - } catch (Exception e) { - e.printStackTrace(); - } - return lines; - } - -} -- cgit v1.2.3