Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java version #48

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ _Because selling out is bad_

## How to use

MKBSD comes in two variants! Node.js and Python.
MKBSD comes in two variants! Node.js, Python and Java.

### Running in Node.js

Expand All @@ -30,6 +30,13 @@ MKBSD comes in two variants! Node.js and Python.
4. Wait a little.
5. All wallpapers are now in a newly created `downloads` subfolder.

### Running in Java w/JBang

1. Ensure you have JBang installed.
2. Run `jbang mkbsd.java`
3. Wait a little.
4. All wallpapers are now in a newly created `downloads` subfolder.

## FAQ

### Q: What's the story behind this?
Expand Down
101 changes: 101 additions & 0 deletions mkbsd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//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.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;

String URL = "https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s";
HttpClient client = HttpClient.newHttpClient();
ObjectMapper mapper = new ObjectMapper();

void main() throws Exception {
asciiArt();
SECONDS.sleep(5);
mainSync();
}

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.");
}

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!");

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);
}
}
}

Map<String, Object> 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);
}

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());
}

void asciiArt() {
out.println("""
/$$ /$$ /$$ /$$ /$$$$$$$ /$$$$$$ /$$$$$$$
| $$$ /$$$| $$ /$$/| $$__ $$ /$$__ $$| $$__ $$
| $$$$ /$$$$| $$ /$$/ | $$ \\ $$| $$ \\__/| $$ \\ $$
| $$ $$/$$ $$| $$$$$/ | $$$$$$$ | $$$$$$ | $$ | $$
| $$ $$$| $$| $$ $$ | $$__ $$ \\____ $$| $$ | $$
| $$\\ $ | $$| $$\\ $$ | $$ \\ $$ /$$ \\ $$| $$ | $$
| $$ \\/ | $$| $$ \\ $$| $$$$$$$/| $$$$$$/| $$$$$$$/
|__/ |__/|__/ \\__/|_______/ \\______/ |_______/""");
out.println("");
out.println("🤑 Starting downloads from your favorite sellout grifter's wallpaper app...");
}