mirror of
https://codeberg.org/icewind/ugc-scaper.git
synced 2026-06-03 18:24:10 +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::*;
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue