|
|
@ -1,15 +1,18 @@ |
|
|
|
package de.yannicpunktdee.yoshibot.main; |
|
|
|
package de.yannicpunktdee.yoshibot.utils; |
|
|
|
|
|
|
|
import de.yannicpunktdee.yoshibot.utils.Logger; |
|
|
|
import de.yannicpunktdee.yoshibot.utils.Logger.Type; |
|
|
|
import lombok.Getter; |
|
|
|
import org.json.JSONArray; |
|
|
|
import org.json.JSONObject; |
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.FileInputStream; |
|
|
|
import java.io.IOException; |
|
|
|
import java.nio.file.Files; |
|
|
|
import java.util.*; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
import java.util.stream.StreamSupport; |
|
|
|
|
|
|
|
public class Resources { |
|
|
|
public final class Resources { |
|
|
|
|
|
|
|
private static final String default_propertiesFilePath = "rsc/Config.properties"; |
|
|
|
private static String propertiesFilePath = default_propertiesFilePath; |
|
|
@ -22,14 +25,22 @@ public class Resources { |
|
|
|
|
|
|
|
private static final String[] default_restrict_commands_to_channel = null; |
|
|
|
private static String[] restrict_commands_to_channel = default_restrict_commands_to_channel; |
|
|
|
|
|
|
|
private static String[] filtered_tags; |
|
|
|
private static Map<String, List<String>> feedDetails; |
|
|
|
|
|
|
|
|
|
|
|
@Getter |
|
|
|
private static String[] filteredTags; |
|
|
|
@Getter |
|
|
|
private static final Map<String, List<String>> feedDetails = new HashMap<>(); |
|
|
|
|
|
|
|
@Getter |
|
|
|
private static String tts_path; |
|
|
|
|
|
|
|
@Getter |
|
|
|
private static String sauceConfigPath; |
|
|
|
|
|
|
|
|
|
|
|
public synchronized static boolean init(String pathToConfig) { |
|
|
|
Logger.log("Lade Config.properties ...", Type.INFO); |
|
|
|
|
|
|
|
|
|
|
|
if (pathToConfig != null) { |
|
|
|
if (!(new File(pathToConfig)).exists()) { |
|
|
|
Logger.log("Der in den Argumenten angegebene Pfad zur Config.properties existiert nicht.", Type.ERROR); |
|
|
@ -41,26 +52,31 @@ public class Resources { |
|
|
|
Type.ERROR); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
propertiesFile = new Properties(); |
|
|
|
try { |
|
|
|
propertiesFile.load( |
|
|
|
new FileInputStream(propertiesFilePath) |
|
|
|
); |
|
|
|
); |
|
|
|
Logger.log("Config-Datei erfolgreich geladen.", Type.INFO); |
|
|
|
} catch (IOException e) { |
|
|
|
Logger.log("Es ist ein Fehler beim Öffnen der Config.propeties aufgetreten.", Type.ERROR); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
tts_path = propertiesFile.getProperty("path_to_tts"); |
|
|
|
sauceConfigPath = propertiesFile.getProperty("path_to_sauce_config"); |
|
|
|
|
|
|
|
boolean isOk = initJdaBuilderString(); |
|
|
|
if (isOk) isOk = initChannelRestrict(); |
|
|
|
if (isOk) isOk = initAudio(); |
|
|
|
initTagFilter(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isOk) Logger.log("Die Konfigurationen wurden erfolgreich geladen.", Type.INFO); |
|
|
|
else Logger.log("Die Konfiguration konnte nicht geladen werden", Type.ERROR); |
|
|
|
|
|
|
|
|
|
|
|
return isOk; |
|
|
|
} |
|
|
|
|
|
|
@ -72,60 +88,75 @@ public class Resources { |
|
|
|
jda_builder_string = propertiesFile.getProperty("jda_builder_string"); |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static String getJdaBuilderString() { |
|
|
|
return jda_builder_string; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void initTagFilter() { |
|
|
|
if (!propertiesFile.containsKey("tags_general_filter")) { |
|
|
|
Logger.log("Kein Attribut 'tags_general_filter' gefunden", Type.WARNING); |
|
|
|
} else { |
|
|
|
filtered_tags = propertiesFile.getProperty("tags_general_filter").split(" "); |
|
|
|
} |
|
|
|
if (!propertiesFile.containsKey("feed_positive_tags")) { |
|
|
|
Logger.log("Kein Attribut 'feed_positive_tags' gefunden", Type.WARNING); |
|
|
|
} else { |
|
|
|
String[] automatedFeeds = propertiesFile.getProperty("feed_positive_tags").split("//"); |
|
|
|
feedDetails = Arrays.stream(automatedFeeds).map(feedDetail -> feedDetail.split(" ")) |
|
|
|
.collect(Collectors.toMap(list -> list[0], |
|
|
|
list -> Arrays.asList(list).subList(1, list.length))); |
|
|
|
try { |
|
|
|
JSONObject configBase = new JSONObject( |
|
|
|
String.join("\n", |
|
|
|
Files.readAllLines(new File(sauceConfigPath).toPath()))); |
|
|
|
JSONArray filter = configBase.getJSONArray("tags_general_filter"); |
|
|
|
filteredTags = |
|
|
|
StreamSupport.stream(filter.spliterator(), false).map(i -> (String) i).toArray(String[]::new); |
|
|
|
JSONArray feeds = configBase.getJSONArray("feeds"); |
|
|
|
for (Object feedConfigObj : feeds) { |
|
|
|
JSONObject feedConfig = (JSONObject) feedConfigObj; |
|
|
|
List<String> tags = new ArrayList<>(); |
|
|
|
for (Object tagObj : feedConfig.getJSONArray("tags")) { |
|
|
|
if (tagObj instanceof String) { |
|
|
|
tags.add((String) tagObj); |
|
|
|
} else if (tagObj instanceof JSONArray) { |
|
|
|
StringBuilder sb = new StringBuilder().append("(%20"); |
|
|
|
sb.append(((JSONArray) tagObj).join("%20~%20")); |
|
|
|
tags.add(sb.append("%20)").toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
feedDetails.put(feedConfig.getString("channel"), tags); |
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static boolean initAudio() { |
|
|
|
if (propertiesFile.containsKey("audio_source_directory")) { |
|
|
|
audio_source_directory = propertiesFile.getProperty("audio_source_directory"); |
|
|
|
} else { |
|
|
|
Logger.log("Die Config.properties spezifiziert kein audio_source_directory. Lade default.", Type.WARNING); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
File file = new File(audio_source_directory); |
|
|
|
if (!file.exists() || !file.isDirectory()) { |
|
|
|
Logger.log("Das Audio-Verzeichnis wurde nicht gefunden.", Type.ERROR); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (file.listFiles().length < 1) |
|
|
|
Logger.log("Das Audio-Verzeichnis ist leer.", Type.WARNING); |
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static String getAudioFilePath(String name) { |
|
|
|
name = audio_source_directory + (audio_source_directory.endsWith("/") ? "" : "/") + name + ".opus"; |
|
|
|
if ((new File(name)).exists()) return name; |
|
|
|
else return null; |
|
|
|
} |
|
|
|
|
|
|
|
public static String getTempAudioFilePath(String name) { |
|
|
|
name = audio_source_directory + (audio_source_directory.endsWith("/")? "" : "/") + "temp/" + name + ".opus"; |
|
|
|
if((new File(name)).exists()) return name; |
|
|
|
name = audio_source_directory + (audio_source_directory.endsWith("/") ? "" : "/") + "temp/" + name + ".opus"; |
|
|
|
if ((new File(name)).exists()) return name; |
|
|
|
else return null; |
|
|
|
} |
|
|
|
public static String buildTempAudioFilePath(String name){ |
|
|
|
|
|
|
|
public static String buildTempAudioFilePath(String name) { |
|
|
|
return System.getProperty("java.io.tmpdir") + "/yoshibot/" + name; |
|
|
|
} |
|
|
|
|
|
|
|
public static String getAudioSourceDirectory() { |
|
|
|
return audio_source_directory; |
|
|
|
} |
|
|
@ -135,17 +166,10 @@ public class Resources { |
|
|
|
restrict_commands_to_channel = propertiesFile.getProperty("restrict_commands_to_channel").split(" "); |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static String[] getRestrictCommandsToChannel() { |
|
|
|
return restrict_commands_to_channel; |
|
|
|
} |
|
|
|
|
|
|
|
public static Map<String, List<String>> getFeedDetails() { |
|
|
|
return feedDetails; |
|
|
|
} |
|
|
|
|
|
|
|
public static String[] getGeneralFilterTags() { |
|
|
|
return filtered_tags; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |