Search in sources :

Example 1 with CommentStream

use of net.dean.jraw.paginators.CommentStream 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)

Aggregations

Blueprint (com.demod.fbsr.Blueprint)1 ImmutableList (com.google.common.collect.ImmutableList)1 SimpleEntry (java.util.AbstractMap.SimpleEntry)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 ApiException (net.dean.jraw.ApiException)1 Comment (net.dean.jraw.models.Comment)1 Listing (net.dean.jraw.models.Listing)1 CommentStream (net.dean.jraw.paginators.CommentStream)1