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) => {
|
Self::SteamId(err) => {
|
||||||
(StatusCode::UNPROCESSABLE_ENTITY, format!("{:#}", err)).into_response()
|
(StatusCode::UNPROCESSABLE_ENTITY, format!("{:#}", err)).into_response()
|
||||||
}
|
}
|
||||||
|
Self::Scrape(ScrapeError::NotFound) => (StatusCode::NOT_FOUND, "").into_response(),
|
||||||
Self::Scrape(err) => {
|
Self::Scrape(err) => {
|
||||||
(StatusCode::INTERNAL_SERVER_ERROR, format!("{:#}", err)).into_response()
|
(StatusCode::INTERNAL_SERVER_ERROR, format!("{:#}", err)).into_response()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@ pub enum ScrapeError {
|
||||||
Request(#[from] reqwest::Error),
|
Request(#[from] reqwest::Error),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Parse(#[from] ParseError),
|
Parse(#[from] ParseError),
|
||||||
|
#[error("Player or team doesn't exist")]
|
||||||
|
NotFound,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Error, Clone)]
|
#[derive(Debug, Error, Clone)]
|
||||||
|
|
|
||||||
21
src/lib.rs
21
src/lib.rs
|
|
@ -9,7 +9,8 @@ use crate::parser::{
|
||||||
TeamRosterHistoryParser,
|
TeamRosterHistoryParser,
|
||||||
};
|
};
|
||||||
pub use error::*;
|
pub use error::*;
|
||||||
use reqwest::Client;
|
use reqwest::redirect::Policy;
|
||||||
|
use reqwest::{Client, 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>;
|
||||||
|
|
@ -28,7 +29,7 @@ pub struct UgcClient {
|
||||||
impl UgcClient {
|
impl UgcClient {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
UgcClient {
|
UgcClient {
|
||||||
client: Client::default(),
|
client: Client::builder().redirect(Policy::none()).build().unwrap(),
|
||||||
player_parser: PlayerParser::new(),
|
player_parser: PlayerParser::new(),
|
||||||
player_detail_parser: PlayerDetailsParser::new(),
|
player_detail_parser: PlayerDetailsParser::new(),
|
||||||
team_parser: TeamParser::new(),
|
team_parser: TeamParser::new(),
|
||||||
|
|
@ -39,31 +40,35 @@ 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 body = self
|
let res = 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?
|
|
||||||
.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 body = self
|
let res = 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?
|
|
||||||
.text()
|
|
||||||
.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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use super::{ElementExt, Parser};
|
use super::{ElementExt, Parser};
|
||||||
use crate::data::{Membership, NameChange, Record, Team};
|
use crate::data::{Membership, NameChange, Record, Team};
|
||||||
use crate::parser::{select_text, steam_id_from_link, DATE_FORMAT, MEMBER_DATE_FORMAT};
|
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 scraper::{Html, Selector};
|
||||||
use time::{Date, PrimitiveDateTime, UtcOffset};
|
use time::{Date, PrimitiveDateTime, UtcOffset};
|
||||||
|
|
||||||
|
|
@ -111,10 +111,7 @@ impl Parser for TeamParser {
|
||||||
let document = Html::parse_document(document);
|
let document = Html::parse_document(document);
|
||||||
let root = document.root_element();
|
let root = document.root_element();
|
||||||
let name = select_text(root, &self.selector_name)
|
let name = select_text(root, &self.selector_name)
|
||||||
.ok_or(ParseError::ElementNotFound {
|
.ok_or(ScrapeError::NotFound)?
|
||||||
selector: SELECTOR_TEAM_NAME,
|
|
||||||
role: "team name",
|
|
||||||
})?
|
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
let tag = select_text(root, &self.selector_tag)
|
let tag = select_text(root, &self.selector_tag)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue