tweaks724/src/main/java/eu/m724/tweaks/updater/VersionFinder.java
Minecon724 740fab869e
Some checks failed
/ deploy (push) Failing after 1m5s
add license
2024-11-27 20:46:13 +01:00

115 lines
4.5 KiB
Java

/*
* Copyright (C) 2024 Minecon724
* Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file
* in the project root for the full license text.
*/
package eu.m724.tweaks.updater;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import eu.m724.tweaks.updater.cache.SpigotResource;
import eu.m724.tweaks.updater.cache.ResourceVersion;
import eu.m724.tweaks.updater.cache.VersionedResource;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
// TODO optimize
public class VersionFinder extends CompletableFuture<VersionedResource> {
private final SpigotResource resource;
private final int fromPage;
VersionFinder(SpigotResource resource, int fromPage) {
this.resource = resource;
this.fromPage = fromPage;
start();
}
VersionFinder(SpigotResource resource) {
this(resource, 1);
}
private void start() {
System.out.printf("STarting for %d %s\n", resource.resourceId(), resource.plugin().getName());
try (ExecutorService executor = Executors.newSingleThreadExecutor()) {
executor.execute(() -> {
try {
ResourceVersion runningVersion = null;
ResourceVersion latestVersion = null;
int page;
for (page = fromPage; page < 1000; page++) {
System.out.println("Page " + page);
String url = "https://api.spigotmc.org/simple/0.2/index.php?action=getResourceUpdates&page=%d&id=%d".formatted(page, resource.resourceId());
HttpRequest request;
try {
request = HttpRequest.newBuilder(new URI(url))
.header("User-Agent", "twu/1")
.build();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
} // this will never happen
try (HttpClient client = HttpClient.newHttpClient()) {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
String body = response.body();
if (body.isBlank()) {
page--;
break;
}
JsonArray jsonArray = JsonParser.parseString(body).getAsJsonArray();
for (JsonElement ele : jsonArray) {
JsonObject versionJson = ele.getAsJsonObject();
if (isRunningVersion(versionJson)) {
runningVersion = new ResourceVersion(
resource.resourceId(),
page,
versionJson.get("id").getAsInt()
);
}
latestVersion = new ResourceVersion(
resource.resourceId(),
page,
versionJson.get("id").getAsInt()
);
System.out.printf("%d %d %s\n", page, versionJson.get("id").getAsInt(), versionJson.get("resource_version").getAsString());
}
if (jsonArray.size() < 10) break;
}
}
System.out.println("Done");
if (page > 999) {
throw new Exception("Too many pages");
} else {
this.complete(new VersionedResource(resource, runningVersion, latestVersion));
}
} catch (Exception e) {
this.completeExceptionally(e);
}
});
}
}
private boolean isRunningVersion(JsonObject versionJson) {
// TODO
return versionJson.get("resource_version").getAsString().equals(resource.plugin().getDescription().getVersion());
}
}