Skip to content

Commit

Permalink
Add category IDs to all mods in mod search database
Browse files Browse the repository at this point in the history
  • Loading branch information
maddie480 committed Aug 23, 2022
1 parent 61b7ece commit d8cb114
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 35 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ Get Maven, then run the following command at the project root:
mvn clean package
```

This will build the project to `target/update-checker-0.1.2.jar`.
This will build the project to `target/update-checker-0.1.3.jar`.

### Running the project

Expand All @@ -216,7 +216,7 @@ First, follow these steps to set it up:
Then, to run the project, browse to where the JAR is in a terminal / command prompt, then run

```
java -jar update-checker-0.1.2.jar [minutes]
java -jar update-checker-0.1.3.jar [minutes]
```
[minutes] is the wait delay in minutes between two GameBanana checks (defaults to 30). Be aware that the program makes ~13 API calls per check, and that the GameBanana API has a cap at 250 requests/hour.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.max480.everest.updatechecker</groupId>
<artifactId>update-checker</artifactId>
<version>0.1.2</version>
<version>0.1.3</version>

<properties>
<jdk.version>1.8</jdk.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,8 @@ public Map<String, Object> toMap() {
}
map.put("MirroredScreenshots", mirroredScreenshots);
map.put("Files", files);
if (categoryName != null) {
map.put("CategoryId", categoryId);
map.put("CategoryName", categoryName);
}
map.put("CategoryId", categoryId);
map.put("CategoryName", categoryName);
if (featured != null) {
map.put("Featured", featured);
}
Expand Down Expand Up @@ -175,9 +173,49 @@ void addMod(String itemtype, int itemid, JSONObject mod) {
* @throws IOException If the file couldn't be written, or something went wrong with getting author/mod category names.
*/
void saveSearchDatabase() throws IOException {
// assign category names to mods from all itemtypes...
for (String itemtype : modSearchInfo.stream().map(m -> m.gameBananaType).collect(Collectors.toSet())) {
assignCategoryNamesToMods(itemtype);
}

// ... then check that we did not miss any.
for (ModSearchInfo mod : modSearchInfo) {
if (mod.categoryName == null) {
throw new IOException("Category name for " + mod.gameBananaType + " category " + mod.categoryId + " was not found!");
}
}

// get featured mods and fill in the info for mods accordingly.
JSONObject featured = DatabaseUpdater.runWithRetry(() -> {
try (InputStream is = openStreamWithTimeout(new URL("https://gamebanana.com/apiv8/Game/6460/TopSubs"))) {
return new JSONObject(IOUtils.toString(is, UTF_8));
}
});
for (String category : featured.keySet()) {
int position = 0;
for (Object mod : featured.getJSONArray(category)) {
JSONObject modObject = (JSONObject) mod;
String itemtype = modObject.getString("_sModelName");
int itemid = modObject.getInt("_idRow");

int thisPosition = position;
modSearchInfo.stream()
.filter(i -> i.gameBananaType.equals(itemtype) && i.gameBananaId == itemid)
.forEach(i -> i.setFeatured(category, thisPosition));
position++;
}
}

// map ModSearchInfo's to Maps and save them.
try (FileWriter writer = new FileWriter("uploads/modsearchdatabase.yaml")) {
new Yaml().dump(modSearchInfo.stream().map(ModSearchInfo::toMap).collect(Collectors.toList()), writer);
}
}

private void assignCategoryNamesToMods(String itemtype) throws IOException {
// get the list of categories from GameBanana
JSONArray listOfCategories = DatabaseUpdater.runWithRetry(() -> {
try (InputStream is = openStreamWithTimeout(new URL("https://gamebanana.com/apiv8/ModCategory/ByGame?_aGameRowIds[]=6460&" +
try (InputStream is = openStreamWithTimeout(new URL("https://gamebanana.com/apiv8/" + itemtype + "Category/ByGame?_aGameRowIds[]=6460&" +
"_csvProperties=_idRow,_idParentCategoryRow,_sName&_sOrderBy=_idRow,ASC&_nPage=1&_nPerpage=50"))) {

return new JSONArray(IOUtils.toString(is, UTF_8));
Expand All @@ -202,7 +240,7 @@ void saveSearchDatabase() throws IOException {

// associate each mod to its root category.
for (ModSearchInfo info : modSearchInfo) {
if (info.gameBananaType.equals("Mod")) {
if (info.gameBananaType.equals(itemtype)) {
int category = info.categoryId;

// go up to the root category!
Expand All @@ -215,31 +253,5 @@ void saveSearchDatabase() throws IOException {
info.categoryName = categoryNames.get(category);
}
}

// get featured mods and fill in the info for mods accordingly.
JSONObject featured = DatabaseUpdater.runWithRetry(() -> {
try (InputStream is = openStreamWithTimeout(new URL("https://gamebanana.com/apiv8/Game/6460/TopSubs"))) {
return new JSONObject(IOUtils.toString(is, UTF_8));
}
});
for (String category : featured.keySet()) {
int position = 0;
for (Object mod : featured.getJSONArray(category)) {
JSONObject modObject = (JSONObject) mod;
String itemtype = modObject.getString("_sModelName");
int itemid = modObject.getInt("_idRow");

int thisPosition = position;
modSearchInfo.stream()
.filter(i -> i.gameBananaType.equals(itemtype) && i.gameBananaId == itemid)
.forEach(i -> i.setFeatured(category, thisPosition));
position++;
}
}

// map ModSearchInfo's to Maps and save them.
try (FileWriter writer = new FileWriter("uploads/modsearchdatabase.yaml")) {
new Yaml().dump(modSearchInfo.stream().map(ModSearchInfo::toMap).collect(Collectors.toList()), writer);
}
}
}

0 comments on commit d8cb114

Please sign in to comment.