summaryrefslogtreecommitdiff
path: root/src/cfetch/main.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/cfetch/main.java')
-rw-r--r--src/cfetch/main.java59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/cfetch/main.java b/src/cfetch/main.java
new file mode 100644
index 0000000..fccb005
--- /dev/null
+++ b/src/cfetch/main.java
@@ -0,0 +1,59 @@
+package cfetch;
+
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.io.IOException;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+
+import org.json.*;
+
+public class main {
+ public static void main(String[] args) throws IOException, InterruptedException {
+ /* System date is required to make the API call use local date not server date */
+ DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
+ LocalDateTime now = LocalDateTime.now();
+ JSONObject obj;
+
+ if (args.length > 0) {
+ obj = request("http://calapi.inadiutorium.cz/api/v0/en/calendars/general-en/"
+ + args[0]);
+ } else {
+ obj = request("http://calapi.inadiutorium.cz/api/v0/en/calendars/general-en/"
+ + dtf.format(now));
+ }
+
+ JSONArray arr = obj.getJSONArray("celebrations");
+
+ String date, season, title, color, celebration, output;
+
+ date = obj.getString("date");
+ season = obj.getString("season");
+ title = arr.getJSONObject(0).getString("title");
+ color = arr.getJSONObject(0).getString("colour");
+
+ output = date + "\nToday: " + title + "\nSeason: " + season + "\nColor: " + color;
+
+ if (arr.length() > 1) {
+ celebration = arr.getJSONObject(1).getString("title");
+ output += "\nCelebration: " + celebration;
+ }
+
+ System.out.println(output);
+ }
+
+ private static JSONObject request(String url) throws IOException, InterruptedException {
+ HttpClient client = HttpClient.newHttpClient();
+ HttpRequest request = HttpRequest.newBuilder()
+ .uri(URI.create(url))
+ .build();
+
+ HttpResponse<String> response = client.send(request,
+ HttpResponse.BodyHandlers.ofString());
+
+ String jsonString = response.body();
+ return new JSONObject(jsonString);
+ }
+} \ No newline at end of file