Search in sources :

Example 1 with InboxPaginator

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

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 InboxManager (net.dean.jraw.managers.InboxManager)1 Listing (net.dean.jraw.models.Listing)1 Message (net.dean.jraw.models.Message)1 InboxPaginator (net.dean.jraw.paginators.InboxPaginator)1