diff options
| author | Moulberry <jjenour@student.unimelb.edu.au> | 2022-03-03 11:03:58 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-03 11:03:58 +0800 |
| commit | 7c6d37b2eb758a13b342b906f0aef88b940bc52a (patch) | |
| tree | 9602a014425b859e3aba98f31f93d6de04521356 /src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java | |
| parent | b11742988dec635b5c5da7c2363803cbfafb37b1 (diff) | |
| parent | db59eba3fd9121c7c0a88363994876c5b582c08c (diff) | |
| download | notenoughupdates-7c6d37b2eb758a13b342b906f0aef88b940bc52a.tar.gz notenoughupdates-7c6d37b2eb758a13b342b906f0aef88b940bc52a.tar.bz2 notenoughupdates-7c6d37b2eb758a13b342b906f0aef88b940bc52a.zip | |
Merge pull request #248 from NotEnoughUpdates/master
NEU 2.1 🙂
Diffstat (limited to 'src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java')
| -rw-r--r-- | src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java | 301 |
1 files changed, 167 insertions, 134 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java b/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java index e75eaebb..2628eb7d 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java @@ -5,151 +5,184 @@ import com.google.gson.JsonObject; import io.github.moulberry.notenoughupdates.NotEnoughUpdates; import org.apache.commons.io.IOUtils; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; import java.net.ConnectException; import java.net.URL; import java.net.URLConnection; +import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; import java.util.function.Consumer; import java.util.zip.GZIPInputStream; public class HypixelApi { - private Gson gson = new Gson(); - private ExecutorService es = Executors.newFixedThreadPool(3); - - private static final int FAILS_BEFORE_SWITCH = 3; - private int currentUrl = 0; - private long lastPrimaryUrl = 0; - private final String[] myApiURLs = {"https://moulberry.codes/"};//, "http://moulberry.codes/", "http://51.79.51.21/"};//, "http://51.75.78.252/" }; - private final Integer[] myApiSuccesses = {0, 0, 0, 0}; - - public void getHypixelApiAsync(String apiKey, String method, HashMap<String, String> args, Consumer<JsonObject> consumer) { - getHypixelApiAsync(apiKey, method, args, consumer, () -> {}); - } - - public void getHypixelApiAsync(String apiKey, String method, HashMap<String, String> args, Consumer<JsonObject> consumer, Runnable error) { - getApiAsync(generateApiUrl(apiKey!=null?apiKey.trim():null, method, args), consumer, error); - } - - private String getMyApiURL() { - if(currentUrl == 0) { - lastPrimaryUrl = System.currentTimeMillis(); - } else if(System.currentTimeMillis() - lastPrimaryUrl > 1000*60*30) { //Try switch back to main url after 30m - currentUrl = 0; - } - - myApiSuccesses[currentUrl] = Math.min(FAILS_BEFORE_SWITCH, myApiSuccesses[currentUrl] + 1); - return myApiURLs[currentUrl]; - } - - private void myApiError(int index) { - myApiSuccesses[index] = myApiSuccesses[index] - 2; - - if(myApiSuccesses[index] < 0) { - myApiSuccesses[index] = 0; - - if(index == currentUrl) { - currentUrl++; - if(currentUrl >= myApiURLs.length) { - currentUrl = 0; - } - } - } - } - - public void getApiAsync(String urlS, Consumer<JsonObject> consumer, Runnable error) { - es.submit(() -> { - try { - consumer.accept(getApiSync(urlS)); - } catch(Exception e) { - error.run(); - } - }); - } - - public void getMyApiAsync(String urlS, Consumer<JsonObject> consumer, Runnable error) { - es.submit(() -> { - int current = currentUrl; - try { - consumer.accept(getApiSync(getMyApiURL()+urlS)); - } catch(Exception e) { - if(NotEnoughUpdates.INSTANCE.config.hidden.dev) { - e.printStackTrace(); - } - myApiError(current); - error.run(); - } - }); - } - - public void getMyApiGZIPAsync(String urlS, Consumer<JsonObject> consumer, Runnable error) { - es.submit(() -> { - int current = currentUrl; - try { - consumer.accept(getApiGZIPSync(getMyApiURL()+urlS)); - } catch(Exception e) { - myApiError(current); - error.run(); - } - }); - } - - public void getApiGZIPAsync(String urlS, Consumer<JsonObject> consumer, Runnable error) { - es.submit(() -> { - try { - consumer.accept(getApiGZIPSync(urlS)); - } catch(Exception e) { - error.run(); - } - }); - } - - public JsonObject getApiSync(String urlS) throws IOException { - URL url = new URL(urlS); - URLConnection connection = url.openConnection(); - connection.setConnectTimeout(10000); - connection.setReadTimeout(10000); - - String response = IOUtils.toString(connection.getInputStream(), StandardCharsets.UTF_8); - - JsonObject json = gson.fromJson(response, JsonObject.class); - if(json == null) throw new ConnectException("Invalid JSON"); - return json; - } - - public JsonObject getApiGZIPSync(String urlS) throws IOException { - URL url = new URL(urlS); - URLConnection connection = url.openConnection(); - connection.setConnectTimeout(10000); - connection.setReadTimeout(10000); - - String response = IOUtils.toString(new GZIPInputStream(connection.getInputStream()), StandardCharsets.UTF_8); - - JsonObject json = gson.fromJson(response, JsonObject.class); - return json; - } - - public String generateApiUrl(String apiKey, String method, HashMap<String, String> args) { - StringBuilder url = new StringBuilder("https://api.hypixel.net/" + method + (apiKey != null ? ("?key=" + apiKey.replace(" ", "")) : "")); - boolean first = true; - for(Map.Entry<String, String> entry : args.entrySet()) { - if(first && apiKey == null) { - url.append("?"); - first = false; - } else { - url.append("&"); - } - url.append(entry.getKey()).append("=").append(entry.getValue()); - } - return url.toString(); - } - + private final Gson gson = new Gson(); + private final ExecutorService es = Executors.newFixedThreadPool(3); + + private static final int FAILS_BEFORE_SWITCH = 3; + private int currentUrl = 0; + private long lastPrimaryUrl = 0; + private final String[] myApiURLs = {"https://moulberry.codes/"}; + //, "http://moulberry.codes/", "http://51.79.51.21/"};//, "http://51.75.78.252/" }; + private final Integer[] myApiSuccesses = {0, 0, 0, 0}; + + public CompletableFuture<JsonObject> getHypixelApiAsync(String apiKey, String method, HashMap<String, String> args) { + return getApiAsync(generateApiUrl(apiKey, method, args)); + } + + public void getHypixelApiAsync( + String apiKey, + String method, + HashMap<String, String> args, + Consumer<JsonObject> consumer + ) { + getHypixelApiAsync(apiKey, method, args, consumer, () -> { + }); + } + + public void getHypixelApiAsync( + String apiKey, + String method, + HashMap<String, String> args, + Consumer<JsonObject> consumer, + Runnable error + ) { + getApiAsync(generateApiUrl(apiKey, method, args), consumer, error); + } + + private String getMyApiURL() { + if (currentUrl == 0) { + lastPrimaryUrl = System.currentTimeMillis(); + } else if (System.currentTimeMillis() - lastPrimaryUrl > 1000 * 60 * 30) { //Try switch back to main url after 30m + currentUrl = 0; + } + + myApiSuccesses[currentUrl] = Math.min(FAILS_BEFORE_SWITCH, myApiSuccesses[currentUrl] + 1); + return myApiURLs[currentUrl]; + } + + private void myApiError(int index) { + myApiSuccesses[index] = myApiSuccesses[index] - 2; + + if (myApiSuccesses[index] < 0) { + myApiSuccesses[index] = 0; + + if (index == currentUrl) { + currentUrl++; + if (currentUrl >= myApiURLs.length) { + currentUrl = 0; + } + } + } + } + + public CompletableFuture<JsonObject> getApiAsync(String urlS) { + CompletableFuture<JsonObject> result = new CompletableFuture<>(); + es.submit(() -> { + try { + result.complete(getApiSync(urlS)); + } catch (Exception e) { + result.completeExceptionally(e); + } + }); + return result; + } + + public void getApiAsync(String urlS, Consumer<JsonObject> consumer, Runnable error) { + es.submit(() -> { + try { + consumer.accept(getApiSync(urlS)); + } catch (Exception e) { + error.run(); + } + }); + } + + public void getMyApiAsync(String urlS, Consumer<JsonObject> consumer, Runnable error) { + es.submit(() -> { + int current = currentUrl; + try { + consumer.accept(getApiSync(getMyApiURL() + urlS)); + } catch (Exception e) { + if (NotEnoughUpdates.INSTANCE.config.hidden.dev) { + e.printStackTrace(); + } + myApiError(current); + error.run(); + } + }); + } + + public void getMyApiGZIPAsync(String urlS, Consumer<JsonObject> consumer, Runnable error) { + es.submit(() -> { + int current = currentUrl; + try { + consumer.accept(getApiGZIPSync(getMyApiURL() + urlS)); + } catch (Exception e) { + myApiError(current); + error.run(); + } + }); + } + + public void getApiGZIPAsync(String urlS, Consumer<JsonObject> consumer, Runnable error) { + es.submit(() -> { + try { + consumer.accept(getApiGZIPSync(urlS)); + } catch (Exception e) { + error.run(); + } + }); + } + + public JsonObject getApiSync(String urlS) throws IOException { + URL url = new URL(urlS); + URLConnection connection = url.openConnection(); + connection.setConnectTimeout(10000); + connection.setReadTimeout(10000); + + String response = IOUtils.toString(connection.getInputStream(), StandardCharsets.UTF_8); + + JsonObject json = gson.fromJson(response, JsonObject.class); + if (json == null) throw new ConnectException("Invalid JSON"); + return json; + } + + public JsonObject getApiGZIPSync(String urlS) throws IOException { + URL url = new URL(urlS); + URLConnection connection = url.openConnection(); + connection.setConnectTimeout(10000); + connection.setReadTimeout(10000); + + String response = IOUtils.toString(new GZIPInputStream(connection.getInputStream()), StandardCharsets.UTF_8); + + JsonObject json = gson.fromJson(response, JsonObject.class); + return json; + } + + public String generateApiUrl(String apiKey, String method, HashMap<String, String> args) { + if (apiKey != null) + args.put("key", apiKey.trim().replace("-", "")); + StringBuilder url = new StringBuilder("https://api.hypixel.net/" + method); + boolean first = true; + for (Map.Entry<String, String> entry : args.entrySet()) { + if (first) { + url.append("?"); + first = false; + } else { + url.append("&"); + } + try { + url.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8.name())).append("=") + .append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.name())); + } catch (UnsupportedEncodingException e) { + } + } + return url.toString(); + } } |
