Updated for compatibility with the new Harble API messages JSON structure

* Expose a static helper method 'updateCache(String,String)' for updating
cache files via a string body instead of a JSONObject
* Updated the HARBLE_API_URL to point towards the new API output path
* Changed the way the messages JSONObject is parsed/handled
This commit is contained in:
ArachisH 2019-01-18 19:45:33 -08:00 committed by ArachisH
parent 7f4f73603d
commit 805d8798aa
3 changed files with 26 additions and 23 deletions

View File

@ -56,12 +56,15 @@ public class Cacher {
return new JSONObject();
}
public static void updateCache(JSONObject contents, String cache_filename) {
updateCache(contents.toString(), cache_filename);
}
public static void updateCache(String content, String cache_filename){
File parent_dir = new File(getCacheDir());
parent_dir.mkdirs();
try (FileWriter file = new FileWriter(new File(getCacheDir(), cache_filename))) {
file.write(contents.toString());
file.write(content);
file.flush();
} catch (IOException e) {

View File

@ -2,6 +2,7 @@ package gearth.misc.harble_api;
import gearth.misc.Cacher;
import gearth.protocol.HMessage;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
@ -93,7 +94,7 @@ public class HarbleAPI {
}
}
private void addMessage(HMessage.Side side, JSONObject object, String id) {
private void addMessage(HMessage.Side side, JSONObject object) {
String name;
try {
name = object.getString("Name");
@ -101,7 +102,7 @@ public class HarbleAPI {
name = null;
}
String hash = object.getString("Hash");
Integer headerId = Integer.parseInt(id);
Integer headerId = object.getInt("Id");
String structure;
try {
@ -139,30 +140,32 @@ public class HarbleAPI {
private void parse(JSONObject object) {
try {
JSONObject incoming = object.getJSONObject("Incoming");
JSONObject outgoing = object.getJSONObject("Outgoing");
JSONArray incoming = object.getJSONArray("Incoming");
JSONArray outgoing = object.getJSONArray("Outgoing");
if (incoming != null && outgoing != null) {
for (String key : incoming.keySet()) {
for (int i = 0; i < incoming.length(); i++) {
try {
JSONObject inMsg = incoming.getJSONObject(key);
addMessage(HMessage.Side.TOCLIENT, inMsg, key);
} catch (Exception e) {
JSONObject message = incoming.getJSONObject(i);
addMessage(HMessage.Side.TOCLIENT, message);
}
catch (Exception e){
e.printStackTrace();
}
}
for (String key : outgoing.keySet()) {
for (int i = 0; i < outgoing.length(); i++) {
try{
JSONObject outMsg = outgoing.getJSONObject(key);
addMessage(HMessage.Side.TOSERVER, outMsg, key);
} catch (Exception e) {
JSONObject message = outgoing.getJSONObject(i);
addMessage(HMessage.Side.TOSERVER, message);
}
catch (Exception e){
e.printStackTrace();
}
}
}
} catch (Exception e) {
success = false;
}
}
public HarbleMessage getHarbleMessageFromHeaderId(HMessage.Side side, int headerId) {

View File

@ -32,7 +32,7 @@ import java.io.IOException;
public class HarbleAPIFetcher {
public static final String CACHE_PREFIX = "HARBLE_API-";
public static final String HARBLE_API_URL = "https://api.harble.net/revisions/$hotelversion$.json";
public static final String HARBLE_API_URL = "https://api.harble.net/messages/$hotelversion$.json";
//latest fetched
public static HarbleAPI HARBLEAPI = null;
@ -46,13 +46,10 @@ public class HarbleAPIFetcher {
else {
Connection connection = Jsoup.connect(HARBLE_API_URL.replace("$hotelversion$", hotelversion)).ignoreContentType(true);
try {
Document doc = connection.get();
Connection.Response response = connection.response();
Connection.Response response = connection.execute();
if (response.statusCode() == 200) {
String s = doc.body().toString();
s = s.substring(6, s.length() - 7);
JSONObject object = new JSONObject(s);
Cacher.updateCache(object, cacheName);
String messagesBodyJson = response.body();
Cacher.updateCache(messagesBodyJson, cacheName);
HARBLEAPI = new HarbleAPI(hotelversion);
}
else {
@ -66,7 +63,7 @@ public class HarbleAPIFetcher {
}
public static void main(String[] args) {
fetch("PRODUCTION-201810171204-70166177");
fetch("PRODUCTION-201901141210-114421986");
HarbleAPI api = HARBLEAPI;
HarbleAPI.HarbleMessage haMessage = api.getHarbleMessageFromHeaderId(HMessage.Side.TOSERVER, 525);