Search in sources :

Example 11 with Emoticon

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

the class FrankerFaceZTest method testParseEmote.

@Test
public void testParseEmote() throws Exception {
    JSONParser parser = new JSONParser();
    JSONObject obj = (JSONObject) parser.parse(loadJSON("FFZ_emote_regular"));
    Emoticon emote = FrankerFaceZParsing.parseEmote(obj, null, null, null);
    assertNotNull(emote);
    assertEquals(emote.code, "joshWASTED");
    assertEquals(emote.creator, "Joshimuz");
    assertEquals(emote.getWidth(), 100);
    assertEquals(emote.getHeight(), 16);
    obj = (JSONObject) parser.parse(loadJSON("FFZ_emote_no_height"));
    emote = FrankerFaceZParsing.parseEmote(obj, null, null, null);
    assertNotNull(emote);
    assertEquals(emote.code, "joshWASTED");
    assertEquals(emote.creator, "Joshimuz");
    assertEquals(emote.getWidth(), 100);
    assertEquals(emote.getHeight(), -1);
    testParseEmoteError("FFZ_emote_id_string");
}
Also used : Emoticon(chatty.util.api.Emoticon) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) Test(org.junit.Test)

Example 12 with Emoticon

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

the class ChannelTextPane method findEmoticons.

private void findEmoticons(User user, Set<Emoticon> emoticons, String text, Map<Integer, Integer> ranges, Map<Integer, MutableAttributeSet> rangesStyle) {
    // Find emoticons
    for (Emoticon emoticon : emoticons) {
        // Check the text for every single emoticon
        if (!emoticon.matchesUser(user)) {
            continue;
        }
        if (main.emoticons.isEmoteIgnored(emoticon)) {
            continue;
        }
        if (emoticon.isAnimated && !styles.isEnabled(Setting.EMOTICONS_SHOW_ANIMATED)) {
            continue;
        }
        Matcher m = emoticon.getMatcher(text);
        while (m.find()) {
            // As long as this emoticon is still found in the text, add
            // it's position (if it doesn't overlap with something already
            // found) and move on
            int start = m.start();
            int end = m.end() - 1;
            addEmoticon(emoticon, start, end, ranges, rangesStyle);
        }
    }
}
Also used : CheerEmoticon(chatty.util.api.CheerEmoticon) Emoticon(chatty.util.api.Emoticon) Matcher(java.util.regex.Matcher)

Example 13 with Emoticon

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

the class ChannelTextPane method addTwitchTagsEmoticons.

/**
 * Adds the emoticons from the Twitch IRCv3 tags.
 *
 * @param emoticons Map of emotes associated with Twitch emote id
 * @param text The message text
 * @param ranges The ranges for this message
 * @param rangesStyle The styles for this message
 * @param emotesDef The emotes definition from the IRCv3 tags
 */
private void addTwitchTagsEmoticons(User user, Map<Integer, Emoticon> emoticons, String text, Map<Integer, Integer> ranges, Map<Integer, MutableAttributeSet> rangesStyle, TagEmotes emotesDef) {
    if (emotesDef == null) {
        return;
    }
    Map<Integer, Emoticons.TagEmote> def = emotesDef.emotes;
    /**
     * Iterate over each character of the message and check if an emote starts
     * at the current position.
     *
     * The offset is used to handle supplemantary characters that consist
     * of two UTF-16 characters. Twitch Chat sees these as only one character
     * so that has to be corrected.
     *
     * https://discuss.dev.twitch.tv/t/jtv-2-receiving-messages/1635/10
     *
     * Example message: "Kappa 𠜎 Kappa"
     */
    int offset = 0;
    for (int i = 0; i < text.length(); ) {
        if (def.containsKey(i - offset)) {
            // An emote starts at the current position, so add it.
            Emoticons.TagEmote emoteData = def.get(i - offset);
            int id = emoteData.id;
            int start = i;
            int end = emoteData.end + offset;
            // Get and check emote
            Emoticon emoticon = null;
            Emoticon customEmote = main.emoticons.getCustomEmoteById(id);
            if (customEmote != null && customEmote.allowedForStream(user.getStream())) {
                emoticon = customEmote;
            } else {
                emoticon = emoticons.get(id);
            }
            if (end < text.length()) {
                if (emoticon == null) {
                    /**
                     * Add emote from message alone
                     */
                    String code = text.substring(start, end + 1);
                    String url = Emoticon.getTwitchEmoteUrlById(id, 1);
                    Emoticon.Builder b = new Emoticon.Builder(Emoticon.Type.TWITCH, code, url);
                    b.setNumericId(id);
                    Emoteset emotesetInfo = main.emoticons.getInfoByEmoteId(id);
                    if (emotesetInfo != null) {
                        b.setEmoteset(emotesetInfo.emoteset_id);
                        b.setStream(emotesetInfo.stream);
                        b.setEmotesetInfo(emotesetInfo.product);
                    } else {
                        b.setEmoteset(Emoticon.SET_UNKNOWN);
                    }
                    emoticon = b.build();
                    main.emoticons.addTempEmoticon(emoticon);
                    LOGGER.info("Added emote from message: " + emoticon);
                }
                if (!main.emoticons.isEmoteIgnored(emoticon)) {
                    addEmoticon(emoticon, start, end, ranges, rangesStyle);
                }
            }
        }
        /**
         * If the current position in the String consists of an character
         * thats more than one long (some Unicode characters), then add to
         * the offset and jump ahead accordingly.
         */
        offset += Character.charCount(text.codePointAt(i)) - 1;
        i += Character.charCount(text.codePointAt(i));
    }
}
Also used : CheerEmoticon(chatty.util.api.CheerEmoticon) Emoticon(chatty.util.api.Emoticon) Emoticons(chatty.util.api.Emoticons) Emoteset(chatty.util.TwitchEmotes.Emoteset)

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