Change downloader to allow different destinations for downloaded songs.

crossfading
Nekojimi 3 years ago
parent 4220222ad5
commit e9ee8a575d
  1. 70
      src/main/java/moe/nekojimi/chords/Downloader.java
  2. 3
      src/main/java/moe/nekojimi/chords/Main.java
  3. 7
      src/main/java/moe/nekojimi/chords/commands/QueueCommand.java

@ -26,22 +26,23 @@ import java.util.stream.Collectors;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import moe.nekojimi.chords.Downloader.DownloadTask;
/**
*
* @author jimj316
*/
public class Downloader implements Consumer<Song>
public class Downloader implements Consumer<DownloadTask>
{
private static final int BITRATE_TARGET = 64_000;
private static final Pattern FORMAT_PATTERN = Pattern.compile("^([\\w]+)\\s+([\\w]+)\\s+(\\w+ ?\\w*)\\s+(.*)$");
public static final Pattern DESTINATION_PATTERN = Pattern.compile("Destination: (.*\\.wav)");
private final List<Song> downloadQueue = new LinkedList<>();
private final List<DownloadTask> downloadQueue = new LinkedList<>();
private final LinkedBlockingDeque<Runnable> workQueue = new LinkedBlockingDeque<>();
private final ThreadPoolExecutor exec = new ThreadPoolExecutor(2, 4, 30, TimeUnit.SECONDS, workQueue);
private Consumer<Song> next;
// private Consumer<Song> next;
private BiConsumer<Song, Exception> messageHandler;
private File downloadDir = null;
@ -52,32 +53,30 @@ public class Downloader implements Consumer<Song>
}
@Override
public void accept(Song song)
public void accept(DownloadTask task)
{
downloadQueue.add(song);
getInfo(song);
exec.submit(new Runnable()
// if already downloaded, just skip
if (task.getSong().isDownloaded())
{
@Override
public void run()
task.getDestination().accept(task.getSong());
return;
}
downloadQueue.add(task);
getInfo(task.getSong());
exec.submit(() ->
{
try
{
getFormats(song);
download(song);
getFormats(task.getSong());
download(task);
} catch (Exception ex)
{
ex.printStackTrace();
}
}
});
}
public void setNext(Consumer<Song> next)
{
this.next = next;
}
private void chooseFormats(Song song)
{
List<Format> formats = song.getFormats();
@ -195,8 +194,9 @@ public class Downloader implements Consumer<Song>
return downloadDir;
}
private void download(Song song)
private void download(DownloadTask task)
{
Song song = task.song;
chooseFormats(song);
String formatCodes = "";
final List<Format> formats = song.getFormats();
@ -225,16 +225,16 @@ public class Downloader implements Consumer<Song>
throw new RuntimeException("youtube-dl failed with error " + exec.exitValue() + ", output:\n" + error);
// return true;
if (next != null)
next.accept(song);
downloadQueue.remove(song);
if (task.getDestination() != null)
task.getDestination().accept(song);
downloadQueue.remove(task);
messageHandler.accept(song, null);
} catch (Exception ex)
{
Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex);
if (messageHandler != null)
messageHandler.accept(song, ex);
downloadQueue.remove(song);
downloadQueue.remove(task);
}
}
@ -262,9 +262,33 @@ public class Downloader implements Consumer<Song>
this.messageHandler = messageHandler;
}
public List<Song> getDownloadQueue()
public List<DownloadTask> getDownloadQueue()
{
return downloadQueue;
}
public static class DownloadTask
{
private final Song song;
private final Consumer<Song> destination;
public DownloadTask(Song song, Consumer<Song> destination)
{
this.song = song;
this.destination = destination;
}
public Song getSong()
{
return song;
}
public Consumer<Song> getDestination()
{
return destination;
}
}
}

@ -172,7 +172,7 @@ public class Main extends ListenerAdapter
song.setRequestedIn(event.getChannel().getId());
song.setNumber(trackNumber);
trackNumber++;
downloader.accept(song);
downloader.accept(new Downloader.DownloadTask(song, musicHandler));
return song;
}
@ -197,7 +197,6 @@ public class Main extends ListenerAdapter
// Get an audio manager for this guild, this will be created upon first use for each guild
AudioManager audioManager = guild.getAudioManager();
musicHandler = new MusicHandler();
downloader.setNext(musicHandler);
// Create our Send/Receive handler for the audio connection
// EchoHandler handler = new EchoHandler();

@ -18,6 +18,7 @@ package moe.nekojimi.chords.commands;
import java.util.List;
import java.util.Queue;
import moe.nekojimi.chords.Downloader;
import moe.nekojimi.chords.Main;
import moe.nekojimi.chords.Song;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
@ -53,13 +54,13 @@ public class QueueCommand extends Command
}
}
final List<Song> downloadQueue = bot.getDownloader().getDownloadQueue();
final List<Downloader.DownloadTask> downloadQueue = bot.getDownloader().getDownloadQueue();
if (!downloadQueue.isEmpty())
{
message += "__Downloading:__\n";
for (Song song : downloadQueue)
for (Downloader.DownloadTask task : downloadQueue)
{
message += ":inbox_tray: **" + (i) + ":** " + song + "\n";
message += ":inbox_tray: **" + (i) + ":** " + task.getSong() + "\n";
i++;
}
}

Loading…
Cancel
Save