From 367f2b93de72b6cc2ae977183f8750d47679493a Mon Sep 17 00:00:00 2001
From: Nekojimi <Jim@nekojimi.moe>
Date: Fri, 24 Sep 2021 20:22:17 +0100
Subject: [PATCH] Add Song class for storing details about queue items.

---
 src/main/java/moe/nekojimi/chords/Main.java   | 25 +++++--
 .../moe/nekojimi/chords/MusicHandler.java     | 14 ++--
 src/main/java/moe/nekojimi/chords/Song.java   | 67 +++++++++++++++++++
 3 files changed, 92 insertions(+), 14 deletions(-)
 create mode 100644 src/main/java/moe/nekojimi/chords/Song.java

diff --git a/src/main/java/moe/nekojimi/chords/Main.java b/src/main/java/moe/nekojimi/chords/Main.java
index f167da5..da95c82 100644
--- a/src/main/java/moe/nekojimi/chords/Main.java
+++ b/src/main/java/moe/nekojimi/chords/Main.java
@@ -9,6 +9,8 @@ import java.io.Closeable;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.nio.ByteBuffer;
 import java.nio.charset.Charset;
 import java.util.*;
@@ -150,11 +152,18 @@ public class Main extends ListenerAdapter
     {
         try
         {
+            Song song = new Song(new URL(arg));
             event.getChannel().sendMessage("Downloading ...").queue();
-            String destination = downloadSong(arg);
-            musicHandler.addSong(new File(destination));
-            event.getChannel().sendMessage("Downloaded and added to queue!").queue();
+            boolean ok = downloadSong(song);
+            if (ok)
+            {
+                musicHandler.addSong(song);
+                event.getChannel().sendMessage("Downloaded and added to queue!").queue();
+            }
 
+        } catch (MalformedURLException ex)
+        {
+            event.getChannel().sendMessage("That's not a valid URL you idiot! " + ex.getMessage()).queue();
         } catch (IOException | InterruptedException | RuntimeException ex)
         {
             event.getChannel().sendMessage("Failed to download! Reason: " + ex.getMessage()).queue();
@@ -162,9 +171,9 @@ public class Main extends ListenerAdapter
         }
     }
 
-    private String downloadSong(String arg) throws IOException, RuntimeException, InterruptedException
+    private boolean downloadSong(Song song) throws IOException, RuntimeException, InterruptedException
     {
-        String cmd = "/usr/bin/youtube-dl -x --audio-format=wav " + arg;
+        String cmd = "/usr/bin/youtube-dl -x --audio-format=wav " + song.getUrl().toString();
         System.out.println("Running command: " + cmd);
 //		Process exec = Runtime.getRuntime().exec().split(" "));
         Process exec = new ProcessBuilder(cmd.split(" ")).redirectOutput(ProcessBuilder.Redirect.PIPE).start();
@@ -176,8 +185,10 @@ public class Main extends ListenerAdapter
             throw new RuntimeException("youtube-dl failed with error " + exec.exitValue());
         Matcher matcher = Pattern.compile("Destination: (.*\\.wav)").matcher(output);
         matcher.find();
-        String destination = matcher.group(1);
-        return destination;
+        song.setLocation(new File(matcher.group(0)));
+        return true;
+//        String destination = matcher.group(1);
+//        return destination;
     }
 
     /**
diff --git a/src/main/java/moe/nekojimi/chords/MusicHandler.java b/src/main/java/moe/nekojimi/chords/MusicHandler.java
index 0bd1f33..0a5e2e7 100644
--- a/src/main/java/moe/nekojimi/chords/MusicHandler.java
+++ b/src/main/java/moe/nekojimi/chords/MusicHandler.java
@@ -26,8 +26,8 @@ import net.dv8tion.jda.api.audio.AudioSendHandler;
 public class MusicHandler implements AudioSendHandler, Closeable
 {
 
-    private final Queue<File> songQueue = new ConcurrentLinkedQueue<>();
-    private File currentSong;
+    private final Queue<Song> songQueue = new ConcurrentLinkedQueue<>();
+    private Song currentSong;
     private AudioInputStream din = null;
     private final Queue<byte[]> queue = new ConcurrentLinkedQueue<>();
     private int byteCount;
@@ -36,10 +36,10 @@ public class MusicHandler implements AudioSendHandler, Closeable
     {
     }
 
-    public void addSong(File file)
+    public void addSong(Song song)
     {
-        System.out.println("Song added to queue: " + file.getAbsolutePath());
-        songQueue.add(file);
+        System.out.println("Song added to queue: " + song.getLocation().getAbsolutePath());
+        songQueue.add(song);
         if (!canProvide())
             nextSong();
     }
@@ -62,8 +62,8 @@ public class MusicHandler implements AudioSendHandler, Closeable
             currentSong = songQueue.poll();
             if (currentSong == null)
                 return false;
-            System.out.println("Playing song " + currentSong.getAbsolutePath());
-            in = AudioSystem.getAudioInputStream(currentSong);
+            System.out.println("Playing song " + currentSong.getLocation().getAbsolutePath());
+            in = AudioSystem.getAudioInputStream(currentSong.getLocation());
             AudioFormat decodedFormat = AudioSendHandler.INPUT_FORMAT;
             din = AudioSystem.getAudioInputStream(decodedFormat, in);
             byteCount = 3840;
diff --git a/src/main/java/moe/nekojimi/chords/Song.java b/src/main/java/moe/nekojimi/chords/Song.java
new file mode 100644
index 0000000..6971d4c
--- /dev/null
+++ b/src/main/java/moe/nekojimi/chords/Song.java
@@ -0,0 +1,67 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package moe.nekojimi.chords;
+
+import java.io.File;
+import java.net.URL;
+
+/**
+ *
+ * @author jimj316
+ */
+public class Song
+{
+    private String title;
+    private String artist;
+    private final URL url;
+    private File location = null;
+
+    public Song(URL url)
+    {
+        this.url = url;
+    }
+
+    public String getTitle()
+    {
+        return title;
+    }
+
+    public void setTitle(String title)
+    {
+        this.title = title;
+    }
+
+    public String getArtist()
+    {
+        return artist;
+    }
+
+    public void setArtist(String artist)
+    {
+        this.artist = artist;
+    }
+
+    public URL getUrl()
+    {
+        return url;
+    }
+
+    public File getLocation()
+    {
+        return location;
+    }
+
+    public void setLocation(File location)
+    {
+        this.location = location;
+    }
+
+    void delete()
+    {
+        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+    }
+
+}