added pokemon.json fetching
This commit is contained in:
parent
849cb35eca
commit
36035f5204
17
Cargo.lock
generated
17
Cargo.lock
generated
|
@ -300,7 +300,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0"
|
checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap_builder",
|
"clap_builder",
|
||||||
"clap_derive",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -315,18 +314,6 @@ dependencies = [
|
||||||
"strsim",
|
"strsim",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "clap_derive"
|
|
||||||
version = "4.5.4"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64"
|
|
||||||
dependencies = [
|
|
||||||
"heck",
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap_lex"
|
name = "clap_lex"
|
||||||
version = "0.7.0"
|
version = "0.7.0"
|
||||||
|
@ -1715,9 +1702,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_json"
|
name = "serde_json"
|
||||||
version = "1.0.115"
|
version = "1.0.116"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "12dc5c46daa8e9fdf4f5e71b6cf9a53f2487da0e86e55808e2d35539666497dd"
|
checksum = "3e17db7126d17feb94eb3fad46bf1a96b034e8aacbc2e775fe81505f8b0b2813"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"itoa",
|
"itoa",
|
||||||
"ryu",
|
"ryu",
|
||||||
|
|
|
@ -4,7 +4,7 @@ version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.5.4", features = ["cargo", "derive"] }
|
clap = { version = "4.5.4", features = ["cargo"] }
|
||||||
crossterm = "0.27.0"
|
crossterm = "0.27.0"
|
||||||
ctrlc = "3.4.4"
|
ctrlc = "3.4.4"
|
||||||
dirs = "5.0.1"
|
dirs = "5.0.1"
|
||||||
|
@ -13,6 +13,6 @@ once_cell = "1.19.0"
|
||||||
rand = { version = "0.8.4", features = ["small_rng"] }
|
rand = { version = "0.8.4", features = ["small_rng"] }
|
||||||
reqwest = { version = "0.11", features = ["blocking", "json"] }
|
reqwest = { version = "0.11", features = ["blocking", "json"] }
|
||||||
rust-embed = "8.3.0"
|
rust-embed = "8.3.0"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0.197", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0.116"
|
||||||
zip = "0.6.6"
|
zip = "0.6.6"
|
||||||
|
|
|
@ -1,27 +1,31 @@
|
||||||
use std::fs;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let contents = fs::read_to_string("testing/testing.fuck").unwrap();
|
let _bruh = "bruh";
|
||||||
let mut found: bool = false;
|
|
||||||
|
|
||||||
for line in contents.lines() {
|
|
||||||
match line {
|
|
||||||
"1" | "2" => {
|
|
||||||
found = true;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// if not found
|
// use std::fs;
|
||||||
_ => found = false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if found == true {
|
// fn main() {
|
||||||
println!("Gotcha bitch");
|
// let contents = fs::read_to_string("testing/testing.fuck").unwrap();
|
||||||
} else {
|
// let mut found: bool = false;
|
||||||
println!("Never mind")
|
|
||||||
}
|
// for line in contents.lines() {
|
||||||
}
|
// match line {
|
||||||
|
// "1" | "2" => {
|
||||||
|
// found = true;
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // if not found
|
||||||
|
// _ => found = false,
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if found == true {
|
||||||
|
// println!("Gotcha bitch");
|
||||||
|
// } else {
|
||||||
|
// println!("Never mind")
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
// use rand::{thread_rng, Rng};
|
// use rand::{thread_rng, Rng};
|
||||||
// use std::io::{self, stdout, Write};
|
// use std::io::{self, stdout, Write};
|
||||||
|
|
|
@ -14,23 +14,3 @@ pub static CACHE_DIRECTORY: once_cell::sync::Lazy<std::path::PathBuf> =
|
||||||
.map(|dir| dir.join("rustmon"))
|
.map(|dir| dir.join("rustmon"))
|
||||||
.expect("Cache directory not found")
|
.expect("Cache directory not found")
|
||||||
});
|
});
|
||||||
|
|
||||||
// fn get_directories() -> (std::path::PathBuf, std::path::PathBuf) {
|
|
||||||
// let data_directory: std::path::PathBuf = match dirs::data_dir() {
|
|
||||||
// Some(dir) => dir.join("rustmon"),
|
|
||||||
// None => {
|
|
||||||
// println!("Data directory not found");
|
|
||||||
// std::process::exit(1);
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
// let cache_directory: std::path::PathBuf = match dirs::cache_dir() {
|
|
||||||
// Some(dir) => dir.join("rustmon"),
|
|
||||||
// None => {
|
|
||||||
// println!("Cache directory not found");
|
|
||||||
// std::process::exit(1);
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
// return (data_directory, cache_directory);
|
|
||||||
// }
|
|
||||||
|
|
161
src/fetch.rs
161
src/fetch.rs
|
@ -10,6 +10,26 @@ pub fn fetch(extract_destination: &std::path::Path, verbose: bool) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// download pokemon.json
|
||||||
|
match fetch_pokemon_json() {
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Error fetching pokemon_raw.json: {}", e);
|
||||||
|
cleanup().unwrap();
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// process pokemon_raw.json
|
||||||
|
match process_pokemon_json() {
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Error processing pokemon_raw.json: {}", e);
|
||||||
|
cleanup().unwrap();
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// download colorscripts archive
|
// download colorscripts archive
|
||||||
match fetch_colorscripts_archive(crate::constants::TARGET_URL) {
|
match fetch_colorscripts_archive(crate::constants::TARGET_URL) {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
|
@ -52,10 +72,11 @@ pub fn fetch(extract_destination: &std::path::Path, verbose: bool) {
|
||||||
};
|
};
|
||||||
|
|
||||||
// cleanup
|
// cleanup
|
||||||
match cleanup() {
|
// TODO: uncomment
|
||||||
Ok(_) => (),
|
// match cleanup() {
|
||||||
Err(e) => eprintln!("Error cleaning up: {}", e),
|
// Ok(_) => (),
|
||||||
};
|
// Err(e) => eprintln!("Error cleaning up: {}", e),
|
||||||
|
// };
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_working_directory() -> std::io::Result<()> {
|
fn create_working_directory() -> std::io::Result<()> {
|
||||||
|
@ -69,6 +90,138 @@ fn create_working_directory() -> std::io::Result<()> {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn fetch_pokemon_json() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
println!("Fetching pokemon_raw.json...");
|
||||||
|
|
||||||
|
let response = reqwest::blocking::get(
|
||||||
|
"https://raw.githubusercontent.com/Vomitblood/pokesprite/master/data/pokemon.json",
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let mut dest = std::fs::File::create(
|
||||||
|
&*crate::constants::CACHE_DIRECTORY
|
||||||
|
.to_path_buf()
|
||||||
|
.join("pokemon_raw.json"),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let response_body = response.error_for_status()?.bytes()?;
|
||||||
|
std::io::copy(&mut response_body.as_ref(), &mut dest)?;
|
||||||
|
|
||||||
|
println!("Downloaded pokemon_raw.json");
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Serialize, serde::Deserialize, Debug)]
|
||||||
|
struct Slug {
|
||||||
|
eng: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Serialize, serde::Deserialize, Debug)]
|
||||||
|
struct Forms {
|
||||||
|
// ignoring actual details in the forms and just capturing form names
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Serialize, serde::Deserialize, Debug)]
|
||||||
|
struct Generation {
|
||||||
|
forms: std::collections::HashMap<String, Forms>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Serialize, serde::Deserialize, Debug)]
|
||||||
|
struct Pokemon {
|
||||||
|
idx: String,
|
||||||
|
slug: Slug,
|
||||||
|
#[serde(rename = "gen-8")]
|
||||||
|
gen_8: Generation,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Serialize, serde::Deserialize, Debug)]
|
||||||
|
struct PokemonCollection {
|
||||||
|
#[serde(flatten)]
|
||||||
|
entries: std::collections::HashMap<String, Pokemon>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Serialize, Debug)]
|
||||||
|
struct ProcessedPokemon {
|
||||||
|
pokedex: String,
|
||||||
|
name: String,
|
||||||
|
forms: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process_pokemon_json() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
println!("Generating pokemon.json...");
|
||||||
|
|
||||||
|
let pokemon_raw_json_path = &*crate::constants::CACHE_DIRECTORY.join("pokemon_raw.json");
|
||||||
|
|
||||||
|
let pokemon_collection = read_pokemon_file(pokemon_raw_json_path)?;
|
||||||
|
|
||||||
|
let processed_pokemon = transform_pokemon_data(&pokemon_collection.entries);
|
||||||
|
|
||||||
|
// serialize the processed data to JSON
|
||||||
|
let serialized_pokemon = serde_json::to_string_pretty(&processed_pokemon)?;
|
||||||
|
|
||||||
|
// write processed data to file
|
||||||
|
std::fs::write(
|
||||||
|
crate::constants::CACHE_DIRECTORY.join("processed_pokemon.json"),
|
||||||
|
serialized_pokemon,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
println!("Generated pokemon.json");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_pokemon_file(
|
||||||
|
file_path: &std::path::Path,
|
||||||
|
) -> Result<PokemonCollection, Box<dyn std::error::Error>> {
|
||||||
|
// open the file in read only mode
|
||||||
|
let file = std::fs::File::open(file_path)?;
|
||||||
|
let reader = std::io::BufReader::new(file);
|
||||||
|
|
||||||
|
// deserialize the into pokemoncollection
|
||||||
|
let collection = serde_json::from_reader(reader)?;
|
||||||
|
|
||||||
|
return Ok(collection);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn transform_pokemon_data(
|
||||||
|
pokemons: &std::collections::HashMap<String, Pokemon>,
|
||||||
|
) -> Vec<ProcessedPokemon> {
|
||||||
|
let mut processed_pokemons: Vec<ProcessedPokemon> = pokemons
|
||||||
|
.iter()
|
||||||
|
.map(|(_key, p)| {
|
||||||
|
let forms = p
|
||||||
|
.gen_8
|
||||||
|
.forms
|
||||||
|
.keys()
|
||||||
|
.map(|key| match key.as_str() {
|
||||||
|
"$" => "regular".to_string(),
|
||||||
|
_ => key.clone(),
|
||||||
|
})
|
||||||
|
.collect::<Vec<String>>();
|
||||||
|
|
||||||
|
ProcessedPokemon {
|
||||||
|
// remove leading zeros from the pokedex number
|
||||||
|
pokedex: p.idx.trim_start_matches('0').to_string(),
|
||||||
|
// use the slug as the name
|
||||||
|
// this is because i am too lazy to decapitalize the name
|
||||||
|
// also in case of name =/= slug
|
||||||
|
name: p.slug.eng.clone(),
|
||||||
|
forms,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
// sort the vector by pokedex number
|
||||||
|
processed_pokemons.sort_by(|a, b| {
|
||||||
|
a.pokedex
|
||||||
|
.parse::<u32>()
|
||||||
|
.unwrap_or(0)
|
||||||
|
.cmp(&b.pokedex.parse::<u32>().unwrap_or(0))
|
||||||
|
});
|
||||||
|
|
||||||
|
return processed_pokemons;
|
||||||
|
}
|
||||||
|
|
||||||
fn fetch_colorscripts_archive(target_url: &str) -> Result<(), Box<dyn std::error::Error>> {
|
fn fetch_colorscripts_archive(target_url: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
println!("Fetching colorscripts archive...");
|
println!("Fetching colorscripts archive...");
|
||||||
|
|
||||||
|
|
|
@ -5,11 +5,12 @@
|
||||||
- `verbose` - Print colorscripts when generating
|
- `verbose` - Print colorscripts when generating
|
||||||
|
|
||||||
## `print` - Print a Pokemon colorscript
|
## `print` - Print a Pokemon colorscript
|
||||||
- `name` - Select Pokemon by name
|
|
||||||
- `big` - Print a bigger version of the colorscript
|
- `big` - Print a bigger version of the colorscript
|
||||||
|
- `id` - Print Pokemon by ID
|
||||||
- `list` - Print a list of all Pokemon names
|
- `list` - Print a list of all Pokemon names
|
||||||
- `random` - Print a random Pokemon colorscript
|
- `name` - Print Pokemon by name
|
||||||
- `no-title` - Do not print Pokemon name
|
- `no-title` - Do not print Pokemon name
|
||||||
|
- `random` - Print a random Pokemon colorscript
|
||||||
- `shiny` - Print the shiny version of the colorscript
|
- `shiny` - Print the shiny version of the colorscript
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue