not founds

This commit is contained in:
Robin Appelman 2023-11-18 19:37:59 +01:00
commit 43bfba6307
4 changed files with 18 additions and 13 deletions

View file

@ -6,6 +6,8 @@ pub enum ScrapeError {
Request(#[from] reqwest::Error),
#[error(transparent)]
Parse(#[from] ParseError),
#[error("Player or team doesn't exist")]
NotFound,
}
#[derive(Debug, Error, Clone)]

View file

@ -9,7 +9,8 @@ use crate::parser::{
TeamRosterHistoryParser,
};
pub use error::*;
use reqwest::Client;
use reqwest::redirect::Policy;
use reqwest::{Client, StatusCode};
pub use steamid_ng::SteamID;
pub type Result<T, E = ScrapeError> = std::result::Result<T, E>;
@ -28,7 +29,7 @@ pub struct UgcClient {
impl UgcClient {
pub fn new() -> Self {
UgcClient {
client: Client::default(),
client: Client::builder().redirect(Policy::none()).build().unwrap(),
player_parser: PlayerParser::new(),
player_detail_parser: PlayerDetailsParser::new(),
team_parser: TeamParser::new(),
@ -39,31 +40,35 @@ impl UgcClient {
/// Retrieve player information
pub async fn player(&self, steam_id: SteamID) -> Result<Player> {
let body = self
let res = self
.client
.get(&format!(
"https://www.ugcleague.com/players_page.cfm?player_id={}",
u64::from(steam_id)
))
.send()
.await?
.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 body = self
let res = self
.client
.get(&format!(
"https://www.ugcleague.com/players_page_details.cfm?player_id={}",
u64::from(steam_id)
))
.send()
.await?
.text()
.await?;
if res.status() == StatusCode::FOUND {
return Err(ScrapeError::NotFound);
}
let body = res.text().await?;
self.player_detail_parser.parse(&body)
}

View file

@ -1,7 +1,7 @@
use super::{ElementExt, Parser};
use crate::data::{Membership, NameChange, Record, Team};
use crate::parser::{select_text, steam_id_from_link, DATE_FORMAT, MEMBER_DATE_FORMAT};
use crate::{ParseError, Result};
use crate::{ParseError, Result, ScrapeError};
use scraper::{Html, Selector};
use time::{Date, PrimitiveDateTime, UtcOffset};
@ -111,10 +111,7 @@ impl Parser for TeamParser {
let document = Html::parse_document(document);
let root = document.root_element();
let name = select_text(root, &self.selector_name)
.ok_or(ParseError::ElementNotFound {
selector: SELECTOR_TEAM_NAME,
role: "team name",
})?
.ok_or(ScrapeError::NotFound)?
.to_string();
let tag = select_text(root, &self.selector_tag)