From 3a8703025a851270e18081e47af2fa719c280d22 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Fri, 22 Jan 2021 22:57:32 +0800 Subject: [PATCH] single picture and text message support long text --- rssbot.py | 67 ++++++++++++++++++++++++++++++++++++++-------------- rssparser.py | 1 + textc.py | 35 ++++++++++++++++++++++++++- 3 files changed, 84 insertions(+), 19 deletions(-) diff --git a/rssbot.py b/rssbot.py index a797dc5..75d5f89 100644 --- a/rssbot.py +++ b/rssbot.py @@ -188,26 +188,57 @@ class main: if not config.send_media or (getListCount(content, 'imgList') == 0 and getListCount(content, 'videoList') == 0): if config.disable_web_page_preview: di['disable_web_page_preview'] = True - di['text'] = text.tostr() - di['parse_mode'] = 'HTML' - re = self._request('sendMessage', 'post', json=di) + while len(text) > 0: + di['text'] = text.tostr() + di['parse_mode'] = 'HTML' + for i in range(self._setting._maxRetryCount + 1): + re = self._request('sendMessage', 'post', json=di) + if re is not None and 'ok' in re and re['ok']: + di['reply_to_message_id'] = re['result']['message_id'] + break + if i == self._setting._maxRetryCount: + if returnError and re is not None and 'description' in re: + return False, re['description'] + else: + return False, '' elif getListCount(content, 'imgList') == 1 and getListCount(content, 'videoList') == 0: - di['caption'] = text.tostr() - di['parse_mode'] = 'HTML' - if not self._setting._downloadMediaFile: - di['photo'] = content['imgList'][0] - re = self._request('sendPhoto', 'post', json=di) - else: - fileEntry = self._tempFileEntries.add(content['imgList'][0]) - if not fileEntry.ok: - return None - if self._setting._sendFileURLScheme: - di['photo'] = fileEntry._localURI - re = self._request('sendPhoto', 'post', json=di) + f = True + while len(text) > 0: + if f: + di['caption'] = text.tostr(1024) else: - fileEntry.open() - re = self._request('sendPhoto', 'post', json=di, files={ - 'photo': (fileEntry._fullfn, fileEntry._f)}) + di['text'] = text.tostr() + di['parse_mode'] = 'HTML' + for i in range(self._setting._maxRetryCount + 1): + if f: + if not self._setting._downloadMediaFile: + di['photo'] = content['imgList'][0] + re = self._request('sendPhoto', 'post', json=di) + else: + fileEntry = self._tempFileEntries.add(content['imgList'][0]) + if not fileEntry.ok: + continue + if self._setting._sendFileURLScheme: + di['photo'] = fileEntry._localURI + re = self._request('sendPhoto', 'post', json=di) + else: + fileEntry.open() + re = self._request('sendPhoto', 'post', json=di, files={'photo': (fileEntry._fullfn, fileEntry._f)}) + else: + re = self._request('sendMessage', 'post', json=di) + if re is not None and 'ok' in re and re['ok']: + di['reply_to_message_id'] = re['result']['message_id'] + if f: + del di['caption'] + if 'photo' in di: + del di['photo'] + f = False + break + if i == self._setting._maxRetryCount: + if returnError and re is not None and 'description' in re: + return False, re['description'] + else: + return False, '' elif getListCount(content, 'imgList') == 0 and getListCount(content, 'videoList') == 1: di['caption'] = text.tostr() di['parse_mode'] = 'HTML' diff --git a/rssparser.py b/rssparser.py index d5f254b..6fd0312 100644 --- a/rssparser.py +++ b/rssparser.py @@ -349,6 +349,7 @@ class RSSParser: try: if fn.find('://') > -1: re = requests.get(fn) + re.encoding = 'utf8' if re.status_code == 200: self.xmldoc = minidom.parseString(re.text) else: diff --git a/textc.py b/textc.py index aa061b6..46bb44d 100644 --- a/textc.py +++ b/textc.py @@ -18,12 +18,45 @@ from urllib.parse import unquote class textc: + def __len__(self): + return len(self.__str) + def __init__(self): self.__str = '' + self.__max = 4096 - def tostr(self): + def __str__(self): return self.__str + def checklen(self): + return len(self) <= self.__max + + def cut(self): + """TODO: Need check html is not breaked + TODO: Need calculate char limits after parsed""" + l = self.__str.splitlines(True) + r = '' + while len(f"{r}{l[0]}") <= self.__max: + r = r + l[0] + l = l[1:] + if len(r) == 0: + r = l[0][:self.__max] + l[0] = l[0][self.__max:] + t = '' + for i in l: + t = t + i + self.__str = t + return r + + def tostr(self, maxLength: int = 4096): + self.__max = maxLength + if self.checklen(): + t = self.__str + self.__str = '' + return t + else: + return self.cut() + def addtotext(self, s: str): if self.__str == '': self.__str = f"{self.__str}{s}"