From 40fcc66c21497d1745dbe9bb01836801b8949e29 Mon Sep 17 00:00:00 2001 From: Max Rydahl Andersen Date: Thu, 26 Sep 2024 19:06:27 +0200 Subject: [PATCH] move to java 23 --- mkbsd.java | 123 ++++++++++++++++++++++++++--------------------------- 1 file changed, 60 insertions(+), 63 deletions(-) diff --git a/mkbsd.java b/mkbsd.java index d9cf7de..bae1ac5 100644 --- a/mkbsd.java +++ b/mkbsd.java @@ -1,12 +1,12 @@ //DEPS com.fasterxml.jackson.core:jackson-databind:2.13.3 //DEPS com.fasterxml.jackson.core:jackson-core:2.13.3 //DEPS com.fasterxml.jackson.core:jackson-annotations:2.13.3 - +//JAVA 23+ +//PREVIEW import static java.lang.System.out; import static java.nio.file.Files.*; import static java.util.concurrent.TimeUnit.*; -import java.io.IOException; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; @@ -18,79 +18,77 @@ import java.util.Map; import com.fasterxml.jackson.databind.ObjectMapper; -public class mkbsd { +String URL = "https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s"; +HttpClient client = HttpClient.newHttpClient(); +ObjectMapper mapper = new ObjectMapper(); - private static final String URL = "https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s"; - private static final HttpClient client = HttpClient.newHttpClient(); - private static final ObjectMapper mapper = new ObjectMapper(); - - public static void main(String[] args) throws Exception { +void main() throws Exception { asciiArt(); SECONDS.sleep(5); mainSync(); - } +} - private static void mainSync() throws Exception { - var jsonData = fetchJson(URL); - var data = (Map) jsonData.get("data"); - if (data == null) { - throw new RuntimeException("⛔ JSON does not have a 'data' property at its root."); - } +void mainSync() throws Exception { + var jsonData = fetchJson(URL); + var data = (Map) jsonData.get("data"); + if (data == null) { + throw new RuntimeException("⛔ JSON does not have a 'data' property at its root."); + } - var downloadDir = Paths.get("downloads"); - if (!exists(downloadDir)) { - createDirectory(downloadDir); - out.println("📁 Created directory: " + downloadDir.toAbsolutePath()); - } + var downloadDir = Paths.get("downloads"); + if (!exists(downloadDir)) { + createDirectory(downloadDir); + out.println("📁 Created directory: " + downloadDir.toAbsolutePath()); + } - for (var entry : data.entrySet()) { - if (entry.getValue() instanceof Map && ((Map) entry.getValue()).get("dhd") != null) { - var imageUrl = (String) ((Map) entry.getValue()).get("dhd"); - var parsedUrl = URI.create(imageUrl); - out.println("🔍 Found image URL!"); + for (var entry : data.entrySet()) { + if (entry.getValue() instanceof Map && ((Map) entry.getValue()).get("dhd") != null) { + var imageUrl = (String) ((Map) entry.getValue()).get("dhd"); + var parsedUrl = URI.create(imageUrl); + out.println("🔍 Found image URL!"); - var filename = parsedUrl.getPath().substring(parsedUrl.getPath().lastIndexOf('/')+1); - - var filePath = downloadDir.resolve(filename); - if(!filePath.toFile().exists()) { - downloadImage(imageUrl, filePath); - out.println("🖼️ Saved image to " + filePath); - } else { - out.println("Already saved " + filePath); - } - - MILLISECONDS.sleep(250); + var filename = parsedUrl.getPath().substring(parsedUrl.getPath().lastIndexOf('/')+1); + + var filePath = downloadDir.resolve(filename); + if(!filePath.toFile().exists()) { + downloadImage(imageUrl, filePath); + out.println("🖼️ Saved image to " + filePath); + } else { + out.println("Already saved " + filePath); } + + MILLISECONDS.sleep(250); } - } - - private static Map fetchJson(String url) throws Exception { - var request = HttpRequest.newBuilder() - .uri(URI.create(url)) - .build(); - - var response = client.send(request, HttpResponse.BodyHandlers.ofString()); - if (response.statusCode() != 200) { - throw new RuntimeException("⛔ Failed to fetch JSON file: " + response.statusCode()); } - return mapper.readValue(response.body(), Map.class); +} + +Map fetchJson(String url) throws Exception { + var request = HttpRequest.newBuilder() + .uri(URI.create(url)) + .build(); + + var response = client.send(request, HttpResponse.BodyHandlers.ofString()); + if (response.statusCode() != 200) { + throw new RuntimeException("⛔ Failed to fetch JSON file: " + response.statusCode()); } + return mapper.readValue(response.body(), Map.class); +} - private static void downloadImage(String imageUrl, Path filePath) throws Exception { - var request = HttpRequest.newBuilder() - .uri(URI.create(imageUrl)) - .build(); +void downloadImage(String imageUrl, Path filePath) throws Exception { + var request = HttpRequest.newBuilder() + .uri(URI.create(imageUrl)) + .build(); - var response = client.send(request, HttpResponse.BodyHandlers.ofByteArray()); - if (response.statusCode() != 200) { - throw new RuntimeException("Failed to download image: " + response.statusCode()); - } - Files.write(filePath, response.body()); + var response = client.send(request, HttpResponse.BodyHandlers.ofByteArray()); + if (response.statusCode() != 200) { + throw new RuntimeException("Failed to download image: " + response.statusCode()); } + Files.write(filePath, response.body()); +} - private static void asciiArt() { - out.println(""" - /$$ /$$ /$$ /$$ /$$$$$$$ /$$$$$$ /$$$$$$$ +void asciiArt() { + out.println(""" +/$$ /$$ /$$ /$$ /$$$$$$$ /$$$$$$ /$$$$$$$ | $$$ /$$$| $$ /$$/| $$__ $$ /$$__ $$| $$__ $$ | $$$$ /$$$$| $$ /$$/ | $$ \\ $$| $$ \\__/| $$ \\ $$ | $$ $$/$$ $$| $$$$$/ | $$$$$$$ | $$$$$$ | $$ | $$ @@ -98,7 +96,6 @@ public class mkbsd { | $$\\ $ | $$| $$\\ $$ | $$ \\ $$ /$$ \\ $$| $$ | $$ | $$ \\/ | $$| $$ \\ $$| $$$$$$$/| $$$$$$/| $$$$$$$/ |__/ |__/|__/ \\__/|_______/ \\______/ |_______/"""); - out.println(""); - out.println("🤑 Starting downloads from your favorite sellout grifter's wallpaper app..."); - } -} \ No newline at end of file + out.println(""); + out.println("🤑 Starting downloads from your favorite sellout grifter's wallpaper app..."); +}