mirror of
https://codeberg.org/icewind/ugc-scaper.git
synced 2026-06-03 10:14:11 +02:00
better not found handling
This commit is contained in:
parent
ae681cb75c
commit
3c86d7792a
1 changed files with 29 additions and 12 deletions
41
src/lib.rs
41
src/lib.rs
|
|
@ -13,12 +13,11 @@ use crate::parser::{
|
|||
};
|
||||
pub use error::*;
|
||||
use reqwest::redirect::Policy;
|
||||
use reqwest::{Client, StatusCode};
|
||||
use reqwest::{Client, Response, StatusCode};
|
||||
pub use steamid_ng::SteamID;
|
||||
|
||||
pub type Result<T, E = ScrapeError> = std::result::Result<T, E>;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct UgcClient {
|
||||
client: Client,
|
||||
player_parser: PlayerParser,
|
||||
|
|
@ -33,6 +32,12 @@ pub struct UgcClient {
|
|||
map_history_parser: MapHistoryParser,
|
||||
}
|
||||
|
||||
impl Default for UgcClient {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
/// "API client" for ugc by scraping the website
|
||||
impl UgcClient {
|
||||
pub fn new() -> Self {
|
||||
|
|
@ -53,35 +58,32 @@ impl UgcClient {
|
|||
|
||||
/// Retrieve player information
|
||||
pub async fn player(&self, steam_id: SteamID) -> Result<Player> {
|
||||
let res = self
|
||||
let body = self
|
||||
.client
|
||||
.get(format!(
|
||||
"https://www.ugcleague.com/players_page.cfm?player_id={}",
|
||||
u64::from(steam_id)
|
||||
))
|
||||
.send()
|
||||
.await?
|
||||
.check_not_found()?
|
||||
.text()
|
||||
.await?;
|
||||
if res.status() == StatusCode::FOUND {
|
||||
return Err(ScrapeError::NotFound);
|
||||
}
|
||||
let body = res.text().await?;
|
||||
self.player_parser.parse(&body)
|
||||
}
|
||||
|
||||
/// Retrieve team membership history for a player
|
||||
pub async fn player_team_history(&self, steam_id: SteamID) -> Result<Vec<MembershipHistory>> {
|
||||
let res = self
|
||||
let body = self
|
||||
.client
|
||||
.get(format!(
|
||||
"https://www.ugcleague.com/players_page_details.cfm?player_id={}",
|
||||
u64::from(steam_id)
|
||||
))
|
||||
.send()
|
||||
.await?
|
||||
.check_not_found()?
|
||||
.await?;
|
||||
if res.status() == StatusCode::FOUND {
|
||||
return Err(ScrapeError::NotFound);
|
||||
}
|
||||
let body = res.text().await?;
|
||||
self.player_detail_parser.parse(&body)
|
||||
}
|
||||
|
||||
|
|
@ -161,6 +163,7 @@ impl UgcClient {
|
|||
))
|
||||
.send()
|
||||
.await?
|
||||
.check_not_found()?
|
||||
.text()
|
||||
.await?;
|
||||
self.match_page_parser.parse(&body)
|
||||
|
|
@ -184,3 +187,17 @@ impl UgcClient {
|
|||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue