Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #14 from Somrlik/master
Browse files Browse the repository at this point in the history
Replaced Curse API v2 with v3
  • Loading branch information
franckyi authored Oct 7, 2018
2 parents 9ceaa20 + 8352d1e commit 00ee13f
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 38 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
hs_err_pid*

.idea/

target/*
*.iml
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
<version>1.11.2</version>
</dependency>
</dependencies>
</project>
</project>
3 changes: 2 additions & 1 deletion src/main/java/com/github/franckyi/cmpdl/CMPDL.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public class CMPDL extends Application {
public static final String AUTHOR = "Franckyi";
public static final String TITLE = String.format("%s v%s by %s", NAME, VERSION, AUTHOR);

public static final String USER_AGENT = String.format("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0 %s/%s (%s)", NAME, VERSION, AUTHOR);
// As per the "Required reading" section on https://staging_cursemeta.dries007.net/docs
public static final String USER_AGENT = String.format("%s/%s (%s)", NAME, AUTHOR, VERSION);

public static final ExecutorService EXECUTOR_SERVICE = Executors.newCachedThreadPool();

Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/github/franckyi/cmpdl/CurseMetaAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
public class CurseMetaAPI {

private static final OkHttpClient CLIENT = new OkHttpClient();
private static final String URL = "http://cursemeta.dries007.net/api/v2/direct";
// Docs: https://staging_cursemeta.dries007.net/docs
private static final String URL = "https://staging_cursemeta.dries007.net/api/v3/direct";

public static Project getProject(int projectId) {
try {
return new Project(new JSONObject(get("/GetAddOn", projectId)));
return new Project(new JSONObject(get("/addon", projectId)));
} catch (JSONException e) {
e.printStackTrace();
Platform.runLater(() -> new Alert(Alert.AlertType.ERROR, String.format("Unknown project (%d)", projectId), ButtonType.OK).show());
Expand All @@ -32,7 +33,7 @@ public static Project getProject(int projectId) {

public static ProjectFilesList getProjectFiles(int projectId) {
try {
return new ProjectFilesList(new JSONArray(get("/GetAllFilesForAddOn", projectId)));
return new ProjectFilesList(new JSONArray(get("/addon", projectId, "/files")));
} catch (JSONException e) {
e.printStackTrace();
Platform.runLater(() -> new Alert(Alert.AlertType.ERROR, String.format("Unknown project (%d)", projectId), ButtonType.OK).show());
Expand All @@ -42,7 +43,7 @@ public static ProjectFilesList getProjectFiles(int projectId) {

public static ProjectFile getProjectFile(int projectId, int fileId) {
try {
return new ProjectFile(new JSONObject(get("/GetAddOnFile", projectId, fileId)));
return new ProjectFile(new JSONObject(get("/addon", projectId, "/file", fileId)));
} catch (JSONException e) {
e.printStackTrace();
Platform.runLater(() -> new Alert(Alert.AlertType.ERROR, String.format("Unknown project file (%d:%d)", projectId, fileId), ButtonType.OK).show());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public class DestinationPaneController implements Initializable, IContentControl
void actionChooseDestination(ActionEvent event) {
DirectoryChooser dc = new DirectoryChooser();
dc.setTitle("Choose the destination folder :");
String currentChosenDirectory = destinationField.getText();
if (currentChosenDirectory != null && ! "".equals(currentChosenDirectory)) {
dc.setInitialDirectory(new File(currentChosenDirectory));
}
File dst = dc.showDialog(CMPDL.stage);
if (dst != null) {
destinationField.setText(dst.getAbsolutePath());
Expand Down Expand Up @@ -120,7 +124,7 @@ public void setProjectAndFile(Project project, ProjectFile file) {
categoryLabel.setText(project.getCategoryName());
fileNameLabel.setText(file.getFileName());
mcVersionLabel.setText(file.getGameVersion());
releaseTypeLabel.setText(file.getFileType());
releaseTypeLabel.setText(file.getFileType().toString());
releaseTypeLabel.setTextFill(file.getColor());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.franckyi.cmpdl.model;

public enum FileReleaseType {
ALPHA,
BETA,
RELEASE,
}
28 changes: 24 additions & 4 deletions src/main/java/com/github/franckyi/cmpdl/model/IProjectFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,31 @@ public interface IProjectFile extends Comparable<IProjectFile> {

int getFileId();

/**
* Parses the integer type provided by api to FileReleaseType
*
* @param releaseType the integer received from API
* @return FileReleaseType
* @throws Exception Thrown when the release type could not be determined
*/
default FileReleaseType parseFileType(int releaseType) throws Exception {
switch (releaseType) {
case 1:
return FileReleaseType.RELEASE;
case 2:
return FileReleaseType.BETA;
case 3:
return FileReleaseType.ALPHA;
default:
throw new Exception("Failed to find release type for id " + releaseType);
}
}

String getFileName();

String getGameVersion();

String getFileType();
FileReleaseType getFileType();

@Override
default int compareTo(IProjectFile o) {
Expand All @@ -22,11 +42,11 @@ default int compareTo(IProjectFile o) {

default Paint getColor() {
switch (getFileType()) {
case "Alpha":
case ALPHA:
return Color.web("#E49788");
case "Beta":
case BETA:
return Color.web("#7FA5C4");
case "Release":
case RELEASE:
return Color.web("#8CAF62");
default:
return Color.BLACK;
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/com/github/franckyi/cmpdl/model/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

public class Project {

private static final int PACKAGE_TYPE_MODPACK = 5;

private final int projectId;
private final String name;
private final String author;
Expand All @@ -21,16 +23,16 @@ public class Project {
private final List<ProjectFileMinimal> files;

public Project(JSONObject json) {
projectId = json.getInt("Id");
name = json.getString("Name");
author = json.getString("PrimaryAuthorName");
summary = json.getString("Summary");
logoUrl = json.getJSONArray("Attachments").getJSONObject(0).getString("Url");
url = json.getString("WebSiteURL");
categoryName = json.getString("PrimaryCategoryName");
categoryLogoUrl = json.getString("PrimaryCategoryAvatarUrl");
modpack = "ModPack".equals(json.getString("PackageType"));
JSONArray array = json.getJSONArray("GameVersionLatestFiles");
projectId = json.getInt("id");
name = json.getString("name");
author = json.getString("primaryAuthorName");
summary = json.getString("summary");
logoUrl = json.getJSONArray("attachments").getJSONObject(0).getString("url");
url = json.getString("websiteUrl");
categoryName = json.getString("primaryCategoryName");
categoryLogoUrl = json.getString("primaryCategoryAvatarUrl");
modpack = json.getInt("packageType") == PACKAGE_TYPE_MODPACK;
JSONArray array = json.getJSONArray("gameVersionLatestFiles");
files = new ArrayList<>(array.length());
for (int i = 0; i < array.length(); i++) {
files.add(new ProjectFileMinimal(array.getJSONObject(i)));
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/com/github/franckyi/cmpdl/model/ProjectFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ public class ProjectFile implements IProjectFile {
private final String fileName;
private final String fileNameOnDisk;
private final String gameVersion;
private final String fileType;
private FileReleaseType fileType;
private final int fileId;
private final String downloadUrl;

public ProjectFile(JSONObject json) {
fileName = json.getString("FileName");
fileNameOnDisk = json.getString("FileNameOnDisk");
gameVersion = json.getJSONArray("GameVersion").getString(0);
fileType = json.getString("ReleaseType");
fileId = json.getInt("Id");
downloadUrl = json.getString("DownloadURL");
fileName = json.getString("fileName");
fileNameOnDisk = json.getString("fileNameOnDisk");
gameVersion = json.getJSONArray("gameVersion").getString(0);
try {
fileType = parseFileType(json.getInt("releaseType"));
} catch (Exception e) {
fileType = FileReleaseType.ALPHA;
e.printStackTrace();
}
fileId = json.getInt("id");
downloadUrl = json.getString("downloadUrl");
}

@Override
Expand All @@ -40,7 +45,7 @@ public String getGameVersion() {
}

@Override
public String getFileType() {
public FileReleaseType getFileType() {
return fileType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ public class ProjectFileMinimal implements IProjectFile {

private final String fileName;
private final String gameVersion;
private final String fileType;
private FileReleaseType fileType;
private final int fileId;

public ProjectFileMinimal(JSONObject json) {
fileName = json.getString("ProjectFileName");
gameVersion = json.getString("GameVesion");
fileType = json.getString("FileType");
fileId = json.getInt("ProjectFileID");
ProjectFileMinimal(JSONObject json) {
fileName = json.getString("projectFileName");
gameVersion = json.getString("gameVersion");
try {
fileType = parseFileType(json.getInt("fileType"));
} catch (Exception e) {
fileType = FileReleaseType.ALPHA;
e.printStackTrace();
}
fileId = json.getInt("projectFileId");
}

public String getFileName() {
Expand All @@ -24,7 +29,8 @@ public String getGameVersion() {
return gameVersion;
}

public String getFileType() {
@Override
public FileReleaseType getFileType() {
return fileType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected void updateItem(IProjectFile item, boolean empty) {
} else {
fileName.setText(item.getFileName());
gameVersion.setText("for MC " + item.getGameVersion());
fileType.setText(item.getFileType());
fileType.setText(item.getFileType().toString());
fileType.setTextFill(item.getColor());
setGraphic(root);
}
Expand Down

0 comments on commit 00ee13f

Please sign in to comment.