Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/core/elements/message/internal.py

119 lines
3.4 KiB
Python
Raw Normal View History

2021-08-07 07:56:48 +00:00
import re
2021-07-26 12:43:51 +00:00
import uuid
2021-11-12 14:25:53 +00:00
from os.path import abspath
2021-12-31 14:44:34 +00:00
from typing import List, Union
2021-11-12 14:25:53 +00:00
2021-08-25 14:32:37 +00:00
import aiohttp
2021-07-26 12:43:51 +00:00
import filetype
2021-10-12 15:02:36 +00:00
from PIL import Image as PImage
2021-12-31 14:44:34 +00:00
from tenacity import retry, stop_after_attempt
2021-11-12 14:25:53 +00:00
2021-08-07 07:56:48 +00:00
from config import CachePath
2021-07-26 12:43:51 +00:00
class Plain:
def __init__(self,
text, *texts):
self.text = str(text)
for t in texts:
self.text += str(t)
2021-07-26 12:43:51 +00:00
class Image:
def __init__(self,
2021-12-31 14:44:34 +00:00
path, headers=None):
self.need_get = False
self.path = path
2021-12-31 14:44:34 +00:00
self.headers = headers
2021-10-12 15:02:36 +00:00
if isinstance(path, PImage.Image):
2021-12-31 14:44:34 +00:00
save = f'{CachePath}{str(uuid.uuid4())}.jpg'
path.convert('RGB').save(save)
self.path = save
2021-10-12 15:02:36 +00:00
elif re.match('^https?://.*', path):
self.need_get = True
async def get(self):
if self.need_get:
2021-12-31 14:44:34 +00:00
return abspath(await self.get_image())
2021-10-12 15:02:36 +00:00
return abspath(self.path)
2021-07-26 12:43:51 +00:00
2021-12-31 14:44:34 +00:00
@retry(stop=stop_after_attempt(3))
async def get_image(self):
url = self.path
async with aiohttp.ClientSession() as session:
async with session.get(url, timeout=aiohttp.ClientTimeout(total=20)) as req:
raw = await req.read()
ft = filetype.match(raw).extension
img_path = f'{CachePath}{str(uuid.uuid4())}.{ft}'
with open(img_path, 'wb+') as image_cache:
image_cache.write(raw)
return img_path
2021-07-28 18:51:24 +00:00
class Voice:
def __init__(self,
path=None):
self.path = path
2021-08-23 12:44:31 +00:00
2021-12-25 14:01:33 +00:00
class EmbedField:
def __init__(self,
name: str = None,
value: str = None,
inline: bool = False):
self.name = name
self.value = value
self.inline = inline
class Embed:
def __init__(self,
title: str = None,
description: str = None,
url: str = None,
timestamp: float = None,
color: int = None,
# image: Image = None,
# thumbnail: Image = None,
author: str = None,
footer: str = None,
fields: List[EmbedField] = None):
self.title = title
self.description = description
self.url = url
self.timestamp = timestamp
self.color = color
# self.image = image
# self.thumbnail = thumbnail
self.author = author
self.footer = footer
self.fields = fields
def to_msgchain(self):
text_lst = []
if self.title is not None:
text_lst.append(self.title)
if self.description is not None:
text_lst.append(self.description)
if self.url is not None:
text_lst.append(self.url)
if self.fields is not None:
for f in self.fields:
if f.inline:
text_lst.append(f"{f.name}: {f.value}")
else:
text_lst.append(f"{f.name}:\n{f.value}")
if self.author is not None:
text_lst.append("作者:" + self.author)
if self.footer is not None:
text_lst.append(self.footer)
msgchain = []
if text_lst:
msgchain.append(Plain('\n'.join(text_lst)))
# if self.image is not None:
# msgchain.append(self.image)
return msgchain
__all__ = ["Plain", "Image", "Voice", "Embed", "EmbedField"]