Parse Twitch Viewers (Java)

652 views
Skip to first unread message

kailan...@gmail.com

unread,
Oct 28, 2013, 6:18:02 PM10/28/13
to justintv-ap...@googlegroups.com
I have a string that is the result of calling "http://api.justin.tv/api/stream/list.json?jsonp=&channel=Channel_Name" and I need to get an integer that is the current amount of viewers.

Could someone give me a hand?

Thanks :)

(I've been attempting it using Gson but I can't figure it out or find it on the ineternet)

dubst...@gmail.com

unread,
Dec 5, 2013, 6:42:25 PM12/5/13
to justintv-ap...@googlegroups.com, kailan...@gmail.com
Its not java, but should give you an idea on how to do it. I parsed another json (summary) document in order to return viewer count. All you need to do is customize the delimiters. Porting this function over to java shouldnt be too tough.

Private Function GetViewers() As String
Dim CheckMe As String
CheckMe = "http://api.justin.tv/api/stream/summary.json?channel=" & Streamer.Text
Dim uri As New Uri(CheckMe)
Dim request As System.Net.HttpWebRequest
Dim response As System.Net.HttpWebResponse
Dim Viewers As String = ""
Dim HTML As String = ""
Dim TempArray As Array
Dim Temp As String = ""
Dim DelimiterA() As String = {"count"":"}
Dim DelimiterB() As String = {",""streams"}

Try
If (uri.Scheme = uri.UriSchemeHttp) Then
request = System.Net.HttpWebRequest.Create(uri)
request.Method = System.Net.WebRequestMethods.Http.Get
request.Timeout = 60000
response = request.GetResponse
Dim reader As New StreamReader(response.GetResponseStream())
HTML = reader.ReadToEnd
response.Close()
Else
HTML = ""
End If

If HTML <> "" Then
If (InStr(HTML, "count"":") > 0) And (InStr(HTML, ",""streams") > 0) Then
TempArray = HTML.Split(DelimiterA, StringSplitOptions.None)
Temp = TempArray(1)
TempArray = Temp.Split(DelimiterB, StringSplitOptions.None)
Viewers = TempArray(0)
End If
End If
Catch ex As Exception
Viewers = ""
End Try

Return Viewers
End Function

thake...@googlemail.com

unread,
Jan 8, 2014, 3:45:22 PM1/8/14
to justintv-ap...@googlegroups.com, kailan...@gmail.com
Here's how you can do it using gson. What you can do with the JSON response is you can change it into Java classes. For example: I make a request for the top games using "https://api.twitch.tv/kraken/games/top". Please use this url and read the json file in order to follow this guide. We can think of the whole .json file as a 'top games container' So we can make a class like so

public class TopGamesContainer {
private List<Top> topGamesList;

public void addToList(Top top) {
topGamesList = top;
}

public List<Top> getTopGamesList() {
return topGamesList;
}
}

The List is a collection of Top objects. Top is an array of 'top games' within the json response given when sending top games request. So now we create a Top.java class

public class Top {
private int viewers;
private int channels;
private Game game;

// Create getters and setters for these field variables (this is important)
}

We need a Game.java class now since the "game" object in the json contains more objects.

public class Game {
private String name;
private int _id;
private int giantbomb_id;
private Box box;
private Logo logo;

// More getters and setters for every field variable
}

Almost done creating the classes. We can see again in the json response that the "game" object contains two objects called "box" and "logo" which further contain more objects.

public class Box {
private String template;
private String small;
private String medium;
private String large;

// Even more getters and setters.
}

In this particular case the "box" object and the "logo" objects both contain exactly the same things so we can just copy the class and change its name to Logo.java.

Now with the classes done we can actually start trying to do something.

We will need to creare a HttpsURLConnection object to send a request.

URL url = new URL("https://api.twitch.tv/kraken/games/top");
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("GET");

Now we are connected, we can read the response.

StringBuffer responseData = new StringBuffer();
InputStreamReader in = new InputStreamReader((InputStream) con.getContent());
BufferedReader br = new BufferedReader(in);
String line;

do {
line = bt.readLine();
if (line != null) {
responseData.append(line);
}
} while (line != null);

What we've done now is we've gotten the json response text and stored in in a StringBuffer object. Now we can start to populate the topGamesList we made earlier inside the TopGamesContainer.

Gson g = new Gson();
TopGamesContainer tgc =
g.fromJson(responseData.toString, TopGamesContainer.class());

List<Top> topGames = tgc.getTopGamesList();

With this list you can use an enhanced for loop to iterate over the list.

for (Top t: topGames) {
System.out.printf(
"Game Name: %s - Viewers: %d", t.getGame().getName(), t.getViewers());
}

This loop simply prints the Game Name and amount of viewers 1 by 1.
I hope this was easy to follow and has helped.

Reply all
Reply to author
Forward
0 new messages