add cache for gen msg

This commit is contained in:
felinae98 2021-09-02 14:58:08 +08:00
parent ad1bd7f6e0
commit 129269c193
No known key found for this signature in database
GPG Key ID: 00C8B010587FF610

View File

@ -19,9 +19,10 @@ class Post:
target_name: Optional[str] = None
compress: bool = False
override_use_pic: Optional[bool] = None
pics: list[str] = field(default_factory=list)
_message: Optional[list] = None
def _use_pic(self):
if not self.override_use_pic is None:
return self.override_use_pic
@ -97,27 +98,29 @@ class Post:
self.pics.insert(0, b64image)
async def generate_messages(self):
await self._pic_merge()
msgs = []
text = ''
if self.text:
text += '{}'.format(self.text if len(self.text) < 500 else self.text[:500] + '...')
text += '\n来源: {}'.format(self.target_type)
if self.target_name:
text += ' {}'.format(self.target_name)
if self._use_pic():
msgs.append(await parse_text(text))
if not self.target_type == 'rss' and self.url:
msgs.append(self.url)
else:
if self.url:
text += ' \n详情: {}'.format(self.url)
msgs.append(text)
for pic in self.pics:
msgs.append("[CQ:image,file={url}]".format(url=pic))
if self.compress:
msgs = [''.join(msgs)]
return msgs
if self._message is None:
await self._pic_merge()
msgs = []
text = ''
if self.text:
text += '{}'.format(self.text if len(self.text) < 500 else self.text[:500] + '...')
text += '\n来源: {}'.format(self.target_type)
if self.target_name:
text += ' {}'.format(self.target_name)
if self._use_pic():
msgs.append(await parse_text(text))
if not self.target_type == 'rss' and self.url:
msgs.append(self.url)
else:
if self.url:
text += ' \n详情: {}'.format(self.url)
msgs.append(text)
for pic in self.pics:
msgs.append("[CQ:image,file={url}]".format(url=pic))
if self.compress:
msgs = [''.join(msgs)]
self._message = msgs
return self._message
def __str__(self):
return 'type: {}\nfrom: {}\ntext: {}\nurl: {}\npic: {}'.format(