1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
package io.github.moulberry.notenoughupdates.util;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.apache.commons.io.IOUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
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) {
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) : ""));
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();
}
}
|