Initial commit
This commit is contained in:
commit
853ab0ff18
9 changed files with 1864 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
/target
|
||||
config.toml
|
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/steam_game_picker.iml" filepath="$PROJECT_DIR$/.idea/steam_game_picker.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
11
.idea/steam_game_picker.iml
generated
Normal file
11
.idea/steam_game_picker.iml
generated
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="EMPTY_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
1699
Cargo.lock
generated
Normal file
1699
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
11
Cargo.toml
Normal file
11
Cargo.toml
Normal file
|
@ -0,0 +1,11 @@
|
|||
[package]
|
||||
name = "steam_game_picker"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
reqwest = { version = "0.12", features = ["json", "blocking"] }
|
||||
rand = "0.9.0"
|
||||
toml = "0.8.20"
|
4
config.toml.example
Normal file
4
config.toml.example
Normal file
|
@ -0,0 +1,4 @@
|
|||
steam_api_key = "your_steam_api_key"
|
||||
|
||||
[user]
|
||||
username = "your_steam_username"
|
115
src/main.rs
Normal file
115
src/main.rs
Normal 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())
|
||||
}
|
Loading…
Add table
Reference in a new issue