mirror of
https://codeberg.org/icewind/ugc-scaper.git
synced 2026-06-03 10:14:11 +02:00
not founds
This commit is contained in:
parent
bc449f894c
commit
43bfba6307
4 changed files with 18 additions and 13 deletions
|
|
@ -32,6 +32,7 @@ impl IntoResponse for ApiError {
|
|||
Self::SteamId(err) => {
|
||||
(StatusCode::UNPROCESSABLE_ENTITY, format!("{:#}", err)).into_response()
|
||||
}
|
||||
Self::Scrape(ScrapeError::NotFound) => (StatusCode::NOT_FOUND, "").into_response(),
|
||||
Self::Scrape(err) => {
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, format!("{:#}", err)).into_response()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)]
|
||||
|
|
|
|||
21
src/lib.rs
21
src/lib.rs
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue