Search in sources :

Example 1 with FfzEmote

use of com.glitchcog.fontificator.emoji.loader.frankerfacez.FfzEmote in project ChatGameFontificator by GlitchCog.

the class EmojiParser method parseFrankerFaceZEmoteJson.

/**
     * Parse emotes loaded using the FrankerFaceZ emote API
     * 
     * @param emoji
     * @param jsonData
     * @param isGlobal
     *            Whether the FFZ emotes to be loaded are the FFZ global emotes
     * @throws IOException
     */
private void parseFrankerFaceZEmoteJson(TypedEmojiMap emoji, String jsonData, boolean isGlobal) throws IOException {
    JsonParser jp = new JsonParser();
    JsonObject root = jp.parse(jsonData).getAsJsonObject();
    if (root.get("error") != null) {
        String errorMessage = "Unable to load FrankerFaceZ emotes";
        if (!root.get("message").isJsonNull()) {
            errorMessage += ": " + root.get("message");
        }
        logBox.log(errorMessage);
        return;
    } else if (root.get("sets").isJsonNull() || (isGlobal && root.get("default_sets").isJsonNull())) {
        logBox.log("Unable to load FrankerFaceZ global emotes");
        return;
    }
    List<String> setsToLoad;
    if (isGlobal) {
        setsToLoad = new ArrayList<String>();
        JsonArray defaultSetsArray = root.get("default_sets").getAsJsonArray();
        for (int i = 0; i < defaultSetsArray.size(); i++) {
            setsToLoad.add(defaultSetsArray.get(i).getAsString());
        }
    } else {
        setsToLoad = null;
    }
    JsonObject sets = root.get("sets").getAsJsonObject();
    Gson gson = new Gson();
    Type emoteType = new TypeToken<FfzEmote[]>() {
    }.getType();
    int frankerCount = 0;
    int eMultiCount = 0;
    List<String> setNames = new ArrayList<String>();
    for (Map.Entry<String, JsonElement> entry : sets.entrySet()) {
        setNames.add(entry.getKey());
        JsonElement emoteElement = entry.getValue().getAsJsonObject().get("emoticons");
        FfzEmote[] jsonEmoteObjects = gson.fromJson(emoteElement, emoteType);
        for (FfzEmote e : jsonEmoteObjects) {
            LazyLoadEmoji lle = null;
            for (String key : e.getUrls().keySet()) {
                lle = new LazyLoadEmoji(e.getName(), "http:" + e.getUrls().get(key), e.getWidth(), e.getHeight(), isGlobal ? EmojiType.FRANKERFACEZ_GLOBAL : EmojiType.FRANKERFACEZ_CHANNEL);
                break;
            }
            if (e.getUrls().size() > 1) {
                eMultiCount++;
            }
            emoji.put(e.getName(), lle);
            frankerCount++;
        }
    }
    String allSets = "";
    for (int n = 0; n < setNames.size(); n++) {
        allSets += (n == 0 ? "" : ", ") + setNames.get(n);
    }
    logBox.log(setNames.size() + " FrankerFaceZ set" + (setNames.size() == 1 ? "" : "s") + " found: {" + allSets + "}");
    logBox.log(frankerCount + " FrankerFaceZ emote" + (frankerCount == 1 ? "" : "s") + " loaded (" + eMultiCount + " multi-image emote" + (eMultiCount == 1 ? "" : "s") + ")");
}
Also used : FfzEmote(com.glitchcog.fontificator.emoji.loader.frankerfacez.FfzEmote) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) JsonArray(com.google.gson.JsonArray) LazyLoadEmoji(com.glitchcog.fontificator.emoji.LazyLoadEmoji) EmojiType(com.glitchcog.fontificator.emoji.EmojiType) UserType(com.glitchcog.fontificator.bot.UserType) Type(java.lang.reflect.Type) JsonElement(com.google.gson.JsonElement) TypedEmojiMap(com.glitchcog.fontificator.emoji.TypedEmojiMap) HashMap(java.util.HashMap) Map(java.util.Map) JsonParser(com.google.gson.JsonParser)

Aggregations

UserType (com.glitchcog.fontificator.bot.UserType)1 EmojiType (com.glitchcog.fontificator.emoji.EmojiType)1 LazyLoadEmoji (com.glitchcog.fontificator.emoji.LazyLoadEmoji)1 TypedEmojiMap (com.glitchcog.fontificator.emoji.TypedEmojiMap)1 FfzEmote (com.glitchcog.fontificator.emoji.loader.frankerfacez.FfzEmote)1 Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1