Modrinth api v2

This commit is contained in:
Johannes Frohnmeyer 2022-02-28 10:32:20 +01:00
parent 1431eb0719
commit 166b3e2124
Signed by: Johannes
GPG Key ID: E76429612C2929F4
9 changed files with 59 additions and 37 deletions

View File

@ -7,6 +7,7 @@ import io.gitlab.jfronny.inceptum.Inceptum;
import io.gitlab.jfronny.inceptum.model.curseforge.CurseforgeMod;
import io.gitlab.jfronny.inceptum.model.inceptum.InstanceMeta;
import io.gitlab.jfronny.inceptum.model.inceptum.ModDescription;
import io.gitlab.jfronny.inceptum.model.modrinth.ModrinthProjectType;
import io.gitlab.jfronny.inceptum.util.PathUtil;
import io.gitlab.jfronny.inceptum.util.mds.IWModDescription;
import io.gitlab.jfronny.inceptum.util.mds.ModsDirScanner;
@ -47,7 +48,7 @@ public class AddModWindow extends Window {
String query = this.query.get();
new Thread(() -> {
try {
ModrinthSearchResult ms = ModrinthApi.search(query, mrPage, instance.getMinecraftVersion());
ModrinthSearchResult ms = ModrinthApi.search(query, mrPage, instance.getMinecraftVersion(), ModrinthProjectType.mod);
if (!this.query.get().equals(query)) return;
mr = ms;
List<CurseforgeMod> cs = CurseforgeApi.search(instance.getMinecraftVersion(), query, cfPage, "Popularity");
@ -85,10 +86,10 @@ public class AddModWindow extends Window {
}
if (mr != null && ImGui.beginTable("mods" + modsDir, 3, ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.Borders)) {
for (ModrinthSearchResult.ModResult mod : mr.hits) {
String modId = (mod.slug != null ? mod.slug : mod.mod_id);
String modId = (mod.slug != null ? mod.slug : mod.project_id);
final String idPrefix = "local-";
if (mod.mod_id.startsWith(idPrefix))
mod.mod_id = mod.mod_id.substring(idPrefix.length());
if (mod.project_id.startsWith(idPrefix))
mod.project_id = mod.project_id.substring(idPrefix.length());
//TODO detail view
ImGui.tableNextColumn();
ImGui.text(mod.title);
@ -100,7 +101,7 @@ public class AddModWindow extends Window {
alreadyPresent = mdsMod.mod().isPresent()
&& mdsMod.mod().get().sources.keySet().stream()
.anyMatch(s -> s instanceof ModrinthModSource ms
&& ms.getModId().equals(mod.mod_id));
&& ms.getModId().equals(mod.project_id));
if (alreadyPresent)
break;
}
@ -108,11 +109,11 @@ public class AddModWindow extends Window {
ImGui.text("Installed");
}
else {
if (ImGui.button("Add##" + mod.mod_id)) {
if (ImGui.button("Add##" + mod.project_id)) {
ModrinthVersion stable = null;
ModrinthVersion beta = null;
ModrinthVersion latest = null;
for (ModrinthVersion version : ModrinthApi.getVersions(mod.mod_id)) {
for (ModrinthVersion version : ModrinthApi.getVersions(mod.project_id)) {
if (version.game_versions.contains(instance.getMinecraftVersion()) && version.loaders.contains("fabric")) {
latest = version;
if (version.version_type == ModrinthVersion.VersionType.beta || version.version_type == ModrinthVersion.VersionType.release) {
@ -132,7 +133,7 @@ public class AddModWindow extends Window {
ModrinthVersion finalLatest = latest;
new Thread(() -> {
try {
download(new ModrinthModSource(finalLatest.id), modsDir.resolve((mod.slug == null ? mod.mod_id : mod.slug) + PathUtil.EXT_IMOD), mds).write();
download(new ModrinthModSource(finalLatest.id), modsDir.resolve((mod.slug == null ? mod.project_id : mod.slug) + PathUtil.EXT_IMOD), mds).write();
} catch (IOException e) {
Inceptum.showError("Could not download mod", e);
}
@ -141,7 +142,7 @@ public class AddModWindow extends Window {
}
}
ImGui.sameLine();
if (ImGui.button("Web##" + mod.mod_id)) {
if (ImGui.button("Web##" + mod.project_id)) {
Utils.openWebBrowser(new URI("https://modrinth.com/mod/" + modId));
}
}

View File

@ -14,7 +14,7 @@ public class CurseforgeMod {
public Integer gameId;
public String summary;
public Integer downloadFiles;
public Integer downloadCount;
public Long downloadCount;
public List<File> latestFiles;
public List<Category> categories;
public Integer statue;

View File

@ -0,0 +1,5 @@
package io.gitlab.jfronny.inceptum.model.modrinth;
public enum ModrinthDependencyType {
required, optional, unsupported
}

View File

@ -3,9 +3,10 @@ package io.gitlab.jfronny.inceptum.model.modrinth;
import java.util.Date;
import java.util.List;
public class ModrinthMod {
public class ModrinthProject {
public String id;
public String slug;
public ModrinthProjectType project_type;
public String team;
public String title;
public String description;
@ -15,10 +16,10 @@ public class ModrinthMod {
public Date updated;
public String status;
public License license;
public String client_side;
public String server_side;
public Integer downloads;
public Integer follows;
public ModrinthDependencyType client_side;
public ModrinthDependencyType server_side;
public Long downloads;
public Long follows;
public List<String> categories;
public List<String> versions;
public String icon_url;
@ -27,10 +28,19 @@ public class ModrinthMod {
public String wiki_url;
public String discord_url;
public List<String> donation_urls;
public List<GalleryItem> gallery;
public static class License {
public String id;
public String name;
public String url;
}
public static class GalleryItem {
public String url;
public Boolean featured;
public String title;
public String description;
public Date created;
}
}

View File

@ -0,0 +1,5 @@
package io.gitlab.jfronny.inceptum.model.modrinth;
public enum ModrinthProjectType {
mod, modpack
}

View File

@ -10,24 +10,23 @@ public class ModrinthSearchResult {
public Integer total_hits;
public static class ModResult {
public String mod_id;
public String project_id;
public ModrinthProjectType project_type;
public String slug;
public String author;
public String title;
public String description;
public List<String> categories;
public List<String> versions;
public Integer downloads;
public Integer follows;
public String page_url;
public Long downloads;
public Long follows;
public String icon_url;
public String author_url;
public Date date_created;
public Date date_modified;
public String latest_version;
public String license;
public String client_side;
public String server_side;
public String host;
public List<String> gallery;
}
}

View File

@ -5,14 +5,15 @@ import java.util.List;
public class ModrinthVersion {
public String id;
public String mod_id;
public String project_id;
public String author_id;
public Boolean featured;
public String name;
public String version_number;
public String changelog;
public String changelog_url;
public Date date_published;
public Integer downloads;
public Long downloads;
public VersionType version_type;
public List<File> files;
public List<Dependency> dependencies;
@ -30,13 +31,14 @@ public class ModrinthVersion {
public Boolean primary;
public static class Hashes {
public String sha512;
public String sha1;
public String sha512;
}
}
public static class Dependency {
public String version_id;
public String project_id;
public DependencyType dependency_type;
public enum DependencyType {

View File

@ -1,7 +1,8 @@
package io.gitlab.jfronny.inceptum.util.api;
import com.google.gson.reflect.TypeToken;
import io.gitlab.jfronny.inceptum.model.modrinth.ModrinthMod;
import io.gitlab.jfronny.inceptum.model.modrinth.ModrinthProject;
import io.gitlab.jfronny.inceptum.model.modrinth.ModrinthProjectType;
import io.gitlab.jfronny.inceptum.model.modrinth.ModrinthSearchResult;
import io.gitlab.jfronny.inceptum.model.modrinth.ModrinthVersion;
import io.gitlab.jfronny.inceptum.util.Utils;
@ -19,32 +20,31 @@ public class ModrinthApi {
//TODO search by categories: facets:[["versions:$ver","versions:$ver"],["categories:$cat","categories:$cat"]]
//TODO filter server/client-only mods
public static ModrinthSearchResult search(String query, int page, String version) throws IOException {
return Utils.downloadObject(Utils.buildUrl(API_HOST, "api/v1/mod", Map.of(
public static ModrinthSearchResult search(String query, int page, String version, ModrinthProjectType type) throws IOException {
return Utils.downloadObject(Utils.buildUrl(API_HOST, "v2/search", Map.of(
"query", query,
"filters", "categories=\"fabric\"",
"facets", "[[\"versions:" + version + "\"]]",
"facets", "[[\"versions:" + version + "\"],[\"categories:fabric\"],[\"project_type:" + type + "\"]]",
"index", "relevance",
"offset", Integer.toString(page * ITEMS_PER_PAGE),
"limit", Integer.toString(ITEMS_PER_PAGE)
)), ModrinthSearchResult.class);
}
public static ModrinthMod getMod(String id) throws IOException {
return Utils.downloadObject(API_HOST + "api/v1/mod/" + id, ModrinthMod.class);
public static ModrinthProject getMod(String id) throws IOException {
return Utils.downloadObject(API_HOST + "v2/project/" + id, ModrinthProject.class);
}
public static List<ModrinthVersion> getVersions(String mod) throws IOException {
List<ModrinthVersion> versions = Utils.downloadObject(API_HOST + "api/v1/mod/" + mod + "/version", modrinthVersionListType);
List<ModrinthVersion> versions = Utils.downloadObject(API_HOST + "v2/project/" + mod + "/version", modrinthVersionListType);
versions.sort(Comparator.comparing(version -> version.date_published));
return versions;
}
public static ModrinthVersion getVersion(String id) throws IOException {
return Utils.downloadObject(API_HOST + "api/v1/version/" + id, ModrinthVersion.class);
return Utils.downloadObject(API_HOST + "v2/version/" + id, ModrinthVersion.class);
}
public static ModrinthVersion getVersionByHash(String sha1) throws IOException {
return Utils.downloadObject(API_HOST + "api/v1/version_file/" + sha1 + "?algorithm=sha1", ModrinthVersion.class);
return Utils.downloadObject(API_HOST + "v2/version_file/" + sha1 + "?algorithm=sha1", ModrinthVersion.class);
}
}

View File

@ -1,6 +1,6 @@
package io.gitlab.jfronny.inceptum.util.source;
import io.gitlab.jfronny.inceptum.model.modrinth.ModrinthMod;
import io.gitlab.jfronny.inceptum.model.modrinth.ModrinthProject;
import io.gitlab.jfronny.inceptum.model.modrinth.ModrinthVersion;
import io.gitlab.jfronny.inceptum.util.HashUtils;
import io.gitlab.jfronny.inceptum.util.Utils;
@ -22,7 +22,7 @@ public final class ModrinthModSource implements ModSource {
private final String versionId;
private final ModrinthVersion current;
private final ModrinthMod mod;
private final ModrinthProject mod;
public ModrinthModSource(String versionId) throws IOException {
this.versionId = versionId;
@ -111,6 +111,6 @@ public final class ModrinthModSource implements ModSource {
}
public String getModId() {
return current.mod_id;
return current.project_id;
}
}