steam group on roster page

This commit is contained in:
Robin Appelman 2024-02-23 22:32:01 +01:00
commit df91032de4
12 changed files with 993 additions and 960 deletions

View file

@ -168,6 +168,14 @@ pub struct Record {
pub losses: u8,
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct TeamRosterData {
pub steam_group: String,
pub history: Vec<RosterHistory>,
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct RosterHistory {

View file

@ -4,8 +4,8 @@ mod error;
pub mod parser;
use crate::data::{
GameMode, MapHistory, MatchInfo, MembershipHistory, Player, RosterHistory, Seasons, Team,
TeamRef, TeamSeason, Transaction,
GameMode, MapHistory, MatchInfo, MembershipHistory, Player, Seasons, Team, TeamRef,
TeamRosterData, TeamSeason, Transaction,
};
use crate::parser::{
MapHistoryParser, MatchPageParser, Parser, PlayerDetailsParser, PlayerParser, SeasonsParser,
@ -101,7 +101,7 @@ impl UgcClient {
}
/// Retrieve team roster history
pub async fn team_roster_history(&self, id: u32) -> Result<Vec<RosterHistory>> {
pub async fn team_roster_history(&self, id: u32) -> Result<TeamRosterData> {
let body = self
.client
.get(&format!(

View file

@ -154,7 +154,10 @@ impl Parser for TeamParser {
let steam_group = root
.select(&self.selector_steam_group)
.next()
.and_then(|link| link.attr("href").map(String::from));
.and_then(|link| {
link.attr("href")
.map(|group| group.replace("http://http", "http"))
});
let division = select_text(root, &self.selector_team_division)
.ok_or(ParseError::ElementNotFound {

View file

@ -1,5 +1,5 @@
use super::Parser;
use crate::data::RosterHistory;
use crate::data::{RosterHistory, TeamRosterData};
use crate::parser::{select_text, ROSTER_HISTORY_DATE_FORMAT};
use crate::{ParseError, Result};
use scraper::{Html, Selector};
@ -13,12 +13,15 @@ const SELECTOR_ROSTER_ID: &str = "h5 small";
const SELECTOR_ROSTER_JOINED: &str = "span.text-success small";
const SELECTOR_ROSTER_LEFT: &str = "span.text-danger small";
const SELECTOR_STEAM: &str = r#"p.muted a[href*="//steamcommunity.com/groups"]"#;
pub struct TeamRosterHistoryParser {
selector_item: Selector,
selector_name: Selector,
selector_id: Selector,
selector_joined: Selector,
selector_left: Selector,
selector_steam_group: Selector,
}
impl Default for TeamRosterHistoryParser {
@ -35,17 +38,29 @@ impl TeamRosterHistoryParser {
selector_id: Selector::parse(SELECTOR_ROSTER_ID).unwrap(),
selector_joined: Selector::parse(SELECTOR_ROSTER_JOINED).unwrap(),
selector_left: Selector::parse(SELECTOR_ROSTER_LEFT).unwrap(),
selector_steam_group: Selector::parse(SELECTOR_STEAM).unwrap(),
}
}
}
impl Parser for TeamRosterHistoryParser {
type Output = Vec<RosterHistory>;
type Output = TeamRosterData;
fn parse(&self, document: &str) -> Result<Self::Output> {
let document = Html::parse_document(document);
document
let steam_group = document.select(&self.selector_steam_group).next().ok_or(
ParseError::ElementNotFound {
selector: SELECTOR_STEAM,
role: "team steam group",
},
)?;
let steam_group = steam_group
.attr("href")
.unwrap_or_default()
.replace("http://http", "http");
let history = document
.select(&self.selector_item)
.map(|item| {
let name =
@ -92,6 +107,10 @@ impl Parser for TeamRosterHistoryParser {
.transpose()?,
})
})
.collect::<Result<Vec<_>>>()
.collect::<Result<Vec<_>>>()?;
Ok(TeamRosterData {
history,
steam_group,
})
}
}