Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ Include the following in your pom.xml
<dependency>
<groupId>com.github.gikkman</groupId>
<artifactId>Java-Twirk</artifactId>
<version>0.6</version>
<version>0.6.1</version>
</dependency>
</dependencies>
```
Or simply download the latest version of the library jar from the release page.

## Changes
### 0.6.1
Hotfix release since some emote IDs were still not parsed correctly (see #22). This hotfix should hopefully fix this issue.
Please report any further issues with parsing emotes.

### 0.6
There has only been minor changes between 0.5 and 0.6. Nothing that should break backwards compatibility. Fixes include:
* Fixed NumberFormatException on modified emotes
* Twitch changed the emote IDs from always being a number to sometimes being a number_string -.-' Its fixed now
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/gikk/twirk/types/emote/Emote.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@
*
*/
public interface Emote {


/** Fetches the emotes numeric ID.
*
* @return The emotes numeric ID
* @deprecated use {@link #getEmoteIDString()} instead
*/
public int getEmoteID();


/** Fetches the emotes ID as String. This became necessary after Twitch start
* including other characters than numbers in emote IDs
*
* @return The emotes ID
*/
public String getEmoteIDString();

/** A list of pairs on indices. Each pair is a BEGIN-END pair of a emote occurrence in the message.<br><br>
*
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/gikk/twirk/types/emote/EmoteImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ class EmoteImpl implements Emote{
private final static String EMOTE_URL_BASE = "http://static-cdn.jtvnw.net/emoticons/v1/";

private int emoteID;
private String emoteIDString;
private final LinkedList<EmoteIndices> indices = new LinkedList<>();
private String pattern;

public EmoteImpl setEmoteIDString(String emoteIDString){
this.emoteIDString = emoteIDString;
return this;
}

public EmoteImpl setEmoteID(int emoteID){
this.emoteID = emoteID;
return this;
Expand All @@ -31,6 +37,11 @@ public int getEmoteID() {
return emoteID;
}

@Override
public String getEmoteIDString() {
return emoteIDString;
}

@Override
public String getPattern() {
return pattern;
Expand All @@ -50,7 +61,7 @@ public String getEmoteImageUrl(EMOTE_SIZE imageSize){

@Override
public String toString(){
StringBuilder out = new StringBuilder(emoteID + " " + ( pattern == null ? "NULL" : pattern) + "[ ");
StringBuilder out = new StringBuilder(emoteIDString + " " + ( pattern == null ? "NULL" : pattern) + "[ ");

for( EmoteIndices index : indices ) {
out.append(index.toString());
Expand Down
25 changes: 22 additions & 3 deletions src/main/java/com/gikk/twirk/types/emote/EmoteParserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmoteParserImpl implements EmoteParser {
private static Pattern integerIdPattern = Pattern.compile("([0-9]+)");

@Override
public List<Emote> parseEmotes(TagMap tagMap, String content) {
Expand Down Expand Up @@ -71,10 +74,26 @@ private static void finalizeEmote(String content, List<Emote> emotes, EmoteImpl
int end = Integer.parseInt( endIndex ) + 1; //The end index we receive from Twitch is inclusive, but Java is almost always exclusive
emote.addIndices(begin, end);

if(emoteID.contains("_")) emoteID = emoteID.substring(0, emoteID.indexOf("_"));
emote.setEmoteID( Integer.parseInt( emoteID ) );
emote.setPattern( content.substring(begin, end) );
emote.setEmoteIDString(emoteID);
emote.setPattern( content.substring(begin, end).trim() );
emotes.add(emote);

// Emote IDs should be strings, but for backwards compatibility in Twirk's API we calculate
// a integer ID as well, as good we can. In case no numbers are part of the ID, we just set
// it to 0.
// Should this STILL fail somehow, we just set this to 0 (I've had too much problem with this
// already!)
String emoteIntegerID;
Matcher matcher = integerIdPattern.matcher(emoteID);
if(matcher.find())
emoteIntegerID = matcher.group(0);
else
emoteIntegerID = "0";
try { emote.setEmoteID( Integer.parseInt( emoteIntegerID ) ); }
catch (NumberFormatException e) {
System.out.println("\tProblem parsing emote ID for string " + emoteID + " - Defaulting to 0");
emote.setEmoteID(0);
}
}

private static void addIndices(EmoteImpl emote, String beginIndex, String endIndex){
Expand Down
89 changes: 84 additions & 5 deletions src/test/java/com/gikk/twirk/types/emote/TestEmote.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void noEmotesTest_whenShortTagIsPresent_thenGivesNoEmotes(){
public void oneEmoteTest_whenFullTagIsPresent_thenParsesTheEmote(){
//Given
String input = "@badges=;color=#FF69B4;display-name=Gikklol;emotes=86:10-19;mod=0;room-id=31974228;subscriber=0;turbo=0;user-id=27658385;user-type= :nn!nn@nn.tmi.twitch.tv PRIVMSG #tv :beefin it BibleThump";
Emote e = new EmoteImpl().setPattern("BibleThump").setEmoteID(86).addIndices(10, 20);
Emote e = new EmoteImpl().setPattern("BibleThump").setEmoteIDString("86").setEmoteID(86).addIndices(10, 20);

// When
TwitchMessage message = TwitchMessageBuilder.getDefault().build(input);
Expand All @@ -66,7 +66,7 @@ public void oneEmoteTest_whenFullTagIsPresent_thenParsesTheEmote(){
public void oneEmoteTest_whenShortTagIsPresent_thenParsesTheEmote(){
//Given
String input = "@emotes=86:10-19 :nn!nn@nn.tmi.twitch.tv PRIVMSG #tv :beefin it BibleThump";
Emote e = new EmoteImpl().setPattern("BibleThump").setEmoteID(86).addIndices(10, 20);
Emote e = new EmoteImpl().setPattern("BibleThump").setEmoteIDString("86").setEmoteID(86).addIndices(10, 20);

// When
TwitchMessage message = TwitchMessageBuilder.getDefault().build(input);
Expand All @@ -79,8 +79,8 @@ public void oneEmoteTest_whenShortTagIsPresent_thenParsesTheEmote(){
public void multipleEmotesTest(){
// Given
String input = "@emotes=4685:4-9,11-16/15614:18-24; :anom!anon@anon.tmi.twitch.tv PRIVMSG #tv :Yo! tmrHat tmrHat tmrToad";
Emote e1 = new EmoteImpl().setPattern("tmrHat").setEmoteID(4685).addIndices(4, 10).addIndices(11, 17);
Emote e2 = new EmoteImpl().setPattern("tmrToad").setEmoteID(15614).addIndices(18, 25);
Emote e1 = new EmoteImpl().setPattern("tmrHat").setEmoteIDString("4685").setEmoteID(4685).addIndices(4, 10).addIndices(11, 17);
Emote e2 = new EmoteImpl().setPattern("tmrToad").setEmoteIDString("15614").setEmoteID(15614).addIndices(18, 25);

// When
TwitchMessage message = TwitchMessageBuilder.getDefault().build(input);
Expand All @@ -93,7 +93,7 @@ public void multipleEmotesTest(){
public void modifiedEmoteTest() {
// Given
String input = "@emotes=123_BW:0-12 :anon!anon@anon.tmi.twitch.tv PRIVMSG #tv :doggoThink_BW what is that?";
Emote e = new EmoteImpl().setPattern("doggoThink_BW").setEmoteID(123).addIndices(0, 13);
Emote e = new EmoteImpl().setPattern("doggoThink_BW").setEmoteIDString("123_BW").setEmoteID(123).addIndices(0, 13);

// When
TwitchMessage message = TwitchMessageBuilder.getDefault().build(input);
Expand All @@ -102,6 +102,85 @@ public void modifiedEmoteTest() {
checkEmotes(message, e);
}

@Test
public void oddEmoteIdTest_whenStartsAndEndsWithLetter() {
// Given
String input = "@emotes=A123A:0-11 :anon!anon@anon.tmi.twitch.tv PRIVMSG #tv :1oggoThink2 what is that?";
Emote e = new EmoteImpl().setPattern("1oggoThink2").setEmoteIDString("A123A").setEmoteID(123).addIndices(0, 12);

// When
TwitchMessage message = TwitchMessageBuilder.getDefault().build(input);

// Then
checkEmotes(message, e);
}

@Test
public void oddEmoteIdTest_whenEndsWithLetter() {
// Given
String input = "@emotes=123BW:0-12 :anon!anon@anon.tmi.twitch.tv PRIVMSG #tv :doggoThink_BW what is that?";
Emote e = new EmoteImpl().setPattern("doggoThink_BW").setEmoteIDString("123BW").setEmoteID(123).addIndices(0, 13);

// When
TwitchMessage message = TwitchMessageBuilder.getDefault().build(input);

// Then
checkEmotes(message, e);
}

@Test
public void oddEmoteIdTest_whenEndsWithUnderscore() {
// Given
String input = "@emotes=123_BW:0-10 :anon!anon@anon.tmi.twitch.tv PRIVMSG #tv :doggoThink2 what is that?";
Emote e = new EmoteImpl().setPattern("doggoThink2").setEmoteIDString("123_BW").setEmoteID(123).addIndices(0, 11);

// When
TwitchMessage message = TwitchMessageBuilder.getDefault().build(input);

// Then
checkEmotes(message, e);
}

@Test
public void oddEmoteIdTest_whenNoNumbers() {
// Given
String input = "@emotes=ABC_BW:0-10 :anon!anon@anon.tmi.twitch.tv PRIVMSG #tv :doggoThink2 what is that?";
Emote e = new EmoteImpl().setPattern("doggoThink2").setEmoteIDString("ABC_BW").setEmoteID(0).addIndices(0, 11);

// When
TwitchMessage message = TwitchMessageBuilder.getDefault().build(input);

// Then
checkEmotes(message, e);
}

@Test
public void oddEmoteIdTest_whenMissingCompletely() {
// Given
String input = "@emotes=:0-10 :anon!anon@anon.tmi.twitch.tv PRIVMSG #tv :doggoThink2 what is that?";
Emote e = new EmoteImpl().setPattern("doggoThink2").setEmoteIDString("").setEmoteID(0).addIndices(0, 11);

// When
TwitchMessage message = TwitchMessageBuilder.getDefault().build(input);

// Then
checkEmotes(message, e);
}

@Test
public void oddEmoteIdTest_whenMultipleOccurrences() {
// Given
String input = "@emotes=X123X:0-10,25-35/A1_B:12-15 :anon!anon@anon.tmi.twitch.tv PRIVMSG #tv :doggoThink2 what is that doggoThink2";
Emote e1 = new EmoteImpl().setPattern("doggoThink2").setEmoteIDString("X123X").setEmoteID(123).addIndices(0, 11).addIndices(25,36);
Emote e2 = new EmoteImpl().setPattern("what").setEmoteIDString("A1_B").setEmoteID(1).addIndices(12, 16);

// When
TwitchMessage message = TwitchMessageBuilder.getDefault().build(input);

// Then
checkEmotes(message, e1, e2);
}

private static void checkEmotes(TwitchMessage message, Emote... emotes){
//Assert emote properties
for( int i = 0; i < emotes.length; i++){
Expand Down