Reputation: 371
I'm working on my IRC Bot for Twitch.tv (pircbot API) and want to make the bot connect automatically to the stream's chat as soon the stream is going live.
I've found a Twitch API (here), but I have no clue how I can implement this into my bot since I'm pretty new to Java.
Would be cool if someone could tell me any hints on how I could retrieve if a stream is online and how I can check the amount of viewers watching right now.
Greetings
Upvotes: 1
Views: 7331
Reputation:
I realize that this question was posted about a year ago, but I feel its important to have it answered just in case others stumble on this post. What Cam.Stokes said is spot on. It's the answer to the question. However the questioner indicated he is new to Java so I want to use code snippets to work out what Cam.Stokes said.
Catching if a stream is live isn't too difficult. The following code is a snippet from my bot. I got a thread that periodically receives the JSON data from the twitch API and then loads it into a JSON object. The JSON library that I'm using is called "minimal-json". Excellent lightweight lib if you ask me. When you review the code you can see what I'm doing; call twitch-api, get the JSON from the target stream, see if the "stream" object is filled with data. if so, stream is live, otherwise, not live.
import com.eclipsesource.json.JsonObject; // minimal-json specific
private static String TWITCH_STREAM = "https://api.twitch.tv/kraken/streams/$c$";
private static String insertChannel(String url, String channel)
{
return url.replace("$c$", channel );
}
public boolean isStreamLive()
{
try
{
URL url = new URL( insertChannel(TWITCH_STREAM, targetChannel) );
URLConnection conn = url.openConnection();
BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() ));
String inputLine = br.readLine();
br.close();
JsonObject jsonObj = JsonObject.readFrom(inputLine);
return ( jsonObj.get("stream").isNull() )?false:true;
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
}
Connecting the bot is rather straight forward with PircBot. Answered by Nicolai. Make sure you have an Oauth key from twitch that's linked to your bot account. The target channel is in lower case and with a # in front. And you should be good to go.
Good luck.
Upvotes: 5
Reputation: 218
Check out the chat section of the twitch API to see how to connect to the chat using PircBot:
https://github.com/justintv/Twitch-API/blob/master/IRC.md
As far as getting the live stream status, that is a little bit more difficult, but the url you want is:
https://github.com/justintv/Twitch-API/blob/master/v2_resources/streams.md#get-streamschannel
My suggestion would be to set a timed thread to check the stream status every 10 seconds or so, and if the "stream: " object in the json is not null, making the connection.
A good lib for parsing json (if you are doing twitch stuff, you will need to parse lots of json backwards and forwards by the looks) is gson.
(I need more than 10 rep to post more than 2 links apparently, but copy paste this without the space http:// code.google.com/p/google-gson/ )
Upvotes: 1
Reputation: 133
you need to get a auth key first
Link: http://twitchapps.com/tmi/
and then you need to set this in the main java file
bot.connect("irc.twitch.tv", 6667, "oauth:YOURAUTHKEY");
bot.joinChannel("#YOURCHANNEL-INLOWERCASE");
let me know if you have some other problems
Upvotes: 2