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

73 lines
1.5 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:
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]:
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:
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:
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]