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(); return new JSONObject();
} }
public static void updateCache(JSONObject contents, String cache_filename) { 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()); File parent_dir = new File(getCacheDir());
parent_dir.mkdirs(); parent_dir.mkdirs();
try (FileWriter file = new FileWriter(new File(getCacheDir(), cache_filename))) { try (FileWriter file = new FileWriter(new File(getCacheDir(), cache_filename))) {
file.write(contents.toString()); file.write(content);
file.flush(); file.flush();
} catch (IOException e) { } catch (IOException e) {

View File

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

View File

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