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

refactor: use modern NodeJS code #26

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ MKBSD comes in two variants! Node.js and Python.
### Running in Node.js

1. Ensure you have Node.js installed.
2. Run `node mkbsd.js`
2. Run `node mkbsd.mjs` or `./mkbsd.mjs` if you're on a Unix-like system.
3. Wait a little.
4. All wallpapers are now in a newly created `downloads` subfolder.

Expand Down
75 changes: 0 additions & 75 deletions mkbsd.js

This file was deleted.

72 changes: 72 additions & 0 deletions mkbsd.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env node

// Copyright 2024 Nadim Kobeissi
// Licensed under the WTFPL License

import { stat, mkdir, writeFile } from 'node:fs/promises';
import { extname } from 'node:path';
import { setTimeout } from 'node:timers/promises'

console.info(`
/$$ /$$ /$$ /$$ /$$$$$$$ /$$$$$$ /$$$$$$$
| $$$ /$$$| $$ /$$/| $$__ $$ /$$__ $$| $$__ $$
| $$$$ /$$$$| $$ /$$/ | $$ \\ $$| $$ \\__/| $$ \\ $$
| $$ $$/$$ $$| $$$$$/ | $$$$$$$ | $$$$$$ | $$ | $$
| $$ $$$| $$| $$ $$ | $$__ $$ \\____ $$| $$ | $$
| $$\\ $ | $$| $$\\ $$ | $$ \\ $$ /$$ \\ $$| $$ | $$
| $$ \\/ | $$| $$ \\ $$| $$$$$$$/| $$$$$$/| $$$$$$$/
|__/ |__/|__/ \\__/|_______/ \\______/ |_______/`);
console.info(``);
console.info(
`🤑 Starting downloads from your favorite sellout grifter's wallpaper app...`
);

const url = 'https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s';
const downloadsDir = new URL('downloads/', import.meta.url);

try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`⛔ Failed to fetch JSON file: ${response.statusText}`);
}
const jsonData = await response.json();
const data = jsonData.data;
if (!data) {
throw new Error('⛔ JSON does not have a "data" property at its root.');
}

try {
await stat(downloadsDir);
} catch {
await mkdir(downloadsDir);
console.info(`📁 Created directory: ${downloadsDir.pathname}`);
}

let fileIndex = 1;
for (const subproperty of Object.values(data)) {
if (subproperty?.dhd) {
const imageUrl = subproperty.dhd;
console.info(`🔍 Found image URL!`);
await setTimeout(100);
const ext = extname(new URL(imageUrl).pathname) || '.jpg';
const filename = `${fileIndex}${ext}`;
const filePath = new URL(filename, downloadsDir);
await downloadImage(imageUrl, filePath);
console.info(`🖼️ Saved image to ${filePath.pathname}`);
fileIndex++;
await setTimeout(250);
}
}
} catch (error) {
console.error(`Error: ${error.message}`);
}

async function downloadImage(url, filePath) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to download image: ${response.statusText}`);
}
const arrayBuffer = await response.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
await writeFile(filePath, buffer);
}