diff --git a/.gitignore b/.gitignore index 4a3b37d..b106311 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,19 @@ -/target +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb + +# RustRover +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + config.toml diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..c95a9d1 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,50 @@ +use crate::{ApiResponse, SteamIdResponse}; +use serde::Deserialize; +use serde::Serialize; +use std::fs::File; +use std::io::Read; + +#[derive(Serialize, Deserialize)] +pub struct User { + pub username: String, +} + +#[derive(Serialize, Deserialize)] +pub struct Config { + pub steam_api_key: String, + pub user: User, +} + +impl Config { + pub fn new() -> Config { + let mut config_file = File::open("config.toml").expect("Failed to open config.toml"); + let mut contents = String::new(); + config_file + .read_to_string(&mut contents) + .expect("Failed to read config.toml"); + let config: Config = toml::from_str(&contents).expect("Failed to parse config.toml"); + + Config { + steam_api_key: config.steam_api_key, + user: config.user, + } + } + + pub fn get_steam_id(&self, username: Option<&String>) -> String { + let username = username.unwrap_or(&self.user.username); + match reqwest::blocking::get(format!( + "https://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key={}&vanityurl={}", + self.steam_api_key, username + )) { + Ok(res) => match res.text() { + Ok(text) => { + let api_response: ApiResponse = + serde_json::from_str(&text).unwrap(); + api_response.response.steamid + } + Err(_) => panic!("Error getting steamid."), + }, + Err(_) => panic!("Error getting steamid."), + } + } +} diff --git a/src/main.rs b/src/main.rs index 47f0590..be6fe36 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,8 @@ +mod config; + +use crate::config::Config; use rand::seq::IteratorRandom; use serde::{Deserialize, Serialize}; -use std::fs::File; -use std::io::Read; #[derive(Serialize, Deserialize)] struct ApiResponse { @@ -41,7 +42,10 @@ impl GameList { fn update(&mut self) { let config = Config::new(); - match reqwest::blocking::get(format!("https://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key={}&steamid={}&format=json&include_appinfo=true", config.steam_api_key, get_steam_id(&config.user.username))) { + match reqwest::blocking::get(format!("https://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key={}&steamid={}&format=json&include_appinfo=true", + config.steam_api_key, + config.get_steam_id(Some(&config.user.username)) + )) { Ok(res) => { match res.text() { Ok(text) => { @@ -60,53 +64,6 @@ impl GameList { } } -#[derive(Serialize, Deserialize)] -struct User { - username: String, -} - -#[derive(Serialize, Deserialize)] -struct Config { - steam_api_key: String, - user: User, -} - -impl Config { - fn new() -> Config { - let mut config_file = File::open("config.toml").expect("Failed to open config.toml"); - - let mut contents = String::new(); - config_file - .read_to_string(&mut contents) - .expect("Failed to read config.toml"); - let config: Config = toml::from_str(&contents).expect("Failed to parse config.toml"); - - Config { - steam_api_key: config.steam_api_key, - user: config.user, - } - } -} - -fn get_steam_id(username: &String) -> String { - let config = Config::new(); - - match reqwest::blocking::get(format!( - "https://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key={}&vanityurl={}", - config.steam_api_key, username - )) { - Ok(res) => match res.text() { - Ok(text) => { - let api_response: ApiResponse = - serde_json::from_str(&text).unwrap(); - api_response.response.steamid - } - Err(_) => panic!("Error getting steamid."), - }, - Err(_) => panic!("Error getting steamid."), - } -} - fn main() { let mut game_list = GameList::new(); game_list.update();