Skip to content

Instantly share code, notes, and snippets.

@jan-krueger
Created August 27, 2018 14:18
Show Gist options
  • Save jan-krueger/d64fb0d12e949d9f7f22e18ec4083a00 to your computer and use it in GitHub Desktop.
Save jan-krueger/d64fb0d12e949d9f7f22e18ec4083a00 to your computer and use it in GitHub Desktop.
package de.sweetcode.scmh;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import okhttp3.*;
import java.awt.*;
import java.io.IOException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
public class SCMH {
private final static String SEARCH_QUERY = "mercury";
private final static OkHttpClient client = new OkHttpClient.Builder().build();
private final static Gson gson = new Gson();
private static String oldLocation = null;
public static void main(String[] args) throws InterruptedException {
while (true) {
fetchData();
Thread.sleep(TimeUnit.MINUTES.toMillis(1));
}
}
private static void fetchData() {
Request postRequest = new Request.Builder()
.url("https://robertsspaceindustries.com/api/starmap/bootup")
.post(RequestBody.create(MediaType.get("application/json"), ""))
.build();
try {
Response postResponse = client.newCall(postRequest).execute();
JsonObject root = gson.fromJson(postResponse.body().string(), JsonObject.class);
JsonArray systems = root.getAsJsonObject("data").getAsJsonObject("systems").getAsJsonArray("resultset");
final boolean[] breakLoop = {false};
for (JsonElement system : systems) {
if (breakLoop[0]) break;
final String code = system.getAsJsonObject().get("code").getAsString();
Request systemRequest = new Request.Builder()
.url(String.format("https://robertsspaceindustries.com/api/starmap/star-systems/%s", code))
.post(RequestBody.create(MediaType.get("application/json"), ""))
.build();
Response searchResponse = client.newCall(systemRequest).execute();
JsonObject systemResponse = gson.fromJson(searchResponse.body().string(), JsonObject.class);
JsonObject systemData = systemResponse.getAsJsonObject("data").getAsJsonArray("resultset").get(0).getAsJsonObject();
systemData.getAsJsonArray("celestial_objects").forEach(object -> {
JsonObject celestial = object.getAsJsonObject();
if (
(!celestial.get("type").isJsonNull() && celestial.get("type").getAsString().equals("MANMADE")) &&
(!celestial.get("code").isJsonNull() && celestial.get("code").getAsString().toLowerCase().contains(SEARCH_QUERY))
) {
final String location = String.format("%s >> Distance: %f Latitude: %f Longitude: %f",
systemData.get("name").getAsString(),
Double.parseDouble(celestial.get("distance").toString().replaceAll("\"", "")),
Double.parseDouble(celestial.get("latitude").toString().replaceAll("\"", "")),
Double.parseDouble(celestial.get("longitude").toString().replaceAll("\"", ""))
);
if(!location.equals(oldLocation)) {
System.out.println(String.format("%s (UTC) - %s", Timestamp.valueOf(LocalDateTime.now()).toLocalDateTime().minusHours(2).toString(), location));
try {
oldLocation = location;
displayTray(location);
} catch (AWTException e) {
e.printStackTrace();
}
}
breakLoop[0] = true;
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void displayTray(String location) throws AWTException {
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().createImage("icon.png");
TrayIcon trayIcon = new TrayIcon(image, "A");
trayIcon.setImageAutoSize(true);
//Set tooltip text for the tray icon
trayIcon.setToolTip("B");
tray.add(trayIcon);
trayIcon.displayMessage("NEW LOCATION", location, TrayIcon.MessageType.ERROR);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment