Search in sources :

Example 1 with Emoticon

use of chatty.util.api.Emoticon in project chatty by chatty.

the class BTTVEmotes method parseEmotes.

/**
 * Parse emotes from the given JSON.
 *
 * @param json
 * @param channelRestriction
 * @return
 */
private static Set<Emoticon> parseEmotes(String json, String channelRestriction) {
    Set<Emoticon> emotes = new HashSet<>();
    if (json == null) {
        return emotes;
    }
    JSONParser parser = new JSONParser();
    try {
        JSONObject root = (JSONObject) parser.parse(json);
        String urlTemplate = (String) root.get("urlTemplate");
        if (urlTemplate == null || urlTemplate.isEmpty()) {
            LOGGER.warning("No URL Template");
            return emotes;
        }
        JSONArray emotesArray = (JSONArray) root.get("emotes");
        for (Object o : emotesArray) {
            if (o instanceof JSONObject) {
                Emoticon emote = parseEmote((JSONObject) o, urlTemplate, channelRestriction);
                if (emote != null) {
                    emotes.add(emote);
                }
            }
        }
    } catch (ParseException | ClassCastException ex) {
        // ClassCastException is also caught in parseEmote(), so it won't
        // quit completely when one emote is invalid.
        LOGGER.warning("BTTV: Error parsing emotes: " + ex);
    }
    return emotes;
}
Also used : Emoticon(chatty.util.api.Emoticon) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException) HashSet(java.util.HashSet)

Example 2 with Emoticon

use of chatty.util.api.Emoticon in project chatty by chatty.

the class BTTVEmotes method parseEmote.

/**
 * Parse a single emote from the given JSONObject.
 *
 * @param o The object containing the emote info
 * @param urlTemplate The URL Template to use for this emote
 * @param channelRestriction The channel restriction to use
 * @return
 */
private static Emoticon parseEmote(JSONObject o, String urlTemplate, String channelRestriction) {
    try {
        String url = urlTemplate;
        String code = (String) o.get("code");
        String sourceChannel = (String) o.get("channel");
        String id = (String) o.get("id");
        String imageType = null;
        if (o.get("imageType") instanceof String) {
            imageType = (String) o.get("imageType");
        }
        if (code == null || code.isEmpty() || id == null || id.isEmpty()) {
            return null;
        }
        Emoticon.Builder builder = new Emoticon.Builder(Emoticon.Type.BTTV, code, url);
        builder.setCreator(sourceChannel);
        builder.setLiteral(true);
        builder.setStringId(id);
        if (channelRestriction != null) {
            builder.addStreamRestriction(channelRestriction);
            builder.setStream(channelRestriction);
        }
        if (imageType != null && imageType.equals("gif")) {
            builder.setAnimated(true);
        }
        // Adds restrictions to emote (if present)
        Object restriction = o.get("restrictions");
        if (restriction != null && restriction instanceof JSONObject) {
            JSONObject restrictions = (JSONObject) restriction;
            for (Object r : restrictions.keySet()) {
                boolean knownAndValid = addRestriction(r, restrictions, builder);
                // Don't add emotes with unknown or invalid restrictions
                if (!knownAndValid) {
                    return null;
                }
            }
        }
        return builder.build();
    } catch (ClassCastException | NullPointerException ex) {
        LOGGER.warning("BTTV: Error parsing emote: " + o + " [" + ex + "]");
        return null;
    }
}
Also used : Emoticon(chatty.util.api.Emoticon) JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject)

Example 3 with Emoticon

use of chatty.util.api.Emoticon in project chatty by chatty.

the class TwitchClient method commandFFZ.

private void commandFFZ(String channel) {
    Set<Emoticon> output;
    StringBuilder b = new StringBuilder();
    if (channel == null) {
        b.append("Global FFZ emotes: ");
        output = Emoticons.filterByType(g.emoticons.getGlobalTwitchEmotes(), Emoticon.Type.FFZ);
    } else {
        b.append("This channel's FFZ emotes: ");
        Set<Emoticon> emotes = g.emoticons.getEmoticons(Helper.toStream(channel));
        output = Emoticons.filterByType(emotes, Emoticon.Type.FFZ);
    }
    if (output.isEmpty()) {
        b.append("None found.");
    }
    String sep = "";
    for (Emoticon emote : output) {
        b.append(sep);
        b.append(emote.code);
        sep = ", ";
    }
    g.printLine(roomManager.getRoom(channel), b.toString());
}
Also used : CheerEmoticon(chatty.util.api.CheerEmoticon) Emoticon(chatty.util.api.Emoticon)

Example 4 with Emoticon

use of chatty.util.api.Emoticon in project chatty by chatty.

the class FrankerFaceZ method parseResult.

private void parseResult(Type type, String stream, String result) {
    if (result == null) {
        return;
    }
    if (type == Type.FEATURE_FRIDAY && stream == null) {
        // Response of the first request having only the info which channel
        handleFeatureFriday(result);
        return;
    }
    // Determine whether these emotes should be global
    final boolean global = type == Type.GLOBAL || type == Type.FEATURE_FRIDAY;
    String globalText = global ? "global" : "local";
    Set<Emoticon> emotes = new HashSet<>();
    List<Usericon> usericons = new ArrayList<>();
    // Parse depending on type
    if (type == Type.GLOBAL) {
        emotes = FrankerFaceZParsing.parseGlobalEmotes(result);
    } else if (type == Type.ROOM) {
        emotes = FrankerFaceZParsing.parseRoomEmotes(result);
        Usericon modIcon = FrankerFaceZParsing.parseModIcon(result);
        if (modIcon != null) {
            usericons.add(modIcon);
        }
    } else if (type == Type.FEATURE_FRIDAY) {
        emotes = FrankerFaceZParsing.parseSetEmotes(result, Emoticon.SubType.FEATURE_FRIDAY, null);
        for (Emoticon emote : emotes) {
            if (featureFridayChannel != null) {
                emote.setStream(featureFridayChannel);
            }
        }
    }
    LOGGER.info("[FFZ] (" + stream + ", " + globalText + "): " + emotes.size() + " emotes received.");
    if (!usericons.isEmpty()) {
        LOGGER.info("[FFZ] (" + stream + "): " + usericons.size() + " usericons received.");
    }
    // Package accordingly and send the result to the listener
    EmoticonUpdate emotesUpdate;
    if (type == Type.FEATURE_FRIDAY) {
        emotesUpdate = new EmoticonUpdate(emotes, Emoticon.Type.FFZ, Emoticon.SubType.FEATURE_FRIDAY, null);
    } else {
        emotesUpdate = new EmoticonUpdate(emotes);
    }
    listener.channelEmoticonsReceived(emotesUpdate);
    // Return icons if mod icon was found (will be empty otherwise)
    listener.usericonsReceived(usericons);
}
Also used : Emoticon(chatty.util.api.Emoticon) Usericon(chatty.util.api.usericons.Usericon) EmoticonUpdate(chatty.util.api.EmoticonUpdate)

Example 5 with Emoticon

use of chatty.util.api.Emoticon in project chatty by chatty.

the class FrankerFaceZParsing method parseEmote.

/**
 * Parses a single emote. Required for creating an emote are the emote code,
 * the id and the x1 URL.
 *
 * @param emote The emote data as a JSONObject
 * @param streamRestriction The stream restriction to use for this emote,
 * can be null if the emote is global
 * @param info The info to set for this emote, can be null if no info should
 * be set
 * @param subType The subType to be set for the Emoticon
 * @return The Emoticon object or null if an error occured
 */
public static Emoticon parseEmote(JSONObject emote, String streamRestriction, String info, Emoticon.SubType subType) {
    try {
        // Base information
        int width = JSONUtil.getInteger(emote, "width", -1);
        int height = JSONUtil.getInteger(emote, "height", -1);
        String code = (String) emote.get("name");
        JSONObject urls = (JSONObject) emote.get("urls");
        String url1 = (String) urls.get("1");
        String url2 = (String) urls.get("2");
        int id = ((Number) emote.get("id")).intValue();
        // Creator
        Object owner = emote.get("owner");
        String creator = null;
        if (owner != null && owner instanceof JSONObject) {
            creator = (String) ((JSONObject) owner).get("display_name");
        }
        // Check if required data is there
        if (code == null || code.isEmpty()) {
            return null;
        }
        if (url1 == null || url1.isEmpty()) {
            return null;
        }
        Emoticon.Builder b = new Emoticon.Builder(Emoticon.Type.FFZ, code, url1);
        b.setX2Url(url2);
        b.setSize(width, height);
        b.setCreator(creator);
        b.setNumericId(id);
        b.addStreamRestriction(streamRestriction);
        b.addInfo(info);
        b.setSubType(subType);
        return b.build();
    } catch (ClassCastException | NullPointerException ex) {
        LOGGER.warning("Error parsing FFZ emote: " + ex);
        return null;
    }
}
Also used : Emoticon(chatty.util.api.Emoticon) JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject)

Aggregations

Emoticon (chatty.util.api.Emoticon)13 JSONObject (org.json.simple.JSONObject)5 CheerEmoticon (chatty.util.api.CheerEmoticon)4 HashSet (java.util.HashSet)3 JSONParser (org.json.simple.parser.JSONParser)3 EmoticonUpdate (chatty.util.api.EmoticonUpdate)2 Emoteset (chatty.util.TwitchEmotes.Emoteset)1 UrlRequest (chatty.util.UrlRequest)1 FullResult (chatty.util.UrlRequest.FullResult)1 Emoticons (chatty.util.api.Emoticons)1 Usericon (chatty.util.api.usericons.Usericon)1 Matcher (java.util.regex.Matcher)1 JSONArray (org.json.simple.JSONArray)1 ParseException (org.json.simple.parser.ParseException)1 Test (org.junit.Test)1