better not found handling

This commit is contained in:
Robin Appelman 2025-04-11 01:56:59 +02:00
commit 3c86d7792a

View file

@ -13,12 +13,11 @@ use crate::parser::{
}; };
pub use error::*; pub use error::*;
use reqwest::redirect::Policy; use reqwest::redirect::Policy;
use reqwest::{Client, StatusCode}; use reqwest::{Client, Response, StatusCode};
pub use steamid_ng::SteamID; pub use steamid_ng::SteamID;
pub type Result<T, E = ScrapeError> = std::result::Result<T, E>; pub type Result<T, E = ScrapeError> = std::result::Result<T, E>;
#[derive(Default)]
pub struct UgcClient { pub struct UgcClient {
client: Client, client: Client,
player_parser: PlayerParser, player_parser: PlayerParser,
@ -33,6 +32,12 @@ pub struct UgcClient {
map_history_parser: MapHistoryParser, map_history_parser: MapHistoryParser,
} }
impl Default for UgcClient {
fn default() -> Self {
Self::new()
}
}
/// "API client" for ugc by scraping the website /// "API client" for ugc by scraping the website
impl UgcClient { impl UgcClient {
pub fn new() -> Self { pub fn new() -> Self {
@ -53,35 +58,32 @@ impl UgcClient {
/// Retrieve player information /// Retrieve player information
pub async fn player(&self, steam_id: SteamID) -> Result<Player> { pub async fn player(&self, steam_id: SteamID) -> Result<Player> {
let res = self let body = self
.client .client
.get(format!( .get(format!(
"https://www.ugcleague.com/players_page.cfm?player_id={}", "https://www.ugcleague.com/players_page.cfm?player_id={}",
u64::from(steam_id) u64::from(steam_id)
)) ))
.send() .send()
.await?
.check_not_found()?
.text()
.await?; .await?;
if res.status() == StatusCode::FOUND {
return Err(ScrapeError::NotFound);
}
let body = res.text().await?;
self.player_parser.parse(&body) self.player_parser.parse(&body)
} }
/// Retrieve team membership history for a player /// Retrieve team membership history for a player
pub async fn player_team_history(&self, steam_id: SteamID) -> Result<Vec<MembershipHistory>> { pub async fn player_team_history(&self, steam_id: SteamID) -> Result<Vec<MembershipHistory>> {
let res = self let body = self
.client .client
.get(format!( .get(format!(
"https://www.ugcleague.com/players_page_details.cfm?player_id={}", "https://www.ugcleague.com/players_page_details.cfm?player_id={}",
u64::from(steam_id) u64::from(steam_id)
)) ))
.send() .send()
.await?
.check_not_found()?
.await?; .await?;
if res.status() == StatusCode::FOUND {
return Err(ScrapeError::NotFound);
}
let body = res.text().await?;
self.player_detail_parser.parse(&body) self.player_detail_parser.parse(&body)
} }
@ -161,6 +163,7 @@ impl UgcClient {
)) ))
.send() .send()
.await? .await?
.check_not_found()?
.text() .text()
.await?; .await?;
self.match_page_parser.parse(&body) self.match_page_parser.parse(&body)
@ -184,3 +187,17 @@ impl UgcClient {
self.map_history_parser.parse(&body) self.map_history_parser.parse(&body)
} }
} }
trait ResponseExt: Sized {
fn check_not_found(self) -> Result<Self, ScrapeError>;
}
impl ResponseExt for Response {
fn check_not_found(self) -> Result<Self, ScrapeError> {
if self.status() == StatusCode::FOUND {
Err(ScrapeError::NotFound)
} else {
Ok(self)
}
}
}