use of net.dean.jraw.paginators.InboxPaginator 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;
}
}
Aggregations