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"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
"clap_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -315,18 +314,6 @@ dependencies = [
|
|||
"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]]
|
||||
name = "clap_lex"
|
||||
version = "0.7.0"
|
||||
|
@ -1715,9 +1702,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.115"
|
||||
version = "1.0.116"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "12dc5c46daa8e9fdf4f5e71b6cf9a53f2487da0e86e55808e2d35539666497dd"
|
||||
checksum = "3e17db7126d17feb94eb3fad46bf1a96b034e8aacbc2e775fe81505f8b0b2813"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"ryu",
|
||||
|
|
|
@ -4,7 +4,7 @@ version = "0.1.0"
|
|||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "4.5.4", features = ["cargo", "derive"] }
|
||||
clap = { version = "4.5.4", features = ["cargo"] }
|
||||
crossterm = "0.27.0"
|
||||
ctrlc = "3.4.4"
|
||||
dirs = "5.0.1"
|
||||
|
@ -13,6 +13,6 @@ once_cell = "1.19.0"
|
|||
rand = { version = "0.8.4", features = ["small_rng"] }
|
||||
reqwest = { version = "0.11", features = ["blocking", "json"] }
|
||||
rust-embed = "8.3.0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
serde = { version = "1.0.197", features = ["derive"] }
|
||||
serde_json = "1.0.116"
|
||||
zip = "0.6.6"
|
||||
|
|
|
@ -1,28 +1,32 @@
|
|||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
let contents = fs::read_to_string("testing/testing.fuck").unwrap();
|
||||
let mut found: bool = false;
|
||||
|
||||
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")
|
||||
}
|
||||
let _bruh = "bruh";
|
||||
}
|
||||
|
||||
// use std::fs;
|
||||
|
||||
// fn main() {
|
||||
// let contents = fs::read_to_string("testing/testing.fuck").unwrap();
|
||||
// let mut found: bool = false;
|
||||
|
||||
// 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 std::io::{self, stdout, Write};
|
||||
// use std::num::ParseIntError;
|
||||
|
|
|
@ -14,23 +14,3 @@ pub static CACHE_DIRECTORY: once_cell::sync::Lazy<std::path::PathBuf> =
|
|||
.map(|dir| dir.join("rustmon"))
|
||||
.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
|
||||
match fetch_colorscripts_archive(crate::constants::TARGET_URL) {
|
||||
Ok(_) => (),
|
||||
|
@ -52,10 +72,11 @@ pub fn fetch(extract_destination: &std::path::Path, verbose: bool) {
|
|||
};
|
||||
|
||||
// cleanup
|
||||
match cleanup() {
|
||||
Ok(_) => (),
|
||||
Err(e) => eprintln!("Error cleaning up: {}", e),
|
||||
};
|
||||
// TODO: uncomment
|
||||
// match cleanup() {
|
||||
// Ok(_) => (),
|
||||
// Err(e) => eprintln!("Error cleaning up: {}", e),
|
||||
// };
|
||||
}
|
||||
|
||||
fn create_working_directory() -> std::io::Result<()> {
|
||||
|
@ -69,6 +90,138 @@ fn create_working_directory() -> std::io::Result<()> {
|
|||
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>> {
|
||||
println!("Fetching colorscripts archive...");
|
||||
|
||||
|
|
|
@ -5,11 +5,12 @@
|
|||
- `verbose` - Print colorscripts when generating
|
||||
|
||||
## `print` - Print a Pokemon colorscript
|
||||
- `name` - Select Pokemon by name
|
||||
- `big` - Print a bigger version of the colorscript
|
||||
- `id` - Print Pokemon by ID
|
||||
- `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
|
||||
- `random` - Print a random Pokemon colorscript
|
||||
- `shiny` - Print the shiny version of the colorscript
|
||||
*/
|
||||
|
||||
|
|
Loading…
Reference in a new issue