extract region

This commit is contained in:
Robin Appelman 2025-04-14 15:44:13 +02:00
commit 8fd98b708f
5 changed files with 51 additions and 1 deletions

View file

@ -5,8 +5,9 @@ use crate::parser::{
}; };
use crate::{ParseError, Result, ScrapeError}; use crate::{ParseError, Result, ScrapeError};
use scraper::{Html, Selector}; use scraper::{Html, Selector};
use std::str::FromStr;
use time::{Date, PrimitiveDateTime, Time, UtcOffset}; use time::{Date, PrimitiveDateTime, Time, UtcOffset};
use ugc_scraper_types::GameMode; use ugc_scraper_types::{GameMode, Region};
const SELECTOR_TEAM_NAME: &str = ".container .col-md-12 h1 > b"; const SELECTOR_TEAM_NAME: &str = ".container .col-md-12 h1 > b";
const SELECTOR_TEAM_TAG: &str = ".container .col-md-12 h1 > span"; const SELECTOR_TEAM_TAG: &str = ".container .col-md-12 h1 > span";
@ -171,6 +172,14 @@ impl Parser for TeamParser {
})? })?
.to_string(); .to_string();
let region = division
.split(' ')
.find_map(|part| Region::from_str(part).ok())
.ok_or_else(|| ParseError::InvalidText {
text: division.clone(),
role: "team region",
})?;
let timezone = select_text(root, &self.selector_team_timezone).map(String::from); let timezone = select_text(root, &self.selector_team_timezone).map(String::from);
let description = select_text(root, &self.selector_team_description) let description = select_text(root, &self.selector_team_description)
@ -354,6 +363,7 @@ impl Parser for TeamParser {
results, results,
members, members,
name_changes, name_changes,
region,
}) })
} }
} }

View file

@ -7,6 +7,7 @@ expression: parsed
"tag": "Melting Pot", "tag": "Melting Pot",
"image": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f7/f75809d7774c917be9883370d772d3099bfe457d_full.jpg", "image": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f7/f75809d7774c917be9883370d772d3099bfe457d_full.jpg",
"format": "9v9", "format": "9v9",
"region": "Euro",
"timezone": "West-Euro", "timezone": "West-Euro",
"steam_group": "http://steamcommunity.com/groups/Melintongpotsss", "steam_group": "http://steamcommunity.com/groups/Melintongpotsss",
"division": "Euro Platinum", "division": "Euro Platinum",

View file

@ -7,6 +7,7 @@ expression: parsed
"tag": "-Xe-", "tag": "-Xe-",
"image": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/db/dbabbd8bab7ccf6d27a9d4ca2e73a76e085bb201_full.jpg", "image": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/db/dbabbd8bab7ccf6d27a9d4ca2e73a76e085bb201_full.jpg",
"format": "9v9", "format": "9v9",
"region": "Euro",
"timezone": "West-Euro", "timezone": "West-Euro",
"steam_group": "https://steamcommunity.com/groups/XenonxTF2", "steam_group": "https://steamcommunity.com/groups/XenonxTF2",
"division": "Euro Platinum", "division": "Euro Platinum",

View file

@ -7,6 +7,7 @@ expression: parsed
"tag": "Grand Meister", "tag": "Grand Meister",
"image": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/d7/d71ff4e1e2635c3d7c1be8d96bc40b499c665aad_full.jpg", "image": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/d7/d71ff4e1e2635c3d7c1be8d96bc40b499c665aad_full.jpg",
"format": "9v9", "format": "9v9",
"region": "NorthAmerica",
"timezone": null, "timezone": null,
"steam_group": "http://steamcommunity.com/groups/BARNEYANDTHEGANKS", "steam_group": "http://steamcommunity.com/groups/BARNEYANDTHEGANKS",
"division": "NA Platinum", "division": "NA Platinum",

View file

@ -115,6 +115,7 @@ pub struct Team {
pub tag: String, pub tag: String,
pub image: String, pub image: String,
pub format: GameMode, pub format: GameMode,
pub region: Region,
pub timezone: Option<String>, pub timezone: Option<String>,
pub steam_group: Option<String>, pub steam_group: Option<String>,
pub division: String, pub division: String,
@ -350,6 +351,42 @@ impl Display for GameMode {
} }
} }
#[derive(Debug, Clone, Error)]
#[error("Invalid team region: {text}")]
pub struct InvalidRegion {
pub text: String,
}
#[derive(Serialize, Deserialize, Copy, Clone, Debug)]
pub enum Region {
Euro,
NorthAmerica,
SouthAmerica,
Asia,
Australia,
}
impl FromStr for Region {
type Err = InvalidRegion;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"Euro" => Ok(Region::Euro),
"EU" => Ok(Region::Euro),
"Asia" => Ok(Region::Asia),
"ASIA" => Ok(Region::Asia),
"NA" => Ok(Region::NorthAmerica),
"South American" => Ok(Region::SouthAmerica),
"SA" => Ok(Region::SouthAmerica),
"AUS" => Ok(Region::Australia),
"AUS/NZ" => Ok(Region::Australia),
_ => Err(InvalidRegion {
text: s.to_string(),
}),
}
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Transaction { pub struct Transaction {