mirror of
https://codeberg.org/icewind/steam-resolve-vanity.git
synced 2026-06-03 09:04:11 +02:00
add method to get vanity url by steamid
This commit is contained in:
parent
84e8419179
commit
1ca978544a
2 changed files with 69 additions and 1 deletions
20
examples/get.rs
Normal file
20
examples/get.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
use std::env::args;
|
||||
use steam_resolve_vanity::get_vanity_url;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut args = args();
|
||||
let binary = args.next().unwrap(); // first argument is binary
|
||||
|
||||
if let Some(steam_id) = args.next() {
|
||||
if let Some(vanity) = get_vanity_url(steam_id.parse()?).await? {
|
||||
println!("{}", vanity);
|
||||
} else {
|
||||
println!("No vanity found for steamid");
|
||||
}
|
||||
} else {
|
||||
eprintln!("usage {} <vanity>", binary);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
50
src/lib.rs
50
src/lib.rs
|
|
@ -1,4 +1,6 @@
|
|||
use reqwest::{Client, StatusCode};
|
||||
use reqwest::header::LOCATION;
|
||||
use reqwest::redirect::Policy;
|
||||
use reqwest::{Client, ClientBuilder, StatusCode};
|
||||
use serde::Deserialize;
|
||||
pub use steamid_ng::SteamID;
|
||||
use thiserror::Error;
|
||||
|
|
@ -48,6 +50,30 @@ pub async fn resolve_vanity_url(url: &str, api_key: &str) -> Result<Option<Steam
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn get_vanity_url(steam_id: SteamID) -> Result<Option<String>, Error> {
|
||||
let response: reqwest::Response = ClientBuilder::new()
|
||||
.redirect(Policy::none())
|
||||
.build()
|
||||
.unwrap()
|
||||
.get(&format!(
|
||||
"https://steamcommunity.com/profiles/{}",
|
||||
u64::from(steam_id)
|
||||
))
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
Ok(match response.status() {
|
||||
StatusCode::FOUND => response
|
||||
.headers()
|
||||
.into_iter()
|
||||
.find_map(|(name, value)| if name == LOCATION { Some(value) } else { None })
|
||||
.and_then(|value| value.to_str().ok())
|
||||
.map(|value| value.split_at("https://steamcommunity.com/id/".len()).1)
|
||||
.map(|value| value.trim_end_matches("/").to_string()),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[tokio::test]
|
||||
async fn test_valid() {
|
||||
|
|
@ -78,3 +104,25 @@ async fn test_not_found() {
|
|||
.unwrap()
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[tokio::test]
|
||||
async fn test_get_vanity() {
|
||||
assert_eq!(
|
||||
Some("icewind1991".to_string()),
|
||||
get_vanity_url(SteamID::from(76561198024494988))
|
||||
.await
|
||||
.unwrap()
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[tokio::test]
|
||||
async fn test_get_vanity_not_found() {
|
||||
assert_eq!(
|
||||
None,
|
||||
get_vanity_url(SteamID::from(76561198024494987))
|
||||
.await
|
||||
.unwrap()
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue