From 7cd118985a2e14b247c524e243eac248e3e603a0 Mon Sep 17 00:00:00 2001 From: Nekojimi Date: Fri, 3 Mar 2023 18:41:02 +0000 Subject: [PATCH] Add new settings system, and allow youtube-dl command to be set via it. --- src/main/java/moe/nekojimi/chords/Chords.java | 14 ++++- .../moe/nekojimi/chords/CommandOptions.java | 28 +++++---- .../java/moe/nekojimi/chords/Downloader.java | 7 ++- .../java/moe/nekojimi/chords/Settings.java | 59 +++++++++++++++++++ 4 files changed, 92 insertions(+), 16 deletions(-) create mode 100644 src/main/java/moe/nekojimi/chords/Settings.java diff --git a/src/main/java/moe/nekojimi/chords/Chords.java b/src/main/java/moe/nekojimi/chords/Chords.java index e2338e8..759e46c 100644 --- a/src/main/java/moe/nekojimi/chords/Chords.java +++ b/src/main/java/moe/nekojimi/chords/Chords.java @@ -44,6 +44,7 @@ public final class Chords extends ListenerAdapter { private static final CommandOptions options = new CommandOptions(); + private static Settings settings; private final File dataDirectory; private final File playlistsDirectory; @@ -85,7 +86,9 @@ public final class Chords extends ListenerAdapter GatewayIntent.GUILD_MEMBERS ); - JDABuilder builder = JDABuilder.createDefault(options.getToken(), intents); + settings = new Settings(new File(options.getSettingsPath())); + + JDABuilder builder = JDABuilder.createDefault(settings.getDiscordToken(), intents); // Disable parts of the cache builder.disableCache(CacheFlag.MEMBER_OVERRIDES); @@ -101,11 +104,11 @@ public final class Chords extends ListenerAdapter builder.addEventListeners(listener); builder.setAutoReconnect(true); - if (options.getLocalAddress() != null) + if (settings.getLocalAddr() != null) { final WebSocketFactory webSocketFactory = new WebSocketFactory(); final LocalBindSocketFactory localBindSocketFactory = new LocalBindSocketFactory(); - localBindSocketFactory.setLocalAddress(InetAddress.getByName(options.getLocalAddress())); + localBindSocketFactory.setLocalAddress(InetAddress.getByName(settings.getLocalAddr())); webSocketFactory.setSocketFactory(localBindSocketFactory); builder.setWebsocketFactory(webSocketFactory); } @@ -399,6 +402,11 @@ public final class Chords extends ListenerAdapter return trackNumber; } + public static Settings getSettings() + { + return settings; + } + private class DownloaderMessageHandler implements BiConsumer { diff --git a/src/main/java/moe/nekojimi/chords/CommandOptions.java b/src/main/java/moe/nekojimi/chords/CommandOptions.java index 232a7b6..158650c 100644 --- a/src/main/java/moe/nekojimi/chords/CommandOptions.java +++ b/src/main/java/moe/nekojimi/chords/CommandOptions.java @@ -25,20 +25,28 @@ import com.beust.jcommander.Parameter; public class CommandOptions { - @Parameter(names = "-token", description = "The API token for Discord.", required = true) - private String token; + @Parameter(names = "-settings", description = "A path to the settings.yml file.") + private String settingsPath = "settings.yml"; - @Parameter(names = "-local-addr", description = "The local address to bind to.") - private String localAddress; +// @Parameter(names = "-token", description = "The API token for Discord.", required = true) +// private String token; +// +// @Parameter(names = "-local-addr", description = "The local address to bind to.") +// private String localAddress; - public String getToken() - { - return token; - } +// public String getToken() +// { +// return token; +// } +// +// public String getLocalAddress() +// { +// return localAddress; +// } - public String getLocalAddress() + public String getSettingsPath() { - return localAddress; + return settingsPath; } } diff --git a/src/main/java/moe/nekojimi/chords/Downloader.java b/src/main/java/moe/nekojimi/chords/Downloader.java index 24892c6..3f47df4 100644 --- a/src/main/java/moe/nekojimi/chords/Downloader.java +++ b/src/main/java/moe/nekojimi/chords/Downloader.java @@ -133,7 +133,7 @@ public class Downloader implements Consumer return; try { - String cmd = "/usr/bin/youtube-dl --skip-download -F " + song.getUrl().toString(); + String cmd = Chords.getSettings().getYtdlCommand() + " --skip-download -F " + song.getUrl().toString(); Process exec = runCommand(cmd, FORMAT_TIMEOUT); InputStream input = exec.getInputStream(); String output = new String(input.readAllBytes(), Charset.defaultCharset()); @@ -166,7 +166,7 @@ public class Downloader implements Consumer { try { - String cmd = "/usr/bin/youtube-dl --skip-download --print-json " + song.getUrl().toString(); + String cmd = Chords.getSettings().getYtdlCommand() + " --skip-download --print-json " + song.getUrl().toString(); Process exec = runCommand(cmd, INFO_TIMEOUT); InputStream input = exec.getInputStream(); JsonReader reader = Json.createReader(input); @@ -213,7 +213,8 @@ public class Downloader implements Consumer try { messageHandler.accept(task.request, null); - String cmd = "/usr/bin/youtube-dl -x" + String cmd = Chords.getSettings().getYtdlCommand() + + " -x" + " -f " + formatCodes + "worstaudio/bestaudio/worst/best" + " --audio-format=wav" + " --no-playlist" diff --git a/src/main/java/moe/nekojimi/chords/Settings.java b/src/main/java/moe/nekojimi/chords/Settings.java new file mode 100644 index 0000000..1e5df54 --- /dev/null +++ b/src/main/java/moe/nekojimi/chords/Settings.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2023 jimj316 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package moe.nekojimi.chords; + +import com.amihaiemil.eoyaml.Yaml; +import com.amihaiemil.eoyaml.YamlInput; +import com.amihaiemil.eoyaml.YamlMapping; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; + +/** + * + * @author jimj316 + */ +public class Settings +{ + + private final YamlMapping mapping; + + public Settings(File file) throws FileNotFoundException, IOException + { + YamlInput input = Yaml.createYamlInput(file); + mapping = input.readYamlMapping(); + + } + + public String getDiscordToken() + { + return mapping.string("discord-token"); + } + + public String getLocalAddr() + { + return mapping.string("local-addr"); + } + + public String getYtdlCommand() + { + String ret = mapping.string("ytdl-cmd"); + if (ret == null) + ret = "/usr/bin/youtube-dl"; + return ret; + } +}