Commit 645cb8b6 authored by Simon Kirsten's avatar Simon Kirsten
Browse files

Added StreamTV.java

parent ac8275d8
package de.simonkirsten.streamtvexampleapp;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class StreamTV {
private Uri baseUri;
/**
* Creates a new instance of StreamTV.
* The instance is very lightweight so it is okay to create a new instance for each request.
*
* @param ipAddressWithPort The IP address with port e.g. "10.0.2.2:8080"
*/
public StreamTV(String ipAddressWithPort) {
baseUri = Uri.parse("http://" + ipAddressWithPort);
}
/**
* Creates a new instance of StreamTV.
* The instance is very lightweight so it is okay to create a new instance for each request.
*
* @param ipAddress The IP address e.g. "10.0.2.2"
* @param port The port e.g. 8080
*/
public StreamTV(String ipAddress, int port) {
baseUri = Uri.parse("http://" + ipAddress + ":" + port);
}
private String doRequest(String path, @Nullable Map<String, String> params) throws IOException {
Uri.Builder builder = baseUri.buildUpon().appendPath(path);
if (params != null) {
for (Map.Entry<String, String> entry : params.entrySet()) {
builder.appendQueryParameter(entry.getKey(), entry.getValue());
}
}
URL url = new URL(builder.build().toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
try {
conn.connect();
int status = conn.getResponseCode();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append("\n"); // add the stripped line back
}
br.close();
String content = sb.toString();
if (status != HttpURLConnection.HTTP_OK) { // status != 200
throw new IOException(String.format("HTTP request %s failed with status %d and content: %s", conn, status, content));
}
return content;
} finally {
conn.disconnect();
}
}
private List<JSONObject> jsonArrayToJsonObjectList(JSONArray array) throws JSONException {
List<JSONObject> list = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
list.add(array.getJSONObject(i));
}
return list;
}
/**
* Updates and returns the new state of the TV.
* Reference at <a href="https://simons-nzse-2.h-da.io/stream-tv/server/reference/#tvstate">https://simons-nzse-2.h-da.io/stream-tv/server/reference/#tvstate</a>
*
* @param large_channel Sets the channel name of the large screen.
* This screen is the always visible main screen.
* Set to "" to disable. Set to null to keep the old value.
*
* @param small_channel Sets the channel name of the small screen.
* This screen is a small Picture-in-Picture like screen in the top right corner.
* Set to "" to disable. Set to null to keep the old value.
*
* @param volume Sets the volume of the large screen.
* Set to 0 or 0.0 to mute.
* Values are in the range of 0.0 - 1.0.
* Note: The small screen is always muted.
* Set to null to keep the old value.
*
* @param small_scale Sets the scale (size) of the small screen.
* The value is relative to the width of the large screen.
* Reasonable values are in the range of 0.2 - 0.5.
* It is best to just experiment with this setting.
* Set to null to keep the old value.
*
* @param show_chat Sets whether the chat should be shown or not.
* Set to null to keep the old value.
*/
public JSONObject tvState(@Nullable String large_channel, @Nullable String small_channel, @Nullable Float volume, @Nullable Float small_scale, @Nullable Boolean show_chat) throws IOException, JSONException {
Map<String, String> params = new HashMap<>();
if (large_channel != null) {
params.put("large_channel", large_channel);
}
if (small_channel != null) {
params.put("small_channel", small_channel);
}
if (volume != null) {
params.put("volume", volume.toString());
}
if (small_scale != null) {
params.put("small_scale", small_scale.toString());
}
if (show_chat != null) {
params.put("show_chat", show_chat.toString());
}
String content = doRequest("/tv/state", params);
return new JSONObject(content);
}
/**
* Gets the most popular games on Twitch right now.
* Reference at <a href="https://simons-nzse-2.h-da.io/stream-tv/server/reference/#twitchgamestop">https://simons-nzse-2.h-da.io/stream-tv/server/reference/#twitchgamestop</a>
*/
public List<JSONObject> twitchGamesTop() throws IOException, JSONException {
String content = doRequest("/twitch/games/top", null);
return jsonArrayToJsonObjectList(new JSONArray(content));
}
/**
* Searches games by query.
* Reference at <a href="https://simons-nzse-2.h-da.io/stream-tv/server/reference/#twitchgamessearch">https://simons-nzse-2.h-da.io/stream-tv/server/reference/#twitchgamessearch</a>
*
* @param query What should be searched.
*/
public List<JSONObject> twitchGamesSearch(@NonNull String query) throws IOException, JSONException {
Map<String, String> params = new HashMap<>();
params.put("query", query);
String content = doRequest("/twitch/games/search", params);
return jsonArrayToJsonObjectList(new JSONArray(content));
}
/**
* Gets the most popular streams on Twitch right now. Optionally filter by channels, game and language.
* Reference at <a href="https://simons-nzse-2.h-da.io/stream-tv/server/reference/#twitchstreamstop">https://simons-nzse-2.h-da.io/stream-tv/server/reference/#twitchstreamstop</a>
*
* @param channels Specify up to 100 channels separated by `,` that the search should be limited to.
* See the examples in the reference why that would be useful.
*
* @param game Specify a game that the search should be limited to.
*
* @param language Specify a language that the search should be limited to.
*/
public List<JSONObject> twitchStreamsTop(@Nullable String[] channels, @Nullable String game, @Nullable String language) throws IOException, JSONException {
Map<String, String> params = new HashMap<>();
if (channels != null) {
params.put("channels", TextUtils.join(",", channels));
}
if (game != null) {
params.put("game", game);
}
if (language != null) {
params.put("language", language);
}
String content = doRequest("/twitch/streams/top", params);
return jsonArrayToJsonObjectList(new JSONArray(content));
}
/**
* Gets the featured games on Twitch's homepage.
* Reference at <a href="https://simons-nzse-2.h-da.io/stream-tv/server/reference/#twitchstreamsfeatured">https://simons-nzse-2.h-da.io/stream-tv/server/reference/#twitchstreamsfeatured</a>
*/
public List<JSONObject> twitchStreamsFeatured() throws IOException, JSONException {
String content = doRequest("/twitch/streams/featured", null);
return jsonArrayToJsonObjectList(new JSONArray(content));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment