Skip to content

Commit 5a60bed

Browse files
committed
Merge branch 'master' of https://github.com/Gikkman/Java-Twirk
2 parents 9709cc2 + 0e1b84a commit 5a60bed

5 files changed

Lines changed: 133 additions & 12 deletions

File tree

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@ Include the following in your pom.xml
2222
<dependency>
2323
<groupId>com.github.gikkman</groupId>
2424
<artifactId>Java-Twirk</artifactId>
25-
<version>0.6</version>
25+
<version>0.6.1</version>
2626
</dependency>
2727
</dependencies>
2828
```
2929
Or simply download the latest version of the library jar from the release page.
3030

3131
## Changes
32+
### 0.6.1
33+
Hotfix release since some emote IDs were still not parsed correctly (see #22). This hotfix should hopefully fix this issue.
34+
Please report any further issues with parsing emotes.
35+
36+
### 0.6
3237
There has only been minor changes between 0.5 and 0.6. Nothing that should break backwards compatibility. Fixes include:
3338
* Fixed NumberFormatException on modified emotes
3439
* Twitch changed the emote IDs from always being a number to sometimes being a number_string -.-' Its fixed now

src/main/java/com/gikk/twirk/types/emote/Emote.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,20 @@
2020
*
2121
*/
2222
public interface Emote {
23-
24-
2523
/** Fetches the emotes numeric ID.
2624
*
2725
* @return The emotes numeric ID
26+
* @deprecated use {@link #getEmoteIDString()} instead
2827
*/
2928
public int getEmoteID();
29+
30+
31+
/** Fetches the emotes ID as String. This became necessary after Twitch start
32+
* including other characters than numbers in emote IDs
33+
*
34+
* @return The emotes ID
35+
*/
36+
public String getEmoteIDString();
3037

3138
/** A list of pairs on indices. Each pair is a BEGIN-END pair of a emote occurrence in the message.<br><br>
3239
*

src/main/java/com/gikk/twirk/types/emote/EmoteImpl.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ class EmoteImpl implements Emote{
88
private final static String EMOTE_URL_BASE = "http://static-cdn.jtvnw.net/emoticons/v1/";
99

1010
private int emoteID;
11+
private String emoteIDString;
1112
private final LinkedList<EmoteIndices> indices = new LinkedList<>();
1213
private String pattern;
1314

15+
public EmoteImpl setEmoteIDString(String emoteIDString){
16+
this.emoteIDString = emoteIDString;
17+
return this;
18+
}
19+
1420
public EmoteImpl setEmoteID(int emoteID){
1521
this.emoteID = emoteID;
1622
return this;
@@ -31,6 +37,11 @@ public int getEmoteID() {
3137
return emoteID;
3238
}
3339

40+
@Override
41+
public String getEmoteIDString() {
42+
return emoteIDString;
43+
}
44+
3445
@Override
3546
public String getPattern() {
3647
return pattern;
@@ -50,7 +61,7 @@ public String getEmoteImageUrl(EMOTE_SIZE imageSize){
5061

5162
@Override
5263
public String toString(){
53-
StringBuilder out = new StringBuilder(emoteID + " " + ( pattern == null ? "NULL" : pattern) + "[ ");
64+
StringBuilder out = new StringBuilder(emoteIDString + " " + ( pattern == null ? "NULL" : pattern) + "[ ");
5465

5566
for( EmoteIndices index : indices ) {
5667
out.append(index.toString());

src/main/java/com/gikk/twirk/types/emote/EmoteParserImpl.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66
import java.util.ArrayList;
77
import java.util.List;
8+
import java.util.regex.Matcher;
9+
import java.util.regex.Pattern;
810

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

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

74-
if(emoteID.contains("_")) emoteID = emoteID.substring(0, emoteID.indexOf("_"));
75-
emote.setEmoteID( Integer.parseInt( emoteID ) );
76-
emote.setPattern( content.substring(begin, end) );
77+
emote.setEmoteIDString(emoteID);
78+
emote.setPattern( content.substring(begin, end).trim() );
7779
emotes.add(emote);
80+
81+
// Emote IDs should be strings, but for backwards compatibility in Twirk's API we calculate
82+
// a integer ID as well, as good we can. In case no numbers are part of the ID, we just set
83+
// it to 0.
84+
// Should this STILL fail somehow, we just set this to 0 (I've had too much problem with this
85+
// already!)
86+
String emoteIntegerID;
87+
Matcher matcher = integerIdPattern.matcher(emoteID);
88+
if(matcher.find())
89+
emoteIntegerID = matcher.group(0);
90+
else
91+
emoteIntegerID = "0";
92+
try { emote.setEmoteID( Integer.parseInt( emoteIntegerID ) ); }
93+
catch (NumberFormatException e) {
94+
System.out.println("\tProblem parsing emote ID for string " + emoteID + " - Defaulting to 0");
95+
emote.setEmoteID(0);
96+
}
7897
}
7998

8099
private static void addIndices(EmoteImpl emote, String beginIndex, String endIndex){

src/test/java/com/gikk/twirk/types/emote/TestEmote.java

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void noEmotesTest_whenShortTagIsPresent_thenGivesNoEmotes(){
5353
public void oneEmoteTest_whenFullTagIsPresent_thenParsesTheEmote(){
5454
//Given
5555
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";
56-
Emote e = new EmoteImpl().setPattern("BibleThump").setEmoteID(86).addIndices(10, 20);
56+
Emote e = new EmoteImpl().setPattern("BibleThump").setEmoteIDString("86").setEmoteID(86).addIndices(10, 20);
5757

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

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

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

9898
// When
9999
TwitchMessage message = TwitchMessageBuilder.getDefault().build(input);
@@ -102,6 +102,85 @@ public void modifiedEmoteTest() {
102102
checkEmotes(message, e);
103103
}
104104

105+
@Test
106+
public void oddEmoteIdTest_whenStartsAndEndsWithLetter() {
107+
// Given
108+
String input = "@emotes=A123A:0-11 :anon!anon@anon.tmi.twitch.tv PRIVMSG #tv :1oggoThink2 what is that?";
109+
Emote e = new EmoteImpl().setPattern("1oggoThink2").setEmoteIDString("A123A").setEmoteID(123).addIndices(0, 12);
110+
111+
// When
112+
TwitchMessage message = TwitchMessageBuilder.getDefault().build(input);
113+
114+
// Then
115+
checkEmotes(message, e);
116+
}
117+
118+
@Test
119+
public void oddEmoteIdTest_whenEndsWithLetter() {
120+
// Given
121+
String input = "@emotes=123BW:0-12 :anon!anon@anon.tmi.twitch.tv PRIVMSG #tv :doggoThink_BW what is that?";
122+
Emote e = new EmoteImpl().setPattern("doggoThink_BW").setEmoteIDString("123BW").setEmoteID(123).addIndices(0, 13);
123+
124+
// When
125+
TwitchMessage message = TwitchMessageBuilder.getDefault().build(input);
126+
127+
// Then
128+
checkEmotes(message, e);
129+
}
130+
131+
@Test
132+
public void oddEmoteIdTest_whenEndsWithUnderscore() {
133+
// Given
134+
String input = "@emotes=123_BW:0-10 :anon!anon@anon.tmi.twitch.tv PRIVMSG #tv :doggoThink2 what is that?";
135+
Emote e = new EmoteImpl().setPattern("doggoThink2").setEmoteIDString("123_BW").setEmoteID(123).addIndices(0, 11);
136+
137+
// When
138+
TwitchMessage message = TwitchMessageBuilder.getDefault().build(input);
139+
140+
// Then
141+
checkEmotes(message, e);
142+
}
143+
144+
@Test
145+
public void oddEmoteIdTest_whenNoNumbers() {
146+
// Given
147+
String input = "@emotes=ABC_BW:0-10 :anon!anon@anon.tmi.twitch.tv PRIVMSG #tv :doggoThink2 what is that?";
148+
Emote e = new EmoteImpl().setPattern("doggoThink2").setEmoteIDString("ABC_BW").setEmoteID(0).addIndices(0, 11);
149+
150+
// When
151+
TwitchMessage message = TwitchMessageBuilder.getDefault().build(input);
152+
153+
// Then
154+
checkEmotes(message, e);
155+
}
156+
157+
@Test
158+
public void oddEmoteIdTest_whenMissingCompletely() {
159+
// Given
160+
String input = "@emotes=:0-10 :anon!anon@anon.tmi.twitch.tv PRIVMSG #tv :doggoThink2 what is that?";
161+
Emote e = new EmoteImpl().setPattern("doggoThink2").setEmoteIDString("").setEmoteID(0).addIndices(0, 11);
162+
163+
// When
164+
TwitchMessage message = TwitchMessageBuilder.getDefault().build(input);
165+
166+
// Then
167+
checkEmotes(message, e);
168+
}
169+
170+
@Test
171+
public void oddEmoteIdTest_whenMultipleOccurrences() {
172+
// Given
173+
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";
174+
Emote e1 = new EmoteImpl().setPattern("doggoThink2").setEmoteIDString("X123X").setEmoteID(123).addIndices(0, 11).addIndices(25,36);
175+
Emote e2 = new EmoteImpl().setPattern("what").setEmoteIDString("A1_B").setEmoteID(1).addIndices(12, 16);
176+
177+
// When
178+
TwitchMessage message = TwitchMessageBuilder.getDefault().build(input);
179+
180+
// Then
181+
checkEmotes(message, e1, e2);
182+
}
183+
105184
private static void checkEmotes(TwitchMessage message, Emote... emotes){
106185
//Assert emote properties
107186
for( int i = 0; i < emotes.length; i++){

0 commit comments

Comments
 (0)