|
|
@ -14,26 +14,30 @@ 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(4); |
|
|
|
|
|
|
|
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(SauceProvider::provideSauce, 0, timer, timeUnit); |
|
|
|
SauceProvider.lastKnownSauce = lastKnownSauce; |
|
|
|
new Thread(SauceProvider::initSauceProviding).start(); |
|
|
|
} |
|
|
|
|
|
|
|
public static MessageEmbed getSauce(int index) { |
|
|
@ -76,8 +80,8 @@ public class SauceProvider { |
|
|
|
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 +110,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 +123,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); |
|
|
|