From e9ee8a575d890528940304997e9326e71c0edc53 Mon Sep 17 00:00:00 2001 From: Nekojimi Date: Tue, 15 Feb 2022 22:58:24 +0000 Subject: [PATCH] Change downloader to allow different destinations for downloaded songs. --- .../java/moe/nekojimi/chords/Downloader.java | 80 ++++++++++++------- src/main/java/moe/nekojimi/chords/Main.java | 3 +- .../chords/commands/QueueCommand.java | 7 +- 3 files changed, 57 insertions(+), 33 deletions(-) diff --git a/src/main/java/moe/nekojimi/chords/Downloader.java b/src/main/java/moe/nekojimi/chords/Downloader.java index f1ae83d..36b05c7 100644 --- a/src/main/java/moe/nekojimi/chords/Downloader.java +++ b/src/main/java/moe/nekojimi/chords/Downloader.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 +public class Downloader implements Consumer { 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 downloadQueue = new LinkedList<>(); + private final List downloadQueue = new LinkedList<>(); private final LinkedBlockingDeque workQueue = new LinkedBlockingDeque<>(); private final ThreadPoolExecutor exec = new ThreadPoolExecutor(2, 4, 30, TimeUnit.SECONDS, workQueue); - private Consumer next; +// private Consumer next; private BiConsumer messageHandler; private File downloadDir = null; @@ -52,32 +53,30 @@ public class Downloader implements Consumer } @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 { - try - { - getFormats(song); - download(song); - } catch (Exception ex) - { - ex.printStackTrace(); - } + getFormats(task.getSong()); + download(task); + } catch (Exception ex) + { + ex.printStackTrace(); } }); } - public void setNext(Consumer next) - { - this.next = next; - } - private void chooseFormats(Song song) { List formats = song.getFormats(); @@ -195,8 +194,9 @@ public class Downloader implements Consumer return downloadDir; } - private void download(Song song) + private void download(DownloadTask task) { + Song song = task.song; chooseFormats(song); String formatCodes = ""; final List formats = song.getFormats(); @@ -225,16 +225,16 @@ public class Downloader implements Consumer 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 this.messageHandler = messageHandler; } - public List getDownloadQueue() + public List getDownloadQueue() { return downloadQueue; } + public static class DownloadTask + { + + private final Song song; + private final Consumer destination; + + public DownloadTask(Song song, Consumer destination) + { + this.song = song; + this.destination = destination; + } + + public Song getSong() + { + return song; + } + + public Consumer getDestination() + { + return destination; + } + + } + } diff --git a/src/main/java/moe/nekojimi/chords/Main.java b/src/main/java/moe/nekojimi/chords/Main.java index 2adc300..3546eae 100644 --- a/src/main/java/moe/nekojimi/chords/Main.java +++ b/src/main/java/moe/nekojimi/chords/Main.java @@ -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(); diff --git a/src/main/java/moe/nekojimi/chords/commands/QueueCommand.java b/src/main/java/moe/nekojimi/chords/commands/QueueCommand.java index a2811e5..e01587d 100644 --- a/src/main/java/moe/nekojimi/chords/commands/QueueCommand.java +++ b/src/main/java/moe/nekojimi/chords/commands/QueueCommand.java @@ -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 downloadQueue = bot.getDownloader().getDownloadQueue(); + final List 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++; } }