config refactor
This commit is contained in:
parent
6f92970f00
commit
c55e7c6233
3 changed files with 75 additions and 51 deletions
19
.gitignore
vendored
19
.gitignore
vendored
|
@ -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
|
||||
|
|
50
src/config.rs
Normal file
50
src/config.rs
Normal file
|
@ -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<SteamIdResponse> =
|
||||
serde_json::from_str(&text).unwrap();
|
||||
api_response.response.steamid
|
||||
}
|
||||
Err(_) => panic!("Error getting steamid."),
|
||||
},
|
||||
Err(_) => panic!("Error getting steamid."),
|
||||
}
|
||||
}
|
||||
}
|
57
src/main.rs
57
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<T> {
|
||||
|
@ -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<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();
|
||||
|
|
Loading…
Add table
Reference in a new issue