This repository has been archived on 2024-07-30. You can view files and clone it, but cannot push or open issues or pull requests.
python-aternos/python_aternos/atplayers.py

102 lines
2.3 KiB
Python
Raw Normal View History

2022-03-18 18:38:36 +04:00
import enum
import lxml.html
2022-03-25 16:45:38 +04:00
from typing import List, Union
2021-10-15 19:31:47 +04:00
from typing import TYPE_CHECKING
2021-10-15 19:31:47 +04:00
if TYPE_CHECKING:
2022-03-18 18:38:36 +04:00
from .atserver import AternosServer
class Lists(enum.Enum):
2021-10-15 19:31:47 +04:00
2022-03-18 18:38:36 +04:00
whl = 'whitelist'
ops = 'ops'
ban = 'banned-players'
ips = 'banned-ips'
class PlayersList:
"""Class for managing operators, whitelist and banned players lists
:param lst: Players list type, must be
:class:`python_aternos.atplayers.Lists` enum value
:type lst: Union[str,Lists]
:param atserv: :class:`python_aternos.atserver.AternosServer` instance
:type atserv: python_aternos.atserver.AternosServer
"""
2022-03-25 16:45:38 +04:00
def __init__(self, lst:Union[str,Lists], atserv:'AternosServer') -> None:
self.atserv = atserv
2022-03-25 16:45:38 +04:00
self.lst = Lists(lst)
2022-03-18 18:38:36 +04:00
self.players = []
2022-03-25 16:45:38 +04:00
self.parsed = False
2022-03-18 18:38:36 +04:00
def list_players(self, cache:bool=True) -> List[str]:
"""Parse a players list
:param cache: If the function can return cached list (highly recommended), defaults to True
:type cache: bool, optional
:return: List of players nicknames
:rtype: List[str]
"""
2022-03-25 16:45:38 +04:00
if cache and self.parsed:
2022-03-18 18:38:36 +04:00
return self.players
listreq = self.atserv.atserver_request(
2022-03-25 16:45:38 +04:00
f'https://aternos.org/players/{self.lst.value}',
'GET'
2022-03-18 18:38:36 +04:00
)
listtree = lxml.html.fromstring(listreq.content)
items = listtree.xpath(
2022-03-25 16:45:38 +04:00
'//div[@class="list-item"]'
2022-03-18 18:38:36 +04:00
)
result = []
for i in items:
name = i.xpath('./div[@class="list-name"]')
2022-03-25 16:45:38 +04:00
result.append(name[0].text.strip())
self.players = result
self.parsed = True
2022-03-18 18:38:36 +04:00
return result
def add(self, name:str) -> None:
"""Appends a player to the list by the nickname
:param name: Player's nickname
:type name: str
"""
self.atserv.atserver_request(
'https://aternos.org/panel/ajax/players/add.php',
2022-03-18 18:38:36 +04:00
'POST', data={
2022-03-25 16:45:38 +04:00
'list': self.lst.value,
'name': name
2022-03-25 16:45:38 +04:00
}, sendtoken=True
)
2022-03-18 18:38:36 +04:00
self.players.append(name)
def remove(self, name:str) -> None:
"""Removes a player from the list by the nickname
:param name: Player's nickname
:type name: str
"""
self.atserv.atserver_request(
'https://aternos.org/panel/ajax/players/remove.php',
2022-03-18 18:38:36 +04:00
'POST', data={
2022-03-25 16:45:38 +04:00
'list': self.lst.value,
'name': name
2022-03-25 16:45:38 +04:00
}, sendtoken=True
)
2022-03-18 18:38:36 +04:00
for i, j in enumerate(self.players):
if j == name:
del self.players[i]