mkbsd/mkbsd.java

102 lines
3.6 KiB
Java
Raw Normal View History

2024-09-26 13:42:47 +00:00
//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
2024-09-26 17:06:27 +00:00
//JAVA 23+
//PREVIEW
2024-09-26 13:42:47 +00:00
import static java.lang.System.out;
import static java.nio.file.Files.*;
import static java.util.concurrent.TimeUnit.*;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
2024-09-26 17:06:27 +00:00
String URL = "https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s";
HttpClient client = HttpClient.newHttpClient();
ObjectMapper mapper = new ObjectMapper();
2024-09-26 13:42:47 +00:00
2024-09-26 17:06:27 +00:00
void main() throws Exception {
2024-09-26 13:42:47 +00:00
asciiArt();
SECONDS.sleep(5);
mainSync();
2024-09-26 17:06:27 +00:00
}
2024-09-26 13:42:47 +00:00
2024-09-26 17:06:27 +00:00
void mainSync() throws Exception {
var jsonData = fetchJson(URL);
var data = (Map<String, Object>) jsonData.get("data");
if (data == null) {
throw new RuntimeException("⛔ JSON does not have a 'data' property at its root.");
}
2024-09-26 13:42:47 +00:00
2024-09-26 17:06:27 +00:00
var downloadDir = Paths.get("downloads");
if (!exists(downloadDir)) {
createDirectory(downloadDir);
out.println("📁 Created directory: " + downloadDir.toAbsolutePath());
}
2024-09-26 13:42:47 +00:00
2024-09-26 17:06:27 +00:00
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);
2024-09-26 13:42:47 +00:00
}
2024-09-26 17:06:27 +00:00
MILLISECONDS.sleep(250);
2024-09-26 13:42:47 +00:00
}
2024-09-26 17:06:27 +00:00
}
}
2024-09-26 13:42:47 +00:00
2024-09-26 17:06:27 +00:00
Map<String, Object> fetchJson(String url) throws Exception {
var request = HttpRequest.newBuilder()
.uri(URI.create(url))
.build();
2024-09-26 13:42:47 +00:00
2024-09-26 17:06:27 +00:00
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
throw new RuntimeException("⛔ Failed to fetch JSON file: " + response.statusCode());
2024-09-26 13:42:47 +00:00
}
2024-09-26 17:06:27 +00:00
return mapper.readValue(response.body(), Map.class);
}
2024-09-26 13:42:47 +00:00
2024-09-26 17:06:27 +00:00
void downloadImage(String imageUrl, Path filePath) throws Exception {
var request = HttpRequest.newBuilder()
.uri(URI.create(imageUrl))
.build();
2024-09-26 13:42:47 +00:00
2024-09-26 17:06:27 +00:00
var response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
if (response.statusCode() != 200) {
throw new RuntimeException("Failed to download image: " + response.statusCode());
2024-09-26 13:42:47 +00:00
}
2024-09-26 17:06:27 +00:00
Files.write(filePath, response.body());
}
2024-09-26 13:42:47 +00:00
2024-09-26 17:06:27 +00:00
void asciiArt() {
out.println("""
/$$ /$$ /$$ /$$ /$$$$$$$ /$$$$$$ /$$$$$$$
2024-09-26 13:42:47 +00:00
| $$$ /$$$| $$ /$$/| $$__ $$ /$$__ $$| $$__ $$
| $$$$ /$$$$| $$ /$$/ | $$ \\ $$| $$ \\__/| $$ \\ $$
| $$ $$/$$ $$| $$$$$/ | $$$$$$$ | $$$$$$ | $$ | $$
| $$ $$$| $$| $$ $$ | $$__ $$ \\____ $$| $$ | $$
| $$\\ $ | $$| $$\\ $$ | $$ \\ $$ /$$ \\ $$| $$ | $$
| $$ \\/ | $$| $$ \\ $$| $$$$$$$/| $$$$$$/| $$$$$$$/
|__/ |__/|__/ \\__/|_______/ \\______/ |_______/""");
2024-09-26 17:06:27 +00:00
out.println("");
out.println("🤑 Starting downloads from your favorite sellout grifter's wallpaper app...");
}