Search in sources :

Example 1 with Listing

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());
        }
    }
}
Also used : WebhookClient(club.minnced.discord.webhook.WebhookClient) Submission(net.dean.jraw.models.Submission) Listing(net.dean.jraw.models.Listing) SubredditReference(net.dean.jraw.references.SubredditReference) SubredditCache(com.bot.caching.SubredditCache)

Example 2 with Listing

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;
    }
}
Also used : Comment(net.dean.jraw.models.Comment) SimpleEntry(java.util.AbstractMap.SimpleEntry) Blueprint(com.demod.fbsr.Blueprint) LinkedList(java.util.LinkedList) Entry(java.util.Map.Entry) SimpleEntry(java.util.AbstractMap.SimpleEntry) Listing(net.dean.jraw.models.Listing) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) CommentStream(net.dean.jraw.paginators.CommentStream) ApiException(net.dean.jraw.ApiException)

Example 3 with Listing

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;
    }
}
Also used : InboxManager(net.dean.jraw.managers.InboxManager) Message(net.dean.jraw.models.Message) SimpleEntry(java.util.AbstractMap.SimpleEntry) InboxPaginator(net.dean.jraw.paginators.InboxPaginator) Blueprint(com.demod.fbsr.Blueprint) LinkedList(java.util.LinkedList) Entry(java.util.Map.Entry) SimpleEntry(java.util.AbstractMap.SimpleEntry) Listing(net.dean.jraw.models.Listing) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) ApiException(net.dean.jraw.ApiException)

Example 4 with Listing

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();
}
Also used : Submission(net.dean.jraw.models.Submission) Listing(net.dean.jraw.models.Listing) SubredditSort(net.dean.jraw.models.SubredditSort) SubredditReference(net.dean.jraw.references.SubredditReference) SubredditCache(com.bot.caching.SubredditCache)

Example 5 with Listing

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;
    }
}
Also used : Submission(net.dean.jraw.models.Submission) SimpleEntry(java.util.AbstractMap.SimpleEntry) CommentNode(net.dean.jraw.models.CommentNode) Blueprint(com.demod.fbsr.Blueprint) LinkedList(java.util.LinkedList) SubredditPaginator(net.dean.jraw.paginators.SubredditPaginator) Entry(java.util.Map.Entry) SimpleEntry(java.util.AbstractMap.SimpleEntry) Listing(net.dean.jraw.models.Listing) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) ApiException(net.dean.jraw.ApiException)

Aggregations

Listing (net.dean.jraw.models.Listing)5 Blueprint (com.demod.fbsr.Blueprint)3 ImmutableList (com.google.common.collect.ImmutableList)3 SimpleEntry (java.util.AbstractMap.SimpleEntry)3 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3 Entry (java.util.Map.Entry)3 ApiException (net.dean.jraw.ApiException)3 Submission (net.dean.jraw.models.Submission)3 SubredditCache (com.bot.caching.SubredditCache)2 SubredditReference (net.dean.jraw.references.SubredditReference)2 WebhookClient (club.minnced.discord.webhook.WebhookClient)1 InboxManager (net.dean.jraw.managers.InboxManager)1 Comment (net.dean.jraw.models.Comment)1 CommentNode (net.dean.jraw.models.CommentNode)1 Message (net.dean.jraw.models.Message)1 SubredditSort (net.dean.jraw.models.SubredditSort)1 CommentStream (net.dean.jraw.paginators.CommentStream)1 InboxPaginator (net.dean.jraw.paginators.InboxPaginator)1