Search in sources :

Example 1 with MessageIterator

use of me.perrycate.groupmeutils.api.MessageIterator in project groupme-utils by TheGuyWithTheFace.

the class Dumper method dump.

/**
     * Dumps the group to output in order, with most recent messages appearing
     * at the bottom.
     */
public int dump(File outputFile) {
    // TODO check that outputFile does not already exist, and if it does
    // prompt user to confirm they want to overwrite it. (Then actually
    // overwrite it, currently we just append.)
    // Actually, that'd be a good way to check to see whether we should use
    // dump() or append(), if we did that check elsewhere. We'd have to have
    // better error handling if the file doesn't match the group, the file
    // is ill-formated (or just plain not a log file), etc, in case the user
    // just accidentally specifies appending at some random-ass file we know
    // nothing about.
    int messagesDumped = 0;
    try (OutputStream o = Files.newOutputStream(outputFile.toPath(), StandardOpenOption.APPEND, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
        BufferedOutputStream output = new BufferedOutputStream(o)) {
        MessageIterator messages = groupme.getAllMessages(groupId);
        while (messages.hasNext()) {
            output.write((format(messages.next()) + '\n').getBytes());
            messagesDumped++;
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return messagesDumped;
}
Also used : MessageIterator(me.perrycate.groupmeutils.api.MessageIterator) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 2 with MessageIterator

use of me.perrycate.groupmeutils.api.MessageIterator in project groupme-utils by TheGuyWithTheFace.

the class Stats method write.

public void write() {
    // Create header
    HashMap<String, String> entries = new HashMap<>();
    for (Member m : group.getMembers()) {
        entries.put(getPostsKey(m.getUserId()), "Posts by " + m.getNickname());
    }
    // Groupme has a quirk where meta messages ("so and so left, joined,
    // etc") are reported by a user with ID system, but system is not
    // listed as a user.
    entries.put(getPostsKey("system"), "System posts");
    writer.addRow(entries);
    // Collect data
    MessageIterator messages = api.getAllMessages(group.getId());
    Message currentMessage = messages.next();
    LocalDate day = LocalDateTime.ofInstant(currentMessage.getCreatedAt(), this.timeZone).toLocalDate();
    HashMap<String, Integer> data = new HashMap<>();
    while (messages.hasNext()) {
        currentMessage = messages.next();
        // Start new day if necessary
        LocalDate currentDay = LocalDateTime.ofInstant(currentMessage.getCreatedAt(), this.timeZone).toLocalDate();
        long daysElapsed = ChronoUnit.DAYS.between(day, currentDay);
        if (daysElapsed != 0) {
            // Finish writing previous day
            writer.addRow(data);
            // If multiple days elapsed, write empty rows
            data = new HashMap<>();
            for (int i = 0; i < daysElapsed - 1; i++) {
                writer.addRow(data);
            }
            // Start new day
            data = new HashMap<>();
            day = currentDay;
        }
        // Add data for this message
        incrementPosts(currentMessage.getUserId(), data);
        if (currentMessage.getFavoritedBy() != null)
            incrementLikes(currentMessage.getFavoritedBy(), data);
    }
    // Add final row
    writer.addRow(data);
    // Write and finish
    writer.writeTo(outputFile, false);
    System.out.println("Finished!");
}
Also used : Message(me.perrycate.groupmeutils.data.Message) HashMap(java.util.HashMap) MessageIterator(me.perrycate.groupmeutils.api.MessageIterator) Member(me.perrycate.groupmeutils.data.Member) LocalDate(java.time.LocalDate)

Aggregations

MessageIterator (me.perrycate.groupmeutils.api.MessageIterator)2 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 LocalDate (java.time.LocalDate)1 HashMap (java.util.HashMap)1 Member (me.perrycate.groupmeutils.data.Member)1 Message (me.perrycate.groupmeutils.data.Message)1