|
|
@ -14,26 +14,36 @@ import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
import java.util.stream.StreamSupport; |
|
|
|
|
|
|
|
public class SauceProvider { |
|
|
|
public final class SauceProvider { |
|
|
|
|
|
|
|
private static final String BASE_URL = "https://r34-json.herokuapp.com/"; |
|
|
|
|
|
|
|
private int lastKnownSauce = -1; |
|
|
|
private static int lastKnownSauce = -1; |
|
|
|
|
|
|
|
private boolean isSauceInit = false; |
|
|
|
private static boolean isSauceInit = false; |
|
|
|
|
|
|
|
private static MessageEmbed notFoundEmbed = null; |
|
|
|
|
|
|
|
public SauceProvider(int timer) { |
|
|
|
lastKnownSauce = this.getNewestIndex(); |
|
|
|
ScheduledExecutorService sauceScheduler = Executors.newScheduledThreadPool(4); |
|
|
|
sauceScheduler.scheduleAtFixedRate(this::provideSauce, 0, timer, TimeUnit.SECONDS); |
|
|
|
new Thread(this::initSauceProviding).start(); |
|
|
|
private static final ScheduledExecutorService sauceScheduler = Executors.newScheduledThreadPool(1); |
|
|
|
|
|
|
|
public static void init(int secondsPerTime) { |
|
|
|
init(secondsPerTime, TimeUnit.SECONDS); |
|
|
|
} |
|
|
|
|
|
|
|
public static void init(int timer, TimeUnit timeUnit) { |
|
|
|
init(timer, timeUnit, getNewestIndex()); |
|
|
|
} |
|
|
|
|
|
|
|
public SauceProvider(int timer, int lastKnownSauce) { |
|
|
|
this(timer); |
|
|
|
this.lastKnownSauce = lastKnownSauce; |
|
|
|
public static void init(int timer, TimeUnit timeUnit, int lastKnownSauce) { |
|
|
|
sauceScheduler.scheduleAtFixedRate(() -> { |
|
|
|
try { |
|
|
|
SauceProvider.provideSauce(); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
}, 0, timer, timeUnit); |
|
|
|
SauceProvider.lastKnownSauce = lastKnownSauce; |
|
|
|
new Thread(SauceProvider::initSauceProviding).start(); |
|
|
|
} |
|
|
|
|
|
|
|
public static MessageEmbed getSauce(int index) { |
|
|
@ -69,15 +79,27 @@ public class SauceProvider { |
|
|
|
private static MessageEmbed makeStringFromJson(JSONObject post) { |
|
|
|
EmbedBuilder eb = new EmbedBuilder(); |
|
|
|
eb.setTitle("Soße").setDescription("URL: " + post.getString("file_url").substring(42)); |
|
|
|
String tags = "`" + post.getJSONArray("tags").join("` `") + "`"; |
|
|
|
if (tags.length() > 1024) { |
|
|
|
List<String> tagParts = new ArrayList<>(); |
|
|
|
while (tags.length() > 1024) { |
|
|
|
int lastindexoftag = tags.substring(0, 1024).lastIndexOf(' '); |
|
|
|
tagParts.add(tags.substring(0, lastindexoftag)); |
|
|
|
tags = tags.substring(lastindexoftag + 1); |
|
|
|
} |
|
|
|
tagParts.add(tags); |
|
|
|
tagParts.forEach(tagPart -> eb.addField("Tags:", tagPart, false)); |
|
|
|
} else { |
|
|
|
eb.addField("Tags", tags, false); |
|
|
|
} |
|
|
|
eb.addField("ID", post.getString("id"), false); |
|
|
|
eb.addField("Tags", "`" + post.getJSONArray("tags").join("` `") + "`", false); |
|
|
|
eb.setImage(post.getString("file_url").substring(42)); |
|
|
|
|
|
|
|
return eb.build(); |
|
|
|
} |
|
|
|
|
|
|
|
private void provideSauce() { |
|
|
|
if (!isSauceInit) return; |
|
|
|
private static void provideSauce() { |
|
|
|
if (!isSauceInit || lastKnownSauce < 0) return; |
|
|
|
for (Map.Entry<String, List<String>> feed : Resources.getFeedDetails().entrySet()) { |
|
|
|
String url = BASE_URL + "posts?tags=" + String.join("+", feed.getValue()) |
|
|
|
+ "+" + String.join("+", Resources.getFilteredTags()); |
|
|
@ -106,10 +128,10 @@ public class SauceProvider { |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
lastKnownSauce = this.getNewestIndex(); |
|
|
|
lastKnownSauce = getNewestIndex(); |
|
|
|
} |
|
|
|
|
|
|
|
private void initSauceProviding() { |
|
|
|
private static void initSauceProviding() { |
|
|
|
YoshiBot yoshiBot = YoshiBot.getInstance(); |
|
|
|
try { |
|
|
|
yoshiBot.jda.awaitReady(); |
|
|
@ -119,15 +141,15 @@ public class SauceProvider { |
|
|
|
for (Map.Entry<String, List<String>> entry : Resources.getFeedDetails().entrySet()) { |
|
|
|
List<TextChannel> channels = yoshiBot.jda.getTextChannelsByName(entry.getKey(), true); |
|
|
|
if (channels.size() > 0) { |
|
|
|
this.isSauceInit = true; |
|
|
|
this.provideSauce(); |
|
|
|
isSauceInit = true; |
|
|
|
provideSauce(); |
|
|
|
} else { |
|
|
|
Logger.logError("Konnte keine Kanaäle finden für die Soße"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private int getNewestIndex() { |
|
|
|
private static int getNewestIndex() { |
|
|
|
JSONObject result = getParsedSauceData("https://r34-json.herokuapp.com/posts?limit=1&q=index"); |
|
|
|
int id = result.getJSONArray("posts").getJSONObject(0).getInt("id"); |
|
|
|
Logger.logDebug("Neuste Soßen-ID: " + id); |
|
|
|