Minecraft-Updater-Java/src/main/java/com/calebfontenot/Common.java

58 lines
2.2 KiB
Java

package com.calebfontenot;
import org.json.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class Common {
public static List<String> parseKey(String fetchURL, String keyToFetch) throws Exception {
String line = rawFetch(fetchURL);
JSONObject json = new JSONObject(line);
//System.out.println(line);
JSONArray key = json.getJSONArray(keyToFetch);
List<String> returnList = new ArrayList<String>();
for (int i = 0; i < key.length() - 1; i++) {
String returnString = (String) key.get(i);
returnList.add(returnString);
}
return returnList;
}
public static String rawFetch(String fetchURL) throws Exception {
//System.out.println("CURLing API...");
URL url = new URL(fetchURL);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
for (String line; (line = reader.readLine()) != null; ) {
//JSONObject json = new JSONObject(line);
//System.out.println(line);
return line;
}
} catch (Exception IllegalArgumentException) {
System.out.println("Illegal Argument!");
IllegalArgumentException.printStackTrace();
return null;
}
return null;
}
public static String regexFetch(String fetchURL) throws Exception{ // Regexes off the start and end square braces returned by certain APIs?
String raw = rawFetch(fetchURL);
raw = raw.substring(1, raw.length() - 1);
return raw;
}
public static String regex(String raw) throws Exception{ // Regexes off the start and end square braces returned by certain APIs?
raw = raw.substring(1, raw.length() - 1);
return raw;
}
public static String parse(String fetchURL, String keyToFetch) throws Exception{
String raw = regexFetch(fetchURL);
JSONTokener tokener = new JSONTokener(raw);
JSONObject object = new JSONObject(tokener);
return object.getString(keyToFetch);
}
}