Skip to content

Commit

Permalink
Avoid JSONObject <-> String <-> stream conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
maddie480 committed May 4, 2024
1 parent 3050ddd commit 0ad950d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,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.6.0.jar`.
This will build the project to `target/update-checker-0.6.1.jar`.

### Running the project

Expand All @@ -213,7 +213,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.6.0.jar [minutes]
java -jar update-checker-0.6.1.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>ovh.maddie480.everest.updatechecker</groupId>
<artifactId>update-checker</artifactId>
<version>0.6.0</version>
<version>0.6.1</version>

<properties>
<jdk.version>21</jdk.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -21,8 +22,6 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import static java.nio.charset.StandardCharsets.UTF_8;

public class DatabaseUpdater {
private static final Logger log = LoggerFactory.getLogger(DatabaseUpdater.class);

Expand Down Expand Up @@ -181,7 +180,7 @@ private void crawlModsFromCategoryFully(String category) throws IOException {
try (InputStream is = ConnectionUtils.openStreamWithTimeout("https://gamebanana.com/apiv10/" + category + "/Index?_nPage=1&_nPerpage=" + incrementalPageSize +
"&_aFilters[Generic_Game]=6460&_sSort=Generic_LatestModified")) {

return new JSONObject(IOUtils.toString(is, UTF_8)).getJSONArray("_aRecords").getJSONObject(0).getInt("_tsDateModified");
return new JSONObject(new JSONTokener(is)).getJSONArray("_aRecords").getJSONObject(0).getInt("_tsDateModified");
} catch (JSONException e) {
// turn JSON parse errors into IOExceptions to trigger a retry.
throw new IOException(e);
Expand All @@ -199,7 +198,7 @@ private void crawlModsFromCategoryFully(String category) throws IOException {
"_tsDateAdded,_tsDateModified,_tsDateUpdated,_aPreviewMedia,_sProfileUrl,_bIsNsfw" +
"&_sOrderBy=_idRow,ASC&_nPage=" + thisPage + "&_nPerpage=" + fullPageSize)) {

return new JSONArray(IOUtils.toString(is, UTF_8));
return new JSONArray(new JSONTokener(is));
} catch (JSONException e) {
// turn JSON parse errors into IOExceptions to trigger a retry.
throw new IOException(e);
Expand Down Expand Up @@ -238,7 +237,7 @@ private void crawlModsFromCategoryIncrementally(String category) throws IOExcept
try (InputStream is = ConnectionUtils.openStreamWithTimeout("https://gamebanana.com/apiv10/" + category + "/Index?_nPage=" + thisPage +
"&_nPerpage=" + incrementalPageSize + "&_aFilters[Generic_Game]=6460&_sSort=Generic_LatestModified")) {

return new JSONObject(IOUtils.toString(is, UTF_8)).getJSONArray("_aRecords");
return new JSONObject(new JSONTokener(is)).getJSONArray("_aRecords");
} catch (JSONException e) {
// turn JSON parse errors into IOExceptions to trigger a retry.
throw new IOException(e);
Expand All @@ -258,7 +257,7 @@ private void crawlModsFromCategoryIncrementally(String category) throws IOExcept
"_csvProperties=_idRow,_sName,_aFiles,_aSubmitter,_sDescription,_sText,_nLikeCount,_nViewCount,_nDownloadCount,_aCategory," +
"_tsDateAdded,_tsDateModified,_tsDateUpdated,_aPreviewMedia,_sProfileUrl,_bIsNsfw&ts=" + System.currentTimeMillis())) {

return new JSONObject(IOUtils.toString(is, UTF_8));
return new JSONObject(new JSONTokener(is));
} catch (JSONException e) {
// turn JSON parse errors into IOExceptions to trigger a retry.
throw new IOException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package ovh.maddie480.everest.updatechecker;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -15,8 +15,6 @@
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static java.nio.charset.StandardCharsets.UTF_8;

public class ModSearchDatabaseBuilder {
private static final Logger log = LoggerFactory.getLogger(ModSearchDatabaseBuilder.class);

Expand Down Expand Up @@ -133,7 +131,7 @@ void addMod(String itemtype, int itemid, JSONObject mod) throws IOException {
if (mod.getBoolean("_bIsNsfw")) {
// mod has content warnings! we need to check which ones.
try (InputStream is = ConnectionUtils.openStreamWithTimeout("https://gamebanana.com/apiv11/" + itemtype + "/" + itemid + "/ProfilePage")) {
JSONObject o = new JSONObject(IOUtils.toString(is, UTF_8));
JSONObject o = new JSONObject(new JSONTokener(is));
redactScreenshots = !"show".equals(o.getString("_sInitialVisibility"));

List<String> contentWarnings = new ArrayList<>();
Expand Down Expand Up @@ -221,7 +219,7 @@ void saveSearchDatabase(boolean full) throws IOException {
// get featured mods and fill in the info for mods accordingly.
JSONObject featured = ConnectionUtils.runWithRetry(() -> {
try (InputStream is = ConnectionUtils.openStreamWithTimeout("https://gamebanana.com/apiv8/Game/6460/TopSubs")) {
return new JSONObject(IOUtils.toString(is, UTF_8));
return new JSONObject(new JSONTokener(is));
} catch (JSONException e) {
// turn JSON parse errors into IOExceptions to trigger a retry.
throw new IOException(e);
Expand Down Expand Up @@ -268,7 +266,7 @@ private void assignCategoryNamesToMods(String itemtype) throws IOException {
try (InputStream is = ConnectionUtils.openStreamWithTimeout("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));
return new JSONArray(new JSONTokener(is));
} catch (JSONException e) {
// turn JSON parse errors into IOExceptions to trigger a retry.
throw new IOException(e);
Expand Down

0 comments on commit 0ad950d

Please sign in to comment.