Initial commit

This commit is contained in:
Edward Tirado Jr 2025-03-16 21:27:18 -05:00
commit 853ab0ff18
9 changed files with 1864 additions and 0 deletions

115
src/main.rs Normal file
View file

@ -0,0 +1,115 @@
use rand::seq::IteratorRandom;
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::Read;
#[derive(Serialize, Deserialize)]
struct ApiResponse<T> {
response: T,
}
#[derive(Serialize, Deserialize)]
struct SteamIdResponse {
steamid: String,
success: u8,
}
#[derive(Serialize, Deserialize, Clone)]
struct Game {
appid: u32,
name: Option<String>,
playtime_2weeks: Option<u32>,
playtime_forever: Option<u32>,
img_icon_url: Option<String>,
img_logo_url: Option<String>,
has_community_visible_stats: Option<bool>,
}
#[derive(Serialize, Deserialize)]
struct GameList {
game_count: i32,
games: Vec<Game>,
}
impl GameList {
fn new() -> GameList {
GameList {
game_count: 0,
games: Vec::new(),
}
}
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))) {
Ok(res) => {
match res.text() {
Ok(text) => {
let api_response: ApiResponse<GameList>= serde_json::from_str(&text).unwrap();
self.games = api_response.response.games;
},
Err(_) => println!("Error getting list") ,
}
}
Err(_) => println!("Error getting list") ,
}
}
fn pick(&self) -> Option<Game> {
self.games.iter().choose(&mut rand::rng()).cloned()
}
}
#[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<SteamIdResponse> =
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();
println!("Time to play {}!", game_list.pick().unwrap().name.unwrap())
}