def __init__(self, steam_id: STEAM_ID_TYPE, stats: dict = None, persona_name: str = None): super().__init__() if not stats: stats = {} self._stats = stats if isinstance(steam_id, SteamID): self._steam_id = steam_id elif isinstance(steam_id, int): self._steam_id = SteamID(steam_id) elif isinstance(steam_id, str): self._steam_id = SteamID(int(steam_id, 16)) else: raise ValueError( f"invalid steam_id type: {type(steam_id)}, expected " f"{STEAM_ID_TYPE}") if persona_name is None: persona_name = SteamWebAPI().get_persona_name(self.steam_id) self._persona_name = persona_name
def persona_name(self) -> Optional[str]: """Player's Steam persona (profile) name.""" if self._persona_name is None and self.is_steam_player: self._persona_name = SteamWebAPI().get_persona_name(self.steam_id) return self._persona_name
def parse_members(self, resp: bytes, adapter: adapters.WebAdminAdapter ) -> List[adapters.MemberWrapper]: parsed_html = self.parse_html(resp) members_table = parsed_html.find("table", attrs={"id": "members"}) members_headers = [th.text for th in members_table.find_all("th")] tracking_tbody = members_table.find("tbody") tb_row_entries = tracking_tbody.find_all("tr") members_rows = self._parse_table(tb_row_entries) id_index = members_headers.index(UNIQUE_ID_KEY) id_to_extra_data = {} steam_ids = [] egs_ids = [] for row in members_rows: unique_id = row[id_index] try: int_id = int(unique_id, 16) is_steam_id = self._is_steam_id(int_id) except ValueError as ve: logger.exception(ve) logger.error("invalid Unique ID: '{uid}', skipping", uid=unique_id) continue tracking_data = { key: value for key, value in zip( members_headers, row) if key.lower() != "actions" } if is_steam_id: steam_id = SteamID(int_id) steam_ids.append(steam_id) id_to_extra_data[steam_id] = tracking_data else: egs_id = EGSID(int_id) egs_ids.append(egs_id) id_to_extra_data[egs_id] = tracking_data persona_names = SteamWebAPI().get_persona_names( steam_ids=steam_ids ) member_wrappers = [] for steam_id in steam_ids: persona_name = "" try: persona_name = persona_names[steam_id] except KeyError as ke: logger.error( "error getting persona name for Steam ID: {sid}", sid=steam_id) logger.exception(ke) extra_data = id_to_extra_data[steam_id] player = models.Player(ident=steam_id, persona_name=persona_name) member_wrappers.append(adapters.MemberWrapper( player=player, member_data=extra_data, adapter=adapter, )) for egs_id in egs_ids: extra_data = id_to_extra_data[egs_id] player = models.Player(ident=egs_id) member_wrappers.append(adapters.MemberWrapper( player=player, member_data=extra_data, adapter=adapter, )) return member_wrappers
def parse_players( self, resp: bytes, adapter: adapters.WebAdminAdapter) -> List[adapters.PlayerWrapper]: parsed_html = self.parse_html(resp) if not parsed_html: logger.error("unable to parse players; no response" " data to parse") return [] player_table = parsed_html.find("table", attrs={"id": "players"}) player_headers = player_table.find("thead") player_headers = player_headers.find_all("th") player_table = player_table.find("tbody") player_table = player_table.find_all("tr") player_table = self._parse_table(player_table) # Not expecting this header to ever change order. # We could probably hard-code this... player_headers = [ph.text for ph in player_headers] player_headers[player_headers.index(TEAM_INDEX_KEY)] = "Team Index" if (len(player_table) == 1) and player_table[0] == NO_PLAYERS: logger.debug("no players") return [] if not all(len(p) for p in player_table): logger.error("player rows in player table differ in length") return [] if not len(player_table[0]) == len(player_headers): logger.error("player table and player headers differ in length") return [] # We use 'Unique ID' column here instead of 'Steam ID' # column because 'Steam ID' column is sometimes not filled. # 'Unique ID' is just a hex-string of the user's SteamID64 # anyway. id_index = player_headers.index(UNIQUE_ID_KEY) players = [] id_to_stats = {} steam_ids = [] for player_row in player_table: steam_id = player_row[id_index] try: steam_id = int(steam_id, 16) except ValueError as ve: logger.error("unable to convert Unique ID to SteamID64") logger.exception(ve) steam_id = 0 steam_ids.append(steam.SteamID(steam_id)) stats = { key: value for key, value in zip(player_headers, player_row) if key.lower() != "actions" } id_to_stats[steam_id] = stats persona_names = SteamWebAPI().get_persona_names(steam_ids=steam_ids) for steam_id in steam_ids: persona_name = "" try: persona_name = persona_names[steam_id] except KeyError as ke: logger.error("error getting persona name for Steam ID: {sid}", sid=steam_id) logger.exception(ke) p_stats = id_to_stats[steam_id] player = models.Player( steam_id=steam_id, stats=p_stats, persona_name=persona_name, ) players.append( adapters.PlayerWrapper( player=player, adapter=adapter, )) return players
def parse_session_bans(self, resp: bytes, adapter: adapters.WebAdminAdapter ) -> List[adapters.SessionBanWrapper]: parsed_html = self.parse_html(resp) ban_table = parsed_html.find("table", attrs={"class": "grid"}) ban_headers = [th.text for th in ban_table.find_all("th")] ban_tbody = ban_table.find("tbody") ban_row_entries = ban_tbody.find_all("tr") ban_rows = self._parse_table(ban_row_entries) if len(ban_rows) == 1: try: if ban_rows[0][0].lower() == "there are no active session-bans": return [] except IndexError: pass # Unique ID field is more reliable as Steam ID # might be missing in the table in some cases. id_index = ban_headers.index(UNIQUE_ID_KEY) id_to_extra_data = {} steam_ids = [] egs_ids = [] for row in ban_rows: unique_id = row[id_index] try: int_id = int(unique_id, 16) # Session bans sometimes show a dummy ban. if int_id == 0: continue is_steam_id = self._is_steam_id(int_id) except ValueError as ve: logger.exception(ve) logger.error("invalid Unique ID: '{uid}', skipping", uid=unique_id) continue extra_data = { key: value for key, value in zip( ban_headers, row) if key.lower() != "actions" } if is_steam_id: steam_id = SteamID(int_id) steam_ids.append(steam_id) id_to_extra_data[steam_id] = extra_data else: egs_id = EGSID(int_id) egs_ids.append(egs_id) id_to_extra_data[egs_id] = extra_data persona_names = SteamWebAPI().get_persona_names( steam_ids=steam_ids ) session_ban_wrappers = [] for steam_id in steam_ids: persona_name = "" try: persona_name = persona_names[steam_id] except KeyError as ke: logger.debug( "error getting persona name for Steam ID: {sid}", sid=steam_id, exc_info=ke) extra_data = id_to_extra_data[steam_id] player_stats = {"Player Name": extra_data["Player Name"]} player = models.Player(ident=steam_id, persona_name=persona_name, stats=player_stats) ban = models.SessionBan( player=player, reason=extra_data["Reason"], admin=extra_data["Admin"], when=extra_data["When"], ) session_ban_wrappers.append(adapters.SessionBanWrapper( ban=ban, adapter=adapter, )) for egs_id in egs_ids: extra_data = id_to_extra_data[egs_id] player_stats = {"Player Name": extra_data["Player Name"]} player = models.Player(ident=egs_id, stats=player_stats) ban = models.SessionBan( player=player, reason=extra_data["Reason"], admin=extra_data["Admin"], when=extra_data["When"], ) session_ban_wrappers.append(adapters.SessionBanWrapper( ban=ban, adapter=adapter, )) return session_ban_wrappers