2022-03-18 18:38:36 +04:00
|
|
|
import enum
|
2021-10-15 15:12:45 +04:00
|
|
|
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 15:12:45 +04:00
|
|
|
|
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:
|
2021-10-15 15:12:45 +04:00
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
def __init__(self, lst:Union[str,Lists], atserv:'AternosServer') -> None:
|
2021-10-15 15:12:45 +04:00
|
|
|
|
|
|
|
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
|
2021-10-15 15:12:45 +04:00
|
|
|
|
|
|
|
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,
|
2021-10-15 15:12:45 +04:00
|
|
|
'name': name
|
2022-03-25 16:45:38 +04:00
|
|
|
}, sendtoken=True
|
2021-10-15 15:12:45 +04:00
|
|
|
)
|
|
|
|
|
2022-03-18 18:38:36 +04:00
|
|
|
self.players.append(name)
|
|
|
|
|
2021-10-15 15:12:45 +04:00
|
|
|
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,
|
2021-10-15 15:12:45 +04:00
|
|
|
'name': name
|
2022-03-25 16:45:38 +04:00
|
|
|
}, sendtoken=True
|
2021-10-15 15:12:45 +04:00
|
|
|
)
|
|
|
|
|
2022-03-18 18:38:36 +04:00
|
|
|
for i, j in enumerate(self.players):
|
|
|
|
if j == name:
|
|
|
|
del self.players[i]
|