Added rust version

This commit is contained in:
cloudgazing 2024-09-25 06:40:43 +03:00
parent 82e50c64f0
commit 5e544df777
4 changed files with 1334 additions and 0 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
.DS_Store
downloads
target

1236
rust/Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

9
rust/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "mkbsd"
version = "0.1.0"
edition = "2021"
[dependencies]
reqwest = { version = "0.12.7", features = ["blocking"] }
serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128"

88
rust/src/main.rs Normal file
View file

@ -0,0 +1,88 @@
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)]
struct JsonData {
version: i8,
data: HashMap<String, HashMap<String, String>>,
}
fn get_extension(url: &str) -> Option<String> {
let s = url.split("?").next();
if let Some(s) = s {
if let Some(dot_pos) = s.rfind('.') {
return Some(s[dot_pos..].to_string());
}
}
None
}
fn download_file(url: &str, filename: &str) -> Result<(), Box<dyn std::error::Error>> {
let mut out = std::fs::File::create(format!("./downloads/{}", filename))?;
reqwest::blocking::get(url)?.copy_to(&mut out)?;
Ok(())
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let file_url = "https://storage.googleapis.com/panels-api/data/20240916/media-1a-i-p~s";
let Ok(json_text) = reqwest::blocking::get(file_url)?.text() else {
panic!("Failed to fetch JSON file")
};
let Ok(json_data) = serde_json::from_str::<JsonData>(&json_text) else {
panic!("Failed to deserialize string")
};
if !std::path::Path::new("./downloads").exists() {
std::fs::create_dir("./downloads").expect("Failed to create downloads directory");
}
println!(
"
/$$ /$$ /$$ /$$ /$$$$$$$ /$$$$$$ /$$$$$$$
| $$$ /$$$| $$ /$$/| $$__ $$ /$$__ $$| $$__ $$
| $$$$ /$$$$| $$ /$$/ | $$ \\ $$| $$ \\__/| $$ \\ $$
| $$ $$/$$ $$| $$$$$/ | $$$$$$$ | $$$$$$ | $$ | $$
| $$ $$$| $$| $$ $$ | $$__ $$ \\____ $$| $$ | $$
| $$\\ $ | $$| $$\\ $$ | $$ \\ $$ /$$ \\ $$| $$ | $$
| $$ \\/ | $$| $$ \\ $$| $$$$$$$/| $$$$$$/| $$$$$$$/
|__/ |__/|__/ \\__/|_______/ \\______/ |_______/"
);
println!("\n 🤑 Starting downloads from your favorite sellout grifter's wallpaper app... \n");
for (key_1, inner_map) in json_data.data {
for (key_2, url) in inner_map {
if key_2 != "dhd" {
continue;
}
let filename: String;
match get_extension(&url) {
Some(ext) => {
filename = format!("{}_{}{}", key_1, key_2, ext);
}
None => {
println!("\n No extension found for URL: {} \n", url);
println!("Saving this file as as .jpg \n");
filename = format!("{}_{}.jpg", key_1, key_2);
}
}
match download_file(&url, &filename) {
Ok(_) => {
println!("Downloaded {}", filename);
}
Err(_) => {
println!("Error downloading file: {}", filename);
}
}
}
}
Ok(())
}