From a6227e2c86763109694c82c79662244160cf9a42 Mon Sep 17 00:00:00 2001 From: Jacob McDonnell Date: Wed, 13 Apr 2022 23:14:59 -0400 Subject: Initial Commit --- src/META-INF/MANIFEST.MF | 3 +++ src/cfetch/main.java | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/META-INF/MANIFEST.MF create mode 100644 src/cfetch/main.java (limited to 'src') diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000..8d55dcf --- /dev/null +++ b/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: cfetch.main + 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 response = client.send(request, + HttpResponse.BodyHandlers.ofString()); + + String jsonString = response.body(); + return new JSONObject(jsonString); + } +} \ No newline at end of file -- cgit v1.2.3