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/atfile.py

100 lines
2.1 KiB
Python
Raw Normal View History

2022-03-18 18:38:36 +04:00
import enum
import lxml.html
from typing import Union
2021-10-15 19:31:47 +04:00
from typing import TYPE_CHECKING
2022-03-18 18:38:36 +04:00
from .aterrors import FileError
2021-10-15 19:31:47 +04:00
if TYPE_CHECKING:
2022-03-18 18:38:36 +04:00
from .atserver import AternosServer
2021-10-15 19:31:47 +04:00
2022-03-18 18:38:36 +04:00
class FileType(enum.IntEnum):
file = 0
directory = 1
class AternosFile:
2021-10-14 18:41:57 +04:00
def __init__(
2021-10-15 19:31:47 +04:00
self, atserv:'AternosServer',
2022-03-18 18:38:36 +04:00
path:str, name:str, ftype:int=FileType.file,
2022-03-25 16:45:38 +04:00
size:Union[int,float]=0) -> None:
self.atserv = atserv
self._path = path.lstrip('/')
self._name = name
2022-03-25 16:45:38 +04:00
self._full = path + name
self._ftype = ftype
2021-10-14 18:41:57 +04:00
self._size = float(size)
2021-10-14 18:41:57 +04:00
def delete(self) -> None:
self.atserv.atserver_request(
'https://aternos.org/panel/ajax/delete.php',
2022-03-25 16:45:38 +04:00
'POST', data={'file': self._full},
sendtoken=True
)
2022-03-18 18:38:36 +04:00
def get_content(self) -> bytes:
2022-03-25 16:45:38 +04:00
file = self.atserv.atserver_request(
2022-03-25 16:45:38 +04:00
'https://aternos.org/panel/ajax/files/download.php',
2022-03-18 18:38:36 +04:00
'GET', params={
2022-03-25 16:45:38 +04:00
'file': self._full
2022-03-18 18:38:36 +04:00
}
)
2022-03-25 16:45:38 +04:00
if file.content == b'{"success":false}':
raise FileError('Unable to download the file. Try to get text')
return file.content
2022-03-18 18:38:36 +04:00
def set_content(self, value:bytes) -> None:
2022-03-25 16:45:38 +04:00
self.atserv.atserver_request(
f'https://aternos.org/panel/ajax/save.php',
2022-03-18 18:38:36 +04:00
'POST', data={
2022-03-25 16:45:38 +04:00
'file': self._full,
2022-03-18 18:38:36 +04:00
'content': value
}, sendtoken=True
)
2022-03-18 18:38:36 +04:00
def get_text(self) -> str:
2022-03-25 16:45:38 +04:00
editor = self.atserv.atserver_request(
f'https://aternos.org/files/{self._full.lstrip("/")}', 'GET'
)
edittree = lxml.html.fromstring(editor.content)
editblock = edittree.xpath('//div[@id="editor"]')[0]
return editblock.text_content()
2022-03-18 18:38:36 +04:00
def set_text(self, value:str) -> None:
2022-03-25 16:45:38 +04:00
2022-03-18 18:38:36 +04:00
self.set_content(value.encode('utf-8'))
@property
def path(self):
return self._path
@property
2021-10-14 18:41:57 +04:00
def name(self) -> str:
return self._name
2022-03-25 16:45:38 +04:00
@property
def full(self) -> str:
return self._full
@property
def is_dir(self) -> bool:
2022-03-18 18:38:36 +04:00
if self._ftype == FileType.directory:
return True
return False
@property
def is_file(self) -> bool:
2022-03-18 18:38:36 +04:00
if self._ftype == FileType.file:
return True
return False
@property
2021-10-14 18:41:57 +04:00
def size(self) -> float:
return self._size