player avatar

This commit is contained in:
Robin Appelman 2024-02-23 19:49:02 +01:00
commit fcdbc71000
4 changed files with 19 additions and 0 deletions

View file

@ -43,6 +43,7 @@ mod serde_date {
#[non_exhaustive] #[non_exhaustive]
pub struct Player { pub struct Player {
pub name: String, pub name: String,
pub avatar: String,
pub steam_id: SteamID, pub steam_id: SteamID,
pub honors: Vec<Honors>, pub honors: Vec<Honors>,
pub teams: Vec<TeamMemberShip>, pub teams: Vec<TeamMemberShip>,

View file

@ -24,6 +24,8 @@ const SELECTOR_PLAYER_TEAM_NAME: &str = "span.text-primary b";
const SELECTOR_PLAYER_TEAM_LEAGUE: &str = "small"; const SELECTOR_PLAYER_TEAM_LEAGUE: &str = "small";
const SELECTOR_PLAYER_TEAM_SINCE: &str = "small"; const SELECTOR_PLAYER_TEAM_SINCE: &str = "small";
const SELECTOR_AVATAR: &str =
r#"a[href*="https://www.ugcleague.com/players_page.cfm?player_id="] img.img-responsive"#;
const SELECTOR_CLASS: &str = r#"img.img-rounded[src*="images/tf2/icon/"]"#; const SELECTOR_CLASS: &str = r#"img.img-rounded[src*="images/tf2/icon/"]"#;
pub struct PlayerParser { pub struct PlayerParser {
@ -41,6 +43,7 @@ pub struct PlayerParser {
selector_team_league: Selector, selector_team_league: Selector,
selector_team_since: Selector, selector_team_since: Selector,
selector_avatar: Selector,
selector_class: Selector, selector_class: Selector,
} }
@ -67,6 +70,7 @@ impl PlayerParser {
selector_team_league: Selector::parse(SELECTOR_PLAYER_TEAM_LEAGUE).unwrap(), selector_team_league: Selector::parse(SELECTOR_PLAYER_TEAM_LEAGUE).unwrap(),
selector_team_since: Selector::parse(SELECTOR_PLAYER_TEAM_SINCE).unwrap(), selector_team_since: Selector::parse(SELECTOR_PLAYER_TEAM_SINCE).unwrap(),
selector_avatar: Selector::parse(SELECTOR_AVATAR).unwrap(),
selector_class: Selector::parse(SELECTOR_CLASS).unwrap(), selector_class: Selector::parse(SELECTOR_CLASS).unwrap(),
} }
} }
@ -99,6 +103,17 @@ impl Parser for PlayerParser {
.unwrap_or_default() .unwrap_or_default()
.to_string(); .to_string();
let avatar = document
.select(&self.selector_avatar)
.next()
.ok_or(ParseError::ElementNotFound {
selector: SELECTOR_AVATAR,
role: "player avatar",
})?
.attr("src")
.unwrap_or_default()
.to_string();
let honors = document let honors = document
.select(&self.selector_honors_group) .select(&self.selector_honors_group)
.flat_map(|group| { .flat_map(|group| {
@ -206,6 +221,7 @@ impl Parser for PlayerParser {
Ok(Player { Ok(Player {
name, name,
avatar,
steam_id: SteamID::from_steam3(&id).unwrap_or_default(), steam_id: SteamID::from_steam3(&id).unwrap_or_default(),
honors, honors,
teams, teams,

View file

@ -4,6 +4,7 @@ expression: parsed
--- ---
{ {
"name": "Dirty Sneeds Don", "name": "Dirty Sneeds Don",
"avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/00/00410e6516bc5d5dcc10004c4fa972950e6a5e60_full.jpg",
"steam_id": 0, "steam_id": 0,
"honors": [ "honors": [
{ {

View file

@ -4,6 +4,7 @@ expression: parsed
--- ---
{ {
"name": "Icewind demostf", "name": "Icewind demostf",
"avatar": "https://avatars.akamai.steamstatic.com/75b84075b70535c5cfb3499af03b3e4e7a7b556f_full.jpg",
"steam_id": 0, "steam_id": 0,
"honors": [ "honors": [
{ {