Skip to content

Instantly share code, notes, and snippets.

@C4N4D4M4N
Last active December 13, 2018 23:51
Show Gist options
  • Save C4N4D4M4N/a28e31abdeb1d60313431ee5006c5f80 to your computer and use it in GitHub Desktop.
Save C4N4D4M4N/a28e31abdeb1d60313431ee5006c5f80 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeUnit;
public class OSBuddyTest {
@SuppressWarnings("InfiniteLoopStatement")
public static void main(String[] args) {
String[] names = {"GoogleAPI", "OSBuddyAPI"};
String[] URLs = {"https://storage.googleapis.com/osbuddy-exchange/summary.json", "https://rsbuddy.com/exchange/summary.json"};
String[] summaries = {null, null};
long[] times = {0, 0};
boolean[] first = {true, true};
while (true) {
for (int a = 0; a < 2; a++) {
try {
HttpURLConnection connectURL = (HttpURLConnection) new URL(URLs[a]).openConnection();
connectURL.setRequestMethod("GET");
connectURL.setConnectTimeout(30000);
connectURL.setReadTimeout(15000);
try (InputStream is = connectURL.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr)) {
String summary = br.readLine();
if (summaries[a] == null) { // Initialize, no println
summaries[a] = summary;
times[a] = System.currentTimeMillis();
System.out.println("Initialized " + names[a]);
} else if (!summaries[a].equals(summary)) {
long now = System.currentTimeMillis();
if (!first[a]) {
System.out.println(names[a] + " updated after " + ((now - times[a]) / 60000D) + " minutes");
}
first[a] = false;
times[a] = now;
summaries[a] = summary;
}
}
} catch (IOException e) {e.printStackTrace();}
}
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(5));
} catch (InterruptedException e) {e.printStackTrace();}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment