mirror of
https://github.com/MrSedan/neuro-reply-bot-reworked.git
synced 2026-01-14 13:39:42 +03:00
ADDED: module for work with api
This commit is contained in:
17
neuroapi/__init__.py
Normal file
17
neuroapi/__init__.py
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
from .post import Post
|
||||||
|
from .admin import Admin
|
||||||
|
from .user import User
|
||||||
|
from .image import Image
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
import os
|
||||||
|
from os.path import join, dirname
|
||||||
|
|
||||||
|
|
||||||
|
load_dotenv(join(dirname(__file__), "..", '.env'))
|
||||||
|
|
||||||
|
|
||||||
|
class neuroapi:
|
||||||
|
post = Post(os.environ.get('API_URL'))
|
||||||
|
admin = Admin(os.environ.get('API_URL'))
|
||||||
|
user = User(os.environ.get('API_URL'))
|
||||||
|
image = Image(os.environ.get('API_URL'))
|
||||||
17
neuroapi/admin.py
Normal file
17
neuroapi/admin.py
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
from aiohttp import ClientSession
|
||||||
|
from .api_method import ApiMethod
|
||||||
|
|
||||||
|
|
||||||
|
class Admin(ApiMethod):
|
||||||
|
|
||||||
|
async def get(self):
|
||||||
|
async with ClientSession() as session:
|
||||||
|
response = await session.get(self.api_url+'/admin/get')
|
||||||
|
return await response.json()
|
||||||
|
|
||||||
|
async def is_admin(self, id: str):
|
||||||
|
async with ClientSession() as session:
|
||||||
|
response = await session.get(self.api_url+f'admin/is-admin/{id}')
|
||||||
|
if await response.text() == 'false':
|
||||||
|
return False
|
||||||
|
return True
|
||||||
5
neuroapi/api_method.py
Normal file
5
neuroapi/api_method.py
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
class ApiMethod:
|
||||||
|
api_url: str
|
||||||
|
|
||||||
|
def __init__(self, api_url: str) -> None:
|
||||||
|
self.api_url = api_url
|
||||||
1
neuroapi/enums/__init__.py
Normal file
1
neuroapi/enums/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from .get_all import EGetAll
|
||||||
7
neuroapi/enums/get_all.py
Normal file
7
neuroapi/enums/get_all.py
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
|
||||||
|
class EGetAll(Enum):
|
||||||
|
all = 'all'
|
||||||
|
will_post = 'will-post'
|
||||||
|
posted = 'posted'
|
||||||
14
neuroapi/image.py
Normal file
14
neuroapi/image.py
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
from aiohttp import ClientSession
|
||||||
|
from .api_method import ApiMethod
|
||||||
|
|
||||||
|
|
||||||
|
class Image(ApiMethod):
|
||||||
|
async def add(self, post_id: str, file_id: str, has_spoiler: bool, message_id: int):
|
||||||
|
payload = {'post_id': post_id, 'file_id': file_id,
|
||||||
|
'has_spoiler': has_spoiler, 'message_id': message_id}
|
||||||
|
async with ClientSession() as session:
|
||||||
|
response = await session.post(
|
||||||
|
self.api_url+'/image/add', data=payload)
|
||||||
|
data = await response.json()
|
||||||
|
if 'statusCode' in data:
|
||||||
|
raise Exception(data['message'])
|
||||||
50
neuroapi/post.py
Normal file
50
neuroapi/post.py
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
from aiohttp import ClientSession, ClientResponse
|
||||||
|
from .enums import EGetAll
|
||||||
|
from .api_method import ApiMethod
|
||||||
|
|
||||||
|
|
||||||
|
class Post(ApiMethod):
|
||||||
|
|
||||||
|
async def new(self, text: str, from_user_id: str, media_group_id: str):
|
||||||
|
payload = {'text': text, 'from_user_id': from_user_id}
|
||||||
|
if media_group_id != 'None':
|
||||||
|
payload['media_group_id'] = media_group_id
|
||||||
|
async with ClientSession() as session:
|
||||||
|
response: ClientResponse = await session.post(self.api_url+'/post/new', data=payload)
|
||||||
|
data = await response.json()
|
||||||
|
if 'statusCode' in data:
|
||||||
|
raise Exception(data['message'])
|
||||||
|
return data['status']
|
||||||
|
|
||||||
|
async def __get_all(self, status: EGetAll):
|
||||||
|
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):
|
||||||
|
result = await self.__get_all(EGetAll.all)
|
||||||
|
return await result.json()
|
||||||
|
|
||||||
|
async def get_will_post(self):
|
||||||
|
result = await self.__get_all(EGetAll.will_post)
|
||||||
|
return await result.json()
|
||||||
|
|
||||||
|
async def get_posted(self):
|
||||||
|
result = await self.__get_all(EGetAll.posted)
|
||||||
|
return await result.json()
|
||||||
|
|
||||||
|
async def get(self, post_id: str):
|
||||||
|
async with ClientSession() as session:
|
||||||
|
response = await session.get(self.api_url+f'/post/get/{post_id}')
|
||||||
|
data = await response.json()
|
||||||
|
if 'statusCode' in data:
|
||||||
|
raise Exception(data['message'])
|
||||||
|
return data
|
||||||
|
|
||||||
|
async def get_by_media_group_id(self, media_group_id: str):
|
||||||
|
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()
|
||||||
|
if 'statusCode' in data:
|
||||||
|
raise Exception(data['message'])
|
||||||
|
return await response.json()
|
||||||
13
neuroapi/user.py
Normal file
13
neuroapi/user.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
from aiohttp import ClientSession
|
||||||
|
from .api_method import ApiMethod
|
||||||
|
|
||||||
|
|
||||||
|
class User(ApiMethod):
|
||||||
|
async def get(self, id: str, username: str):
|
||||||
|
payload = {'id': id, 'username': username}
|
||||||
|
async with ClientSession() as session:
|
||||||
|
response = await session.post(
|
||||||
|
self.api_url+'/user/get', data=payload)
|
||||||
|
data = await response.json()
|
||||||
|
if 'statusCode' in data:
|
||||||
|
raise Exception(data['message'])
|
||||||
Reference in New Issue
Block a user