Search in sources :

Example 1 with Member

use of me.perrycate.groupmeutils.data.Member in project groupme-utils by TheGuyWithTheFace.

the class GroupDeserializer method deserialize.

@Override
public Group deserialize(JsonElement jsonElement, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject json = getResponse(jsonElement).getAsJsonObject();
    GroupBuilder g = new GroupBuilder();
    g.setId(json.get("id").getAsString());
    g.setName(json.get("name").getAsString());
    g.setType(json.get("type").getAsString());
    g.setDescription(json.get("description").getAsString());
    if (!json.get("image_url").isJsonNull()) {
        g.setImageUrl(json.get("image_url").getAsString());
    }
    g.setCreatorUserId(json.get("creator_user_id").getAsString());
    long createTime = json.get("created_at").getAsLong();
    g.setCreatedAt(Instant.ofEpochSecond(createTime));
    long updateTime = json.get("updated_at").getAsLong();
    g.setUpdatedAt(Instant.ofEpochSecond(updateTime));
    if (!json.get("share_url").isJsonNull()) {
        g.setShareUrl(json.get("share_url").getAsString());
    }
    // Get members of the group
    JsonArray response = json.get("members").getAsJsonArray();
    int numMembers = response.size();
    Member[] members = new Member[numMembers];
    for (int i = 0; i < numMembers; i++) {
        JsonElement elem = response.get(i);
        members[i] = context.deserialize(elem, Member.class);
    }
    g.setMembers(members);
    // TODO this will be reworked later, see note in Group Class.
    JsonObject messages = json.get("messages").getAsJsonObject();
    g.setMessageCount(messages.get("count").getAsInt());
    g.setLastMessageId(messages.get("last_message_id").getAsString());
    long lastCreate = messages.get("last_message_created_at").getAsLong();
    g.setLastMessageCreatedAt(Instant.ofEpochSecond(lastCreate));
    return g.createGroup();
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) GroupBuilder(me.perrycate.groupmeutils.data.GroupBuilder) JsonObject(com.google.gson.JsonObject) Member(me.perrycate.groupmeutils.data.Member)

Example 2 with Member

use of me.perrycate.groupmeutils.data.Member 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)

Example 3 with Member

use of me.perrycate.groupmeutils.data.Member in project groupme-utils by TheGuyWithTheFace.

the class ListGroups method main.

public static void main(String[] args) {
    System.out.println("Please enter your groupme API Token: ");
    Scanner s = new Scanner(System.in);
    String token = s.nextLine();
    System.out.println("Fetching groups...");
    GroupMe groupme = new GroupMe(token);
    Group[] groups = groupme.getGroups();
    for (int i = 0; i < groups.length; i++) {
        System.out.println("Name: " + groups[i].getName());
        System.out.print("Members: ");
        Member[] members = groups[i].getMembers();
        for (int j = 0; j < members.length; j++) {
            System.out.print(members[j].getNickname() + " ");
        }
        System.out.println();
    }
    s.close();
}
Also used : Scanner(java.util.Scanner) Group(me.perrycate.groupmeutils.data.Group) GroupMe(me.perrycate.groupmeutils.api.GroupMe) Member(me.perrycate.groupmeutils.data.Member)

Aggregations

Member (me.perrycate.groupmeutils.data.Member)3 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 LocalDate (java.time.LocalDate)1 HashMap (java.util.HashMap)1 Scanner (java.util.Scanner)1 GroupMe (me.perrycate.groupmeutils.api.GroupMe)1 MessageIterator (me.perrycate.groupmeutils.api.MessageIterator)1 Group (me.perrycate.groupmeutils.data.Group)1 GroupBuilder (me.perrycate.groupmeutils.data.GroupBuilder)1 Message (me.perrycate.groupmeutils.data.Message)1