use of net.dean.jraw.models.Listing in project Vinny by kikkia.
the class RedditHelper method getRandomSubmissionAndSend.
public static void getRandomSubmissionAndSend(RedditConnection redditConnection, CommandEvent commandEvent, SubredditSort sortType, TimePeriod timePeriod, int limit, boolean isChannelNSFW, String subredditName) throws Exception {
// If the subreddit name contains an invalid character throw a error response
if (!subredditName.matches("^[^<>@!#$%^&*() ,.=+;]+$")) {
commandEvent.reply(commandEvent.getClient().getError() + " Invalid subreddit. Please ensure you are using only the name with no symbols.");
return;
}
SubredditReference subreddit = redditConnection.getClient().subreddit(subredditName);
if (!isChannelNSFW && subreddit.about().isNsfw()) {
commandEvent.reply(commandEvent.getClient().getWarning() + " NSFW subreddit detected and NSFW is not enabled on this channel. " + "Please make sure that nsfw is enabled in the discord channel settings");
return;
}
DefaultPaginator<Submission> paginator = subreddit.posts().limit(limit).timePeriod(timePeriod).sorting(sortType).build();
SubredditCache cache = SubredditCache.getInstance();
List<Listing<Submission>> submissions = cache.get(sortType.toString() + subredditName);
if (submissions == null) {
submissions = paginator.accumulate(1);
cache.put(sortType.toString() + subredditName, submissions);
}
// Get the only page
Listing<Submission> page = submissions.get(0);
// Get random child post from the page
Submission submission = getRandomSubmission(page, false);
boolean isNsfwSubmission = submission.isNsfw();
if (isNsfwSubmission && !isChannelNSFW) {
// Submission is nsfw but sub is not. Try 10 times to find non nsfw-post
int tries = 0;
while (isNsfwSubmission) {
submission = getRandomSubmission(page, false);
isNsfwSubmission = submission.isNsfw();
tries++;
if (tries == 10) {
commandEvent.reply(commandEvent.getClient().getWarning() + " I only found NSFW posts and NSFW is not enabled on this channel. " + "Please make sure that nsfw is enabled in the discord channel settings.");
return;
}
}
}
// Scheduled commands generate an insane amount of traffic, lets send them to webhooks to help with global ratelimiting
if (ScheduledCommandUtils.isScheduled(commandEvent)) {
WebhookClient client = ScheduledCommandUtils.getWebhookForChannel(commandEvent);
client.send(buildWebhookEmbedMessageForSubmission(commandEvent, submission));
client.send(buildWebhookMessageForSubmission(commandEvent, submission));
} else {
// Send the embed, content will be sent separatly below
commandEvent.reply(buildEmbedForSubmission(submission));
String text = submission.getSelfText();
if (submission.isSelfPost() && text != null && !text.isEmpty()) {
// Since discord only allows us to send 2000 characters we need to break long posts down
if (text.length() > 1900) {
// Split message into parts and send them all separately
for (String part : FormattingUtils.splitTextIntoChunksByWords(text, 1500)) {
commandEvent.reply("```" + part + "```");
}
} else {
// Its short enough so just send it
commandEvent.reply("```" + text + " ```");
}
} else {
commandEvent.reply(submission.getUrl());
}
}
}
use of net.dean.jraw.models.Listing in project Factorio-FBSR by demodude4u.
the class BlueprintBotRedditService method processNewComments.
private boolean processNewComments(JSONObject cacheJson, String subreddit, long ageLimitMillis, Optional<WatchdogService> watchdog) throws ApiException, IOException {
long lastProcessedMillis = cacheJson.optLong("lastProcessedCommentMillis-" + subreddit);
CommentStream commentStream = new CommentStream(reddit, subreddit);
commentStream.setTimePeriod(TimePeriod.ALL);
commentStream.setSorting(Sorting.NEW);
int processedCount = 0;
long newestMillis = lastProcessedMillis;
List<Entry<Comment, String>> pendingReplies = new LinkedList<>();
paginate: for (Listing<Comment> listing : commentStream) {
for (Comment comment : listing) {
long createMillis = comment.getCreated().getTime();
if (createMillis <= lastProcessedMillis || (System.currentTimeMillis() - createMillis > ageLimitMillis)) {
break paginate;
}
processedCount++;
newestMillis = Math.max(newestMillis, createMillis);
if (comment.getAuthor().equals(myUserName)) {
break paginate;
}
if (comment.isArchived()) {
continue;
}
List<String> responses = processContent(comment.getBody(), getPermaLink(comment), comment.getSubredditName(), comment.getAuthor(), watchdog);
for (String response : responses) {
pendingReplies.add(new SimpleEntry<>(comment, response));
}
}
}
for (Entry<Comment, String> pair : pendingReplies) {
System.out.println("IM TRYING TO REPLY TO A COMMENT!");
String message = pair.getValue();
if (message.length() > 10000) {
message = WebUtils.uploadToHostingService("MESSAGE_TOO_LONG.txt", message.getBytes()).toString();
}
while (true) {
try {
account.reply(pair.getKey(), message);
break;
} catch (ApiException e) {
if (e.getReason().equals("RATELIMIT")) {
System.out.println("RATE LIMITED! WAITING 6 MINUTES...");
Uninterruptibles.sleepUninterruptibly(6, TimeUnit.MINUTES);
} else {
throw e;
}
}
}
}
if (processedCount > 0) {
System.out.println("Processed " + processedCount + " comment(s) from /r/" + subreddit);
cacheJson.put("lastProcessedCommentMillis-" + subreddit, newestMillis);
return true;
} else {
return false;
}
}
use of net.dean.jraw.models.Listing in project Factorio-FBSR by demodude4u.
the class BlueprintBotRedditService method processNewMessages.
private boolean processNewMessages(JSONObject cacheJson, long ageLimitMillis, Optional<WatchdogService> watchdog) throws ApiException, IOException {
long lastProcessedMillis = cacheJson.getLong("lastProcessedMessageMillis");
InboxPaginator paginator = new InboxPaginator(reddit, "messages");
paginator.setTimePeriod(TimePeriod.ALL);
paginator.setSorting(Sorting.NEW);
int processedCount = 0;
long newestMillis = lastProcessedMillis;
List<Entry<Message, String>> pendingReplies = new LinkedList<>();
List<Message> processedMessages = new LinkedList<>();
paginate: for (Listing<Message> listing : paginator) {
for (Message message : listing) {
if (message.isRead()) {
break paginate;
}
long createMillis = message.getCreated().getTime();
if (createMillis <= lastProcessedMillis || (System.currentTimeMillis() - createMillis > ageLimitMillis)) {
break paginate;
}
processedCount++;
newestMillis = Math.max(newestMillis, createMillis);
processedMessages.add(message);
List<String> responses = processContent(message.getBody(), getPermaLink(message), "(Private)", message.getAuthor(), watchdog);
for (String response : responses) {
pendingReplies.add(new SimpleEntry<>(message, response));
}
}
}
if (!processedMessages.isEmpty()) {
new InboxManager(reddit).setRead(true, processedMessages.get(0), processedMessages.stream().skip(1).toArray(Message[]::new));
}
for (Entry<Message, String> pair : pendingReplies) {
System.out.println("IM TRYING TO REPLY TO A MESSAGE!");
String message = pair.getValue();
if (message.length() > 10000) {
message = WebUtils.uploadToHostingService("MESSAGE_TOO_LONG.txt", message.getBytes()).toString();
}
while (true) {
try {
account.reply(pair.getKey(), message);
break;
} catch (ApiException e) {
if (e.getReason().equals("RATELIMIT")) {
System.out.println("RATE LIMITED! WAITING 6 MINUTES...");
Uninterruptibles.sleepUninterruptibly(6, TimeUnit.MINUTES);
} else {
throw e;
}
}
}
}
if (processedCount > 0) {
System.out.println("Processed " + processedCount + " message(s)");
cacheJson.put("lastProcessedMessageMillis", newestMillis);
return true;
} else {
return false;
}
}
use of net.dean.jraw.models.Listing in project Vinny by kikkia.
the class RedditHelper method getRandomCopyPasta.
public static String getRandomCopyPasta(RedditConnection redditConnection) {
String subredditName = "copypasta";
SubredditReference subreddit = redditConnection.getClient().subreddit(subredditName);
SubredditSort sortType = SubredditSort.TOP;
DefaultPaginator<Submission> paginator = subreddit.posts().limit(1000).timePeriod(TimePeriod.ALL).sorting(sortType).build();
SubredditCache cache = SubredditCache.getInstance();
List<Listing<Submission>> submissions = cache.get(sortType.toString() + subredditName);
if (submissions == null) {
submissions = paginator.accumulate(1);
cache.put(sortType.toString() + subredditName, submissions);
}
// Get the only page
Listing<Submission> page = submissions.get(0);
// Get random child post from the page
Submission submission = getRandomSubmission(page, false);
return submission.getSelfText();
}
use of net.dean.jraw.models.Listing in project Factorio-FBSR by demodude4u.
the class BlueprintBotRedditService method processNewSubmissions.
private boolean processNewSubmissions(JSONObject cacheJson, String subreddit, long ageLimitMillis, Optional<WatchdogService> watchdog) throws NetworkException, ApiException, IOException {
long lastProcessedMillis = cacheJson.optLong("lastProcessedSubmissionMillis-" + subreddit);
SubredditPaginator paginator = new SubredditPaginator(reddit, subreddit);
paginator.setTimePeriod(TimePeriod.ALL);
paginator.setSorting(Sorting.NEW);
int processedCount = 0;
long newestMillis = lastProcessedMillis;
List<Entry<Submission, String>> pendingReplies = new LinkedList<>();
paginate: for (Listing<Submission> listing : paginator) {
for (Submission submission : listing) {
long createMillis = submission.getCreated().getTime();
if (createMillis <= lastProcessedMillis || (System.currentTimeMillis() - createMillis > ageLimitMillis)) {
break paginate;
}
processedCount++;
newestMillis = Math.max(newestMillis, createMillis);
if (!submission.isSelfPost() || submission.isLocked() || submission.isArchived()) {
continue;
}
CommentNode comments = submission.getComments();
if (comments == null && submission.getCommentCount() > 0) {
submission = reddit.getSubmission(submission.getId());
comments = submission.getComments();
}
if (comments != null && getMyReply(comments).isPresent()) {
break paginate;
}
List<String> responses = processContent(submission.getSelftext(), submission.getUrl(), submission.getSubredditName(), submission.getAuthor(), watchdog);
for (String response : responses) {
pendingReplies.add(new SimpleEntry<>(submission, response));
}
}
}
for (Entry<Submission, String> pair : pendingReplies) {
System.out.println("IM TRYING TO REPLY TO A SUBMISSION!");
String message = pair.getValue();
if (message.length() > 10000) {
message = WebUtils.uploadToHostingService("MESSAGE_TOO_LONG.txt", message.getBytes()).toString();
}
while (true) {
try {
account.reply(pair.getKey(), message);
break;
} catch (ApiException e) {
if (e.getReason().equals("RATELIMIT")) {
System.out.println("RATE LIMITED! WAITING 6 MINUTES...");
Uninterruptibles.sleepUninterruptibly(6, TimeUnit.MINUTES);
} else {
throw e;
}
}
}
}
if (processedCount > 0) {
System.out.println("Processed " + processedCount + " submission(s) from /r/" + subreddit);
cacheJson.put("lastProcessedSubmissionMillis-" + subreddit, newestMillis);
return true;
} else {
return false;
}
}
Aggregations