2015-11-12 15:48:37 +01:00
|
|
|
package com.ixab.UserInfoHandling;
|
|
|
|
|
|
|
|
|
|
import com.ixab.GUI.ErrorMessageGate;
|
|
|
|
|
import org.json.JSONArray;
|
2015-11-12 21:35:32 +01:00
|
|
|
import org.json.JSONException;
|
2015-11-12 15:48:37 +01:00
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
public class UserInfo {
|
2015-11-12 21:35:32 +01:00
|
|
|
private static JSONArray userFollowData; // TODO: change this to an arraylist of multiple jsonarrays
|
2015-11-12 15:48:37 +01:00
|
|
|
private static String userFollowCount;
|
|
|
|
|
|
|
|
|
|
private static int getFollowCount() {
|
2015-11-12 21:35:32 +01:00
|
|
|
return Integer.parseInt(userFollowCount);
|
2015-11-12 15:48:37 +01:00
|
|
|
}
|
|
|
|
|
public static ArrayList<String> getFollowedChannelNames() {
|
|
|
|
|
ArrayList<String> names = new ArrayList<String>();
|
2015-11-12 21:35:32 +01:00
|
|
|
for (int i = 0; i<getFollowCount(); i++) {
|
|
|
|
|
try {
|
|
|
|
|
JSONObject followed = new JSONObject(userFollowData.get(i).toString());
|
|
|
|
|
JSONObject channel = new JSONObject(followed.get("channel").toString());
|
2015-11-30 17:00:22 +01:00
|
|
|
System.out.println(i+" "+channel.get("name").toString()); //remove this line if not needed anymore
|
|
|
|
|
names.add(channel.get("name").toString());
|
2015-11-12 21:35:32 +01:00
|
|
|
// TODO: bisher nur maximal 100 importieren wegen twitch api.
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
// workaround for stupid limit of jsonarrays of twitch's api.
|
|
|
|
|
// there's nothing interesting to show.
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-12 15:48:37 +01:00
|
|
|
return names;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-12 21:35:32 +01:00
|
|
|
public static boolean initUserFollowData(String userName, int offset) {
|
2015-11-12 15:48:37 +01:00
|
|
|
if (userName != null) {
|
|
|
|
|
InputStream is = null;
|
|
|
|
|
URL url = null;
|
|
|
|
|
JSONObject object = null;
|
|
|
|
|
StringBuilder urlBuilder = new StringBuilder();
|
|
|
|
|
urlBuilder.append("https://api.twitch.tv/kraken/users/");
|
|
|
|
|
|
|
|
|
|
urlBuilder.append(userName);
|
|
|
|
|
|
2015-11-12 21:35:32 +01:00
|
|
|
urlBuilder.append("/follows/channels?direction=DESC&limit=100&offset=");
|
|
|
|
|
|
|
|
|
|
urlBuilder.append(offset);
|
2015-11-12 15:48:37 +01:00
|
|
|
|
|
|
|
|
String URLString = urlBuilder.toString();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
url = new URL(URLString);
|
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
is = url.openStream();
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
ErrorMessageGate.setErrorText("Benutzer "+userName+" existiert nicht.");
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
ErrorMessageGate.setErrorText("Fehler bei Verbindungsaufbau zu Twitch.");
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
InputStreamReader isr = new InputStreamReader(is);
|
|
|
|
|
BufferedReader br = new BufferedReader(isr);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
String line;
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
while ((line = br.readLine()) != null) {
|
|
|
|
|
sb.append(line);
|
|
|
|
|
}
|
|
|
|
|
object = new JSONObject(sb.toString());
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-12 21:35:32 +01:00
|
|
|
String result = object.get("_total").toString(); // TODO: reagiere hier auf >100, weil dann mehrere jsonarrays benötigt werden.
|
2015-11-12 15:48:37 +01:00
|
|
|
|
|
|
|
|
if (result == "null") {
|
|
|
|
|
userFollowData = null;
|
|
|
|
|
userFollowCount = null;
|
|
|
|
|
} else {
|
|
|
|
|
userFollowData = new JSONArray(object.getJSONArray("follows").toString());
|
|
|
|
|
userFollowCount = result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|