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;
}
}
Aggregations