Refactor config loading
This commit is contained in:
parent
5dd7d03053
commit
21955413ed
58
src/main/java/com/annimon/voicyanski/Config.java
Normal file
58
src/main/java/com/annimon/voicyanski/Config.java
Normal file
@ -0,0 +1,58 @@
|
||||
package com.annimon.voicyanski;
|
||||
|
||||
public class Config {
|
||||
|
||||
private String locale;
|
||||
private String telegramChatId, telegramBotToken;
|
||||
private boolean encodeStereo;
|
||||
private String encodeBitrate;
|
||||
private String encodeSampleRate;
|
||||
|
||||
public String getLocale() {
|
||||
return locale;
|
||||
}
|
||||
|
||||
public void setLocale(String locale) {
|
||||
this.locale = locale;
|
||||
}
|
||||
|
||||
public String getTelegramChatId() {
|
||||
return telegramChatId;
|
||||
}
|
||||
|
||||
public void setTelegramChatId(String telegramChatId) {
|
||||
this.telegramChatId = telegramChatId;
|
||||
}
|
||||
|
||||
public String getTelegramBotToken() {
|
||||
return telegramBotToken;
|
||||
}
|
||||
|
||||
public void setTelegramBotToken(String telegramBotToken) {
|
||||
this.telegramBotToken = telegramBotToken;
|
||||
}
|
||||
|
||||
public boolean isEncodeStereo() {
|
||||
return encodeStereo;
|
||||
}
|
||||
|
||||
public void setEncodeStereo(boolean encodeStereo) {
|
||||
this.encodeStereo = encodeStereo;
|
||||
}
|
||||
|
||||
public String getEncodeBitrate() {
|
||||
return encodeBitrate;
|
||||
}
|
||||
|
||||
public void setEncodeBitrate(String encodeBitrate) {
|
||||
this.encodeBitrate = encodeBitrate;
|
||||
}
|
||||
|
||||
public String getEncodeSampleRate() {
|
||||
return encodeSampleRate;
|
||||
}
|
||||
|
||||
public void setEncodeSampleRate(String encodeSampleRate) {
|
||||
this.encodeSampleRate = encodeSampleRate;
|
||||
}
|
||||
}
|
46
src/main/java/com/annimon/voicyanski/ConfigLoader.java
Normal file
46
src/main/java/com/annimon/voicyanski/ConfigLoader.java
Normal file
@ -0,0 +1,46 @@
|
||||
package com.annimon.voicyanski;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Properties;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
public class ConfigLoader {
|
||||
|
||||
private final String resourceName;
|
||||
private final Path externalPath;
|
||||
|
||||
public ConfigLoader(String resourceName, Path externalPath) {
|
||||
this.resourceName = resourceName;
|
||||
this.externalPath = externalPath;
|
||||
}
|
||||
|
||||
public Config load() {
|
||||
final Config config = new Config();
|
||||
final ResourceBundle res = ResourceBundle.getBundle(resourceName);
|
||||
try (InputStream extConfig = Files.newInputStream(externalPath)) {
|
||||
final Properties props = new Properties();
|
||||
props.load(extConfig);
|
||||
loadConfig(config, key -> props.getProperty(key, res.getString(key)));
|
||||
} catch (IOException ex) {
|
||||
loadConfig(config, res::getString);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
private void loadConfig(Config config, PropertyLoader loader) {
|
||||
config.setLocale(loader.load("locale"));
|
||||
config.setTelegramChatId(loader.load("telegram-chat-id"));
|
||||
config.setTelegramBotToken(loader.load("telegram-bot-token"));
|
||||
config.setEncodeStereo(loader.load("encode-stereo").equalsIgnoreCase("true"));
|
||||
config.setEncodeBitrate(loader.load("encode-bitrate"));
|
||||
config.setEncodeSampleRate(loader.load("encode-samplerate"));
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
private interface PropertyLoader {
|
||||
String load(String key);
|
||||
}
|
||||
}
|
@ -1,11 +1,8 @@
|
||||
package com.annimon.voicyanski;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Locale;
|
||||
import java.util.Properties;
|
||||
import java.util.ResourceBundle;
|
||||
import javafx.application.Application;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
@ -14,58 +11,24 @@ import javafx.stage.Stage;
|
||||
|
||||
public class Main extends Application {
|
||||
|
||||
private static ResourceBundle resources, config;
|
||||
Properties props = new Properties();
|
||||
public static String locale, chatId, botToken, stereo, bitrate, samplerate;
|
||||
private static ResourceBundle resources;
|
||||
private static Config config;
|
||||
|
||||
public static ResourceBundle getResources() {
|
||||
return resources;
|
||||
}
|
||||
|
||||
public static ResourceBundle getConfig() {
|
||||
public static Config getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
private String getVar(String key) {
|
||||
String result = props.getProperty(key);
|
||||
return result != null ? result : config.getString(key);
|
||||
}
|
||||
|
||||
private void setupVars() {
|
||||
|
||||
config = ResourceBundle.getBundle("app");
|
||||
try {
|
||||
InputStream extConfig = Files.newInputStream(Paths.get(
|
||||
System.getProperty("user.home"),
|
||||
".config",
|
||||
"voicyanski.conf"));
|
||||
props.load(extConfig);
|
||||
extConfig.close();
|
||||
|
||||
locale = getVar("locale");
|
||||
chatId = getVar("telegram-chat-id");
|
||||
botToken = getVar("telegram-bot-token");
|
||||
stereo = getVar("encode-stereo");
|
||||
bitrate = getVar("encode-bitrate");
|
||||
samplerate = getVar("encode-samplerate");
|
||||
} catch (IOException ex) {
|
||||
locale = config.getString("locale");
|
||||
chatId = config.getString("telegram-chat-id");
|
||||
botToken = config.getString("telegram-bot-token");
|
||||
stereo = config.getString("encode-stereo");
|
||||
bitrate = config.getString("encode-bitrate");
|
||||
samplerate = config.getString("encode-samplerate");
|
||||
|
||||
}
|
||||
if (locale != null) {
|
||||
Locale.setDefault(new Locale(locale));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Stage stage) throws Exception {
|
||||
setupVars();
|
||||
final Path configPath = Paths.get(System.getProperty("user.home"), ".config", "voicyanski.conf");
|
||||
Main.config = new ConfigLoader("app", configPath).load();
|
||||
if (config.getLocale() != null) {
|
||||
Locale.setDefault(new Locale(config.getLocale()));
|
||||
}
|
||||
Main.resources = ResourceBundle.getBundle("locales.Language", Locale.getDefault());
|
||||
final FXMLLoader loader = new FXMLLoader(
|
||||
getClass().getResource("/fxml/Main.fxml"), Main.resources);
|
||||
|
@ -22,12 +22,12 @@ public final class OpusConvertTask implements UnaryOperator<ClipPartData> {
|
||||
"-ss", Double.toString(data.startTime),
|
||||
"-i", data.metadata.file.getAbsolutePath(),
|
||||
"-t", Double.toString(data.endTime - data.startTime),
|
||||
"-ac", Main.stereo.equalsIgnoreCase("true") ? "2" : "1",
|
||||
"-ac", Main.getConfig().isEncodeStereo() ? "2" : "1",
|
||||
"-map", "0:a",
|
||||
"-codec:a", "libopus",
|
||||
"-b:a", Main.bitrate,
|
||||
"-b:a", Main.getConfig().getEncodeBitrate(),
|
||||
"-vbr", "off",
|
||||
"-ar", Main.samplerate,
|
||||
"-ar", Main.getConfig().getEncodeSampleRate(),
|
||||
data.outputAudioFile.getAbsolutePath()
|
||||
);
|
||||
final Process ffmpeg = pb.start();
|
||||
|
@ -25,13 +25,13 @@ public final class SendTelegramVoiceTask implements Function<ClipPartData, Boole
|
||||
if (data.metadata.hasTrackInfo()) {
|
||||
builder.addFormDataPart("caption", data.metadata.toString());
|
||||
}
|
||||
builder.addFormDataPart("chat_id", Main.chatId);
|
||||
builder.addFormDataPart("chat_id", Main.getConfig().getTelegramChatId());
|
||||
builder.addFormDataPart("voice", data.metadata.file.getName(),
|
||||
RequestBody.create(MEDIA_TYPE_OGG, data.outputAudioFile));
|
||||
final RequestBody requestBody = builder.build();
|
||||
|
||||
final Request request = new Request.Builder()
|
||||
.url(String.format(URL_FORMAT, Main.botToken, "sendVoice"))
|
||||
.url(String.format(URL_FORMAT, Main.getConfig().getTelegramBotToken(), "sendVoice"))
|
||||
.post(requestBody)
|
||||
.build();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user