mirror of
https://github.com/MrSedan/neuro-reply-bot-reworked.git
synced 2026-01-15 05:59:43 +03:00
Added comments
This commit is contained in:
@@ -1,18 +1,31 @@
|
||||
from aiohttp import ClientSession
|
||||
|
||||
from .api_method import ApiMethod
|
||||
|
||||
from neuroapi.types import Admin as AdminType
|
||||
|
||||
from .api_method import ApiMethod
|
||||
|
||||
|
||||
class Admin(ApiMethod):
|
||||
|
||||
"""Class for admin methods"""
|
||||
async def get(self):
|
||||
"""
|
||||
Asynchronous function to retrieve data from the specified API endpoint and return a list of admins.
|
||||
:return List[Admin]
|
||||
"""
|
||||
async with ClientSession() as session:
|
||||
response = await session.get(self.api_url+'/admin/get')
|
||||
return [AdminType.from_dict(admin) for admin in await response.json()]
|
||||
|
||||
async def is_admin(self, id: str):
|
||||
"""
|
||||
Asynchronous function to check if the user with the given ID is an admin.
|
||||
|
||||
Args:
|
||||
id (str): The ID of the user to be checked.
|
||||
|
||||
Returns:
|
||||
bool: True if the user is an admin, False otherwise.
|
||||
"""
|
||||
async with ClientSession() as session:
|
||||
response = await session.get(self.api_url+f'/admin/is-admin/{id}')
|
||||
if await response.text() == 'false':
|
||||
|
||||
@@ -4,4 +4,5 @@ from ..config import GlobalConfig as Config
|
||||
|
||||
|
||||
class ApiMethod(BaseModel):
|
||||
"""Base class for API methods"""
|
||||
api_url: str = Field(Config().api_url)
|
||||
|
||||
@@ -6,13 +6,29 @@ from .api_method import ApiMethod
|
||||
|
||||
|
||||
class BotSettings(ApiMethod):
|
||||
"""Class for bot settings API methods"""
|
||||
async def get(self)-> BotSettingsType:
|
||||
"""
|
||||
Asynchronous function that retrieves bot settings from the API.
|
||||
|
||||
Returns:
|
||||
BotSettings: The bot settings retrieved from the API.
|
||||
"""
|
||||
async with ClientSession() as session:
|
||||
response = await session.get(self.api_url+'/settings')
|
||||
settings = BotSettingsType.from_dict(await response.json())
|
||||
return settings
|
||||
|
||||
async def get_update(self) -> BotSettingsType:
|
||||
"""
|
||||
Asynchronously gets and returns the bot settings from the specified API URL. Clearing server cache.
|
||||
|
||||
Parameters:
|
||||
self: The instance of the class.
|
||||
|
||||
Returns:
|
||||
BotSettings: The bot settings obtained from the API.
|
||||
"""
|
||||
async with ClientSession() as session:
|
||||
response = await session.get(self.api_url+'/settings/active')
|
||||
settings = BotSettingsType.from_dict(await response.json())
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
"""Enums package"""
|
||||
from .get_all import EGetAll
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class EGetAll(Enum):
|
||||
class EGetAll(Enum):
|
||||
all = 'all'
|
||||
will_post = 'will-post'
|
||||
posted = 'posted'
|
||||
|
||||
@@ -9,7 +9,24 @@ from .api_method import ApiMethod
|
||||
|
||||
|
||||
class Image(ApiMethod):
|
||||
"""Class for Image API methods"""
|
||||
async def add(self, from_id: str, file_id: str, has_spoiler: bool | None, message_id: int, text: str, media_group_id: str | None, message_entities: Optional[List[MessageEntity]], message: types.Message):
|
||||
"""
|
||||
An asynchronous function to add an image to post, along with its metadata, to a specific API endpoint. Also, creates a new post.
|
||||
|
||||
Args:
|
||||
from_id (str): The ID of the user who sent the image.
|
||||
file_id (str): The ID of the file containing the image.
|
||||
has_spoiler (bool | None): A boolean indicating whether the image has spoiler content.
|
||||
message_id (int): The ID of the message containing the image.
|
||||
text (str): The text associated with the image.
|
||||
media_group_id (str | None): The ID of the media group containing the image, if applicable.
|
||||
message_entities (Optional[List[MessageEntity]]): A list of message entities associated with the image.
|
||||
message (types.Message): The message object associated with the image.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
payload = {'from_user_id': from_id, 'file_id': file_id,
|
||||
'has_spoiler': has_spoiler, 'message_id': message_id }
|
||||
if text != '':
|
||||
|
||||
@@ -12,8 +12,20 @@ from .enums import EGetAll
|
||||
|
||||
|
||||
class Post(ApiMethod):
|
||||
|
||||
"""Class for Post API methods"""
|
||||
async def new(self, text: str, from_user_id: str, media_group_id: str = "None", message_entities: Optional[List[MessageEntity]] = None):
|
||||
"""
|
||||
Asynchronously creates a new post with the given text, from_user_id, media_group_id, and message_entities.
|
||||
|
||||
Args:
|
||||
text (str): The text of the post.
|
||||
from_user_id (str): The ID of the user creating the post.
|
||||
media_group_id (str, optional): The media group ID. Defaults to "None".
|
||||
message_entities (List[MessageEntity], optional): List of message entities. Defaults to None.
|
||||
|
||||
Returns:
|
||||
Post: A new post created from the given data.
|
||||
"""
|
||||
payload = {'text': text, 'from_user_id': from_user_id}
|
||||
if media_group_id != 'None':
|
||||
payload['media_group_id'] = media_group_id
|
||||
@@ -32,23 +44,46 @@ class Post(ApiMethod):
|
||||
return neuroTypes.Post.from_dict(data)
|
||||
|
||||
async def __get_all(self, status: EGetAll):
|
||||
"""
|
||||
An asynchronous function to retrieve all items based on the given status using the provided API URL.
|
||||
It takes a status parameter of type EGetAll.
|
||||
The function returns the response obtained from the API call.
|
||||
"""
|
||||
async with ClientSession() as session:
|
||||
response = await session.get(self.api_url+f'/post/get-all/{status.value}')
|
||||
return response
|
||||
|
||||
async def get_all(self):
|
||||
"""
|
||||
Asynchronously retrieves all items and returns a list of Post objects.
|
||||
"""
|
||||
result = await self.__get_all(EGetAll.all)
|
||||
return [neuroTypes.Post.from_dict(post) for post in await result.json()]
|
||||
|
||||
async def get_will_post(self):
|
||||
"""
|
||||
Asynchronously retrieves and returns the will_post data from the API.
|
||||
"""
|
||||
result = await self.__get_all(EGetAll.will_post)
|
||||
return [neuroTypes.Post.from_dict(post) for post in await result.json()]
|
||||
|
||||
async def get_posted(self):
|
||||
"""
|
||||
Asynchronously gets all the posted items and returns a list of Post objects.
|
||||
"""
|
||||
result = await self.__get_all(EGetAll.posted)
|
||||
return [neuroTypes.Post.from_dict(post) for post in await result.json()]
|
||||
|
||||
async def get(self, post_id: str):
|
||||
"""
|
||||
An asynchronous function to retrieve a post by its ID from the API.
|
||||
|
||||
Args:
|
||||
post_id (str): The ID of the post to retrieve.
|
||||
|
||||
Returns:
|
||||
Post: The retrieved post object.
|
||||
"""
|
||||
async with ClientSession() as session:
|
||||
response = await session.get(self.api_url+f'/post/get/{post_id}')
|
||||
data = await response.json()
|
||||
@@ -57,6 +92,15 @@ class Post(ApiMethod):
|
||||
return neuroTypes.Post.from_dict(data)
|
||||
|
||||
async def get_by_order(self, post_order: str):
|
||||
"""
|
||||
Asynchronously gets a post by order from the API.
|
||||
|
||||
Args:
|
||||
post_order (str): The order of the post to retrieve.
|
||||
|
||||
Returns:
|
||||
Post: The post retrieved from the API.
|
||||
"""
|
||||
async with ClientSession() as session:
|
||||
response = await session.get(self.api_url+f'/post/get-post-by-order/{post_order}')
|
||||
data = await response.json()
|
||||
@@ -65,6 +109,15 @@ class Post(ApiMethod):
|
||||
return neuroTypes.Post.from_dict(data)
|
||||
|
||||
async def get_by_media_group_id(self, media_group_id: str):
|
||||
"""
|
||||
Asynchronous function to retrieve data by media group ID.
|
||||
|
||||
Args:
|
||||
media_group_id (str): The media group ID for retrieval.
|
||||
|
||||
Returns:
|
||||
neuroTypes.Post: The retrieved post data.
|
||||
"""
|
||||
async with ClientSession() as session:
|
||||
response = await session.get(self.api_url+f'/post/get-by-media-group-id/{media_group_id}')
|
||||
data = await response.json()
|
||||
@@ -73,6 +126,16 @@ class Post(ApiMethod):
|
||||
return neuroTypes.Post.from_dict(data)
|
||||
|
||||
async def edit_text(self, post_id: str, text: str):
|
||||
"""
|
||||
Asynchronously edits the text of a post.
|
||||
|
||||
Args:
|
||||
post_id (str): The ID of the post to edit.
|
||||
text (str): The new text for the post.
|
||||
|
||||
Returns:
|
||||
Post: The edited post object.
|
||||
"""
|
||||
response = requests.post(
|
||||
self.api_url+f"/post/edit/{post_id}", data={"text": text})
|
||||
data = response.json()
|
||||
@@ -81,6 +144,20 @@ class Post(ApiMethod):
|
||||
return neuroTypes.Post.from_dict(data)
|
||||
|
||||
async def edit_text_by_order_num(self, order: str, text: str, message_entities: Optional[List[MessageEntity]] = None):
|
||||
"""
|
||||
Asynchronously edits text by order number.
|
||||
|
||||
Args:
|
||||
order (str): The order number.
|
||||
text (str): The new text.
|
||||
message_entities (Optional[List[MessageEntity]], optional): A list of message entities. Defaults to None.
|
||||
|
||||
Returns:
|
||||
Post: The edited post.
|
||||
|
||||
Raises:
|
||||
Exception: If the response contains an error status code.
|
||||
"""
|
||||
payload = {"text": text}
|
||||
if message_entities is not None:
|
||||
if message_entities is not None:
|
||||
@@ -98,6 +175,18 @@ class Post(ApiMethod):
|
||||
return neuroTypes.Post.from_dict(data)
|
||||
|
||||
async def get_post_to_post(self):
|
||||
"""
|
||||
Retrieves a post from the API and returns it as a `neuroTypes.Post` object.
|
||||
|
||||
Returns:
|
||||
neuroTypes.Post: The retrieved post.
|
||||
|
||||
Raises:
|
||||
Exception: If the API returns a non-200 status code or if there is an error message in the response.
|
||||
|
||||
Returns:
|
||||
None: If the API returns a 404 status code.
|
||||
"""
|
||||
response = requests.get(self.api_url+f"/post/post")
|
||||
data = response.json()
|
||||
if 'statusCode' in data:
|
||||
@@ -108,11 +197,29 @@ class Post(ApiMethod):
|
||||
return neuroTypes.Post.from_dict(data)
|
||||
|
||||
async def delete_by_order(self, order: str):
|
||||
"""
|
||||
Asynchronously deletes a post by order.
|
||||
|
||||
Args:
|
||||
order (str): The order of the post to be deleted.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
response = requests.delete(self.api_url+f"/post/delete-post-by-order/{order}")
|
||||
data = response.json()
|
||||
if 'statusCode' in data:
|
||||
raise Exception(data['message'])
|
||||
async def get_deleted_posts(self) -> List[neuroTypes.Post]:
|
||||
"""
|
||||
Asynchronously retrieves a list of deleted posts from the API.
|
||||
|
||||
Parameters:
|
||||
self: The instance of the class.
|
||||
|
||||
Returns:
|
||||
List[Post]: A list of Post objects representing the deleted posts.
|
||||
"""
|
||||
async with ClientSession() as session:
|
||||
response = await session.get(self.api_url+f'/post/get-deleted')
|
||||
data = await response.json()
|
||||
@@ -121,6 +228,15 @@ class Post(ApiMethod):
|
||||
return [neuroTypes.Post.from_dict(post) for post in data]
|
||||
|
||||
async def restore_post(self, order: str):
|
||||
"""
|
||||
Asynchronously restores a post using the given order string.
|
||||
|
||||
Args:
|
||||
order (str): The order string used to identify the post to be restored.
|
||||
|
||||
Returns:
|
||||
Post: A Post object representing the restored post.
|
||||
"""
|
||||
async with ClientSession() as session:
|
||||
response = await session.put(self.api_url+f'/post/restore-post-by-order/{order}')
|
||||
data = await response.json()
|
||||
|
||||
@@ -4,7 +4,21 @@ from .api_method import ApiMethod
|
||||
|
||||
|
||||
class User(ApiMethod):
|
||||
"""User class for API Methods"""
|
||||
async def get(self, id: str, username: str):
|
||||
"""
|
||||
Asynchronous function to retrieve user information by ID and username.
|
||||
|
||||
Args:
|
||||
id (str): The user ID.
|
||||
username (str): The username.
|
||||
|
||||
Raises:
|
||||
Exception: If the API request failing.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
payload = {'id': id, 'username': username}
|
||||
async with ClientSession() as session:
|
||||
response = await session.post(
|
||||
|
||||
Reference in New Issue
Block a user