summaryrefslogtreecommitdiff
path: root/ATRI/request.py
diff options
context:
space:
mode:
authorKyomotoi <[email protected]>2021-02-06 00:32:26 +0800
committerKyomotoi <[email protected]>2021-02-06 00:32:26 +0800
commitf5ceb8927f2e7f2a9e29d62c8e4cef876f917249 (patch)
tree40b9dcd6b7d3db486054e3aa9b5a04d25fa2284e /ATRI/request.py
parenteb52fab79ada7efe6191e3a5f90179766feaded0 (diff)
downloadATRI-f5ceb8927f2e7f2a9e29d62c8e4cef876f917249.tar.gz
ATRI-f5ceb8927f2e7f2a9e29d62c8e4cef876f917249.tar.bz2
ATRI-f5ceb8927f2e7f2a9e29d62c8e4cef876f917249.zip
🏗 💩 更改项目结构,修复啥b BUG
Diffstat (limited to 'ATRI/request.py')
-rw-r--r--ATRI/request.py68
1 files changed, 0 insertions, 68 deletions
diff --git a/ATRI/request.py b/ATRI/request.py
deleted file mode 100644
index 78bad6e..0000000
--- a/ATRI/request.py
+++ /dev/null
@@ -1,68 +0,0 @@
-import os
-import string
-import aiofiles
-from random import sample
-from pathlib import Path
-from typing import Optional
-from aiohttp.client import ClientSession
-
-from .exceptions import InvalidWriteText
-
-
-TEMP_PATH = Path('.') / 'ATRI' / 'data' / 'temp'
-os.makedirs(TEMP_PATH, exist_ok=True)
-
-
-class Request:
- @staticmethod
- async def get_text(url: str, headers: Optional[dict] = None) -> str:
- '''
- 异步方式请求接口
- :return: text
- '''
- async with ClientSession() as session:
- async with session.get(url, headers=headers) as r:
- result = await r.text()
- return result
-
- @staticmethod
- async def get_bytes(url: str, headers: Optional[dict] = None) -> bytes:
- '''
- 异步方式请求接口
- :return: bytes
- '''
- async with ClientSession() as session:
- async with session.get(url, headers=headers) as r:
- result = await r.read()
- return result
-
- @classmethod
- async def get_image(cls, url: str, params: Optional[dict] = None) -> str:
- '''
- 异步方式下载图片
- :return: img file
- '''
- file_path = TEMP_PATH / 'img'
- os.makedirs(file_path, exist_ok=True)
- file_name = ''.join(sample(string.ascii_letters + string.digits, 16))
- path = os.path.abspath(file_path)
- path = path + f'\\{file_name}.png'
- async with ClientSession() as session:
- async with session.get(url) as res:
- try:
- async with aiofiles.open(path, 'wb') as f:
- await f.write(await res.read())
- except InvalidWriteText:
- raise InvalidWriteText('Writing file failed!')
- return path
-
- @staticmethod
- async def post_bytes(url: str, params: Optional[dict] = None) -> bytes:
- '''
- 异步方式 Post 接口
- :return: bytes
- '''
- async with ClientSession() as session:
- async with session.post(url, params=params) as r:
- result = await r.read()
- return result \ No newline at end of file