|
|
@ -24,6 +24,8 @@ public class SauceProvider { |
|
|
|
|
|
|
|
private boolean isSauceInit = false; |
|
|
|
|
|
|
|
private static MessageEmbed notFoundEmbed = null; |
|
|
|
|
|
|
|
public SauceProvider(int timer) { |
|
|
|
this(); |
|
|
|
ScheduledExecutorService sauceScheduler = Executors.newScheduledThreadPool(1); |
|
|
@ -39,17 +41,24 @@ public class SauceProvider { |
|
|
|
|
|
|
|
public static MessageEmbed getSauce(int index) { |
|
|
|
String url = BASE_URL + "posts?id=" + index; |
|
|
|
JSONObject post = getParsedSauceData(url).getJSONArray("posts").getJSONObject(0); |
|
|
|
JSONObject base = getParsedSauceData(url); |
|
|
|
if (base.getInt("count") == 0){ |
|
|
|
return getNotFoundEmbed(); |
|
|
|
} |
|
|
|
JSONObject post = base.getJSONArray("posts").getJSONObject(0); |
|
|
|
return makeStringFromJson(post); |
|
|
|
} |
|
|
|
|
|
|
|
public static MessageEmbed getRandomSauce(String tags) { |
|
|
|
tags = tagsForRest(tags); |
|
|
|
tags += String.join("+-", Resources.getGeneralFilterTags()); |
|
|
|
tags += "+" + String.join("+", Resources.getGeneralFilterTags()); |
|
|
|
Random rand = new Random(); |
|
|
|
String url = BASE_URL + "posts?tags=" + String.join("+", tags); |
|
|
|
JSONObject baseObj = getParsedSauceData(url); |
|
|
|
int amount = baseObj.getInt("count"); |
|
|
|
if (amount == 0){ |
|
|
|
return getNotFoundEmbed(); |
|
|
|
} |
|
|
|
int selectedIndex = rand.nextInt(amount); |
|
|
|
int page = (selectedIndex / 100) % 100; |
|
|
|
int pageIndex = selectedIndex % 100; |
|
|
@ -85,8 +94,15 @@ public class SauceProvider { |
|
|
|
Collections.reverse(postsInternal); |
|
|
|
YoshiBot yoshiBot = YoshiBot.getInstance(); |
|
|
|
for (JSONObject post : postsInternal) { |
|
|
|
yoshiBot.jda.getTextChannelsByName(feed.getKey(), true).get(0) |
|
|
|
.sendMessage(makeStringFromJson(post)).queue(); |
|
|
|
List<TextChannel> channels = yoshiBot.jda.getTextChannelsByName(feed.getKey(), true); |
|
|
|
if (channels.size() == 0) { |
|
|
|
Logger.log("Kein Kanal mit dem Namen " + feed.getKey() + "gefunden", Logger.Type.ERROR); |
|
|
|
break; |
|
|
|
} else if (!channels.get(0).isNSFW()) { |
|
|
|
Logger.log("Kanal " + feed.getKey() + " ist nicht als NSFW markiert!", Logger.Type.ERROR); |
|
|
|
break; |
|
|
|
} |
|
|
|
channels.get(0).sendMessage(makeStringFromJson(post)).queue(); |
|
|
|
} |
|
|
|
Logger.log(String.format("Found %d posts for feed '%s'", postsInternal.size(), feed.getKey()), |
|
|
|
Logger.Type.INFO); |
|
|
@ -95,12 +111,12 @@ public class SauceProvider { |
|
|
|
} |
|
|
|
|
|
|
|
private void initSauceProviding() { |
|
|
|
YoshiBot yoshiBot = YoshiBot.getInstance(); |
|
|
|
try { |
|
|
|
Thread.sleep(5000); |
|
|
|
yoshiBot.jda.awaitReady(); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
YoshiBot yoshiBot = YoshiBot.getInstance(); |
|
|
|
for (Map.Entry<String, List<String>> entry : Resources.getFeedDetails().entrySet()) { |
|
|
|
List<TextChannel> channels = yoshiBot.jda.getTextChannelsByName(entry.getKey(), true); |
|
|
|
if (channels.size() > 0) { |
|
|
@ -143,5 +159,13 @@ public class SauceProvider { |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
private static MessageEmbed getNotFoundEmbed(){ |
|
|
|
if (SauceProvider.notFoundEmbed == null){ |
|
|
|
EmbedBuilder eb = new EmbedBuilder(); |
|
|
|
eb.setTitle("Could not find any posts matching the filter!"); |
|
|
|
SauceProvider.notFoundEmbed = eb.build(); |
|
|
|
} |
|
|
|
return SauceProvider.notFoundEmbed; |
|
|
|
} |
|
|
|
|
|
|
|
} |