From ab54a5f5aed4022ab616066ccbc874140d0151e0 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Tue, 30 Aug 2022 14:40:03 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8D=95=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=8F=91=E9=80=81=E6=97=B6=E4=BE=9D=E6=97=A7?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E7=BC=BA=E5=B0=91=E7=BC=A9=E7=95=A5=E5=9B=BE?= =?UTF-8?q?=E7=9A=84BUG=20=E5=A4=9A=E4=B8=AA=E5=AA=92=E4=BD=93=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=BF=AE=E5=A4=8D=EF=BC=9A=20=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E6=B7=B7=E5=90=88=E6=96=87=E4=BB=B6=E5=92=8C=E5=85=B6=E4=BB=96?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=96=87=E4=BB=B6=E7=9A=84=E7=B1=BB=E5=88=AB?= =?UTF-8?q?=20PHOTO=5FINVALID=5FDIMENSIONS=E6=8A=A5=E9=94=99=20=E9=83=A8?= =?UTF-8?q?=E5=88=86=E5=9B=BE=E7=89=87=E6=96=87=E4=BB=B6=E7=BC=BA=E5=B0=91?= =?UTF-8?q?=E7=BC=A9=E7=95=A5=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fileEntry.py | 7 +- rssbot.py | 248 ++++++++++++++++++++++++++++++--------------------- 2 files changed, 148 insertions(+), 107 deletions(-) diff --git a/fileEntry.py b/fileEntry.py index 1409973..a78ef8a 100644 --- a/fileEntry.py +++ b/fileEntry.py @@ -89,7 +89,8 @@ class FileEntry: self._tempdir: str = None ph = urlsplit(url).path self._ext = splitext(ph)[1] - if self._ext == '' and ph.endswith('/RSSProxy'): # Support my own proxy link + is_rssproxy = ph.endswith('/RSSProxy') or ph.endswith('/RSSProxy/') + if self._ext == '' and is_rssproxy: # Support my own proxy link qs = parse_qs(urlsplit(url).query) if 't' in qs: self._ext = splitext(urlsplit(qs['t'][0]).path)[1] @@ -98,9 +99,9 @@ class FileEntry: if self._config.send_origin_file_name: self._usetempdir = True self._tempdir = f"{time_ns()}{randint(0, 9999)}" - if ph.endswith('/RSSProxy'): + if is_rssproxy: self._fn = basename(splitext(urlsplit(qs['t'][0]).path)[0]) - if self._fn == '': + if self._fn is None or self._fn == '': self._fn = basename(splitext(ph)[0]) if self._fn == '': self._usetempdir = False diff --git a/rssbot.py b/rssbot.py index 7d0a256..ab139ac 100644 --- a/rssbot.py +++ b/rssbot.py @@ -284,9 +284,9 @@ class main: continue should_use_file = False if fileEntry._fileSize < MAX_PHOTO_SIZE and not config.send_img_as_file else True is_supported_photo = None - if not should_use_file and self._rssbotLib is not None: + if self._rssbotLib is not None: is_supported_photo = self._rssbotLib.is_supported_photo(fileEntry._abspath) - if is_supported_photo is not None: + if not should_use_file and is_supported_photo is not None: should_use_file = not is_supported_photo if self._setting.sendFileURLScheme: if not should_use_file: @@ -558,16 +558,111 @@ class main: return False sleep(5) else: - # TODO: 修复 PHOTO_INVALID_DIMENSIONS - # TODO: 修复部分图片文件缺少缩略图 - # TODO: 修复图片和文件不可混用 ind = 0 if self._setting.downloadMediaFile and not self._setting.sendFileURLScheme: ind2 = 0 di3 = {} di['media'] = [] - for i in content['imgList']: - if ind % MAX_ITEM_IN_MEDIA_GROUP == 0 and ind != 0: + contain_files = False + contain_nonfiles = False + re = None + def send_file_in_list(): + nonlocal re + if len(di['media']) == 1: + tmp_di = di['media'][0] + for k in di.keys(): + if k != 'media': + tmp_di[k] = di[k] + di['media'] = di['media'][1:] + if tmp_di['type'] == 'photo': + if not self._setting.downloadMediaFile or self._setting.sendFileURLScheme: + tmp_di['photo'] = tmp_di['media'] + else: + tmp_di3 = {} + mekey = tmp_di['media'][9:] + tmp_di3['photo'] = di3[mekey] + del di3[mekey] + del tmp_di['media'] + for _ in range(self._setting.maxRetryCount + 1): + if not self._setting.downloadMediaFile or self._setting.sendFileURLScheme: + re = self._request('sendPhoto', 'post', json=tmp_di) + else: + re = self._request( + 'sendPhoto', 'post', json=tmp_di, files=tmp_di3) + if re is not None and 'ok' in re and re['ok']: + di['reply_to_message_id'] = re['result']['message_id'] + break + sleep(5) + elif tmp_di['type'] == 'video': + if not self._setting.downloadMediaFile or self._setting.sendFileURLScheme: + tmp_di['video'] = tmp_di['media'] + else: + tmp_di3 = {} + mekey = tmp_di['media'][9:] + tmp_di3['video'] = di3[mekey] + del di3[mekey] + if 'thumb' in tmp_di: + mekey = tmp_di['thumb'][9:] + tmp_di3['thumb'] = di3[mekey] + del di3[mekey] + del tmp_di['media'] + for _ in range(self._setting.maxRetryCount + 1): + if not self._setting.downloadMediaFile or self._setting.sendFileURLScheme: + re = self._request('sendVideo', 'post', json=tmp_di) + else: + re = self._request( + 'sendVideo', 'post', json=tmp_di, files=tmp_di3) + if re is not None and 'ok' in re and re['ok']: + di['reply_to_message_id'] = re['result']['message_id'] + break + sleep(5) + elif tmp_di['type'] == 'document': + if not self._setting.downloadMediaFile or self._setting.sendFileURLScheme: + tmp_di['document'] = tmp_di['media'] + else: + tmp_di3 = {} + mekey = tmp_di['media'][9:] + tmp_di3['document'] = di3[mekey] + del di3[mekey] + if 'thumb' in tmp_di: + mekey = tmp_di['thumb'][9:] + tmp_di3['thumb'] = di3[mekey] + del di3[mekey] + del tmp_di['media'] + for _ in range(self._setting.maxRetryCount + 1): + if not self._setting.downloadMediaFile or self._setting.sendFileURLScheme: + re = self._request('sendDocument', 'post', json=tmp_di) + else: + re = self._request( + 'sendDocument', 'post', json=tmp_di, files=tmp_di3) + if re is not None and 'ok' in re and re['ok']: + di['reply_to_message_id'] = re['result']['message_id'] + break + sleep(5) + elif tmp_di['type'] == 'animation': + if not self._setting.downloadMediaFile or self._setting.sendFileURLScheme: + tmp_di['animation'] = tmp_di['media'] + else: + tmp_di3 = {} + mekey = tmp_di['media'][9:] + tmp_di3['animation'] = di3[mekey] + del di3[mekey] + if 'thumb' in tmp_di: + mekey = tmp_di['thumb'][9:] + tmp_di3['thumb'] = di3[mekey] + del di3[mekey] + del tmp_di['media'] + for _ in range(self._setting.maxRetryCount + 1): + if not self._setting.downloadMediaFile or self._setting.sendFileURLScheme: + re = self._request('sendAnimation', 'post', json=tmp_di) + else: + re = self._request( + 'sendAnimation', 'post', json=tmp_di, files=tmp_di3) + if re is not None and 'ok' in re and re['ok']: + di['reply_to_message_id'] = re['result']['message_id'] + break + sleep(5) + else: for _ in range(self._setting.maxRetryCount + 1): if not self._setting.downloadMediaFile or self._setting.sendFileURLScheme: re = self._request('sendMediaGroup', 'post', json=di) @@ -584,16 +679,49 @@ class main: di3 = {} break sleep(5) + nonlocal contain_files + nonlocal contain_nonfiles + contain_files = False + contain_nonfiles = False + + for i in content['imgList']: + if len(di['media']) == MAX_ITEM_IN_MEDIA_GROUP: + send_file_in_list() di2 = {'type': 'photo'} if not self._setting.downloadMediaFile: di2['media'] = i + if contain_files: + send_file_in_list() + contain_nonfiles = True else: fileEntry = self._tempFileEntries.add(i, config) if not fileEntry.ok: return None should_use_file = False if fileEntry._fileSize < MAX_PHOTO_SIZE and not config.send_img_as_file else True + is_supported_photo = None + if self._rssbotLib is not None: + is_supported_photo = self._rssbotLib.is_supported_photo(fileEntry._abspath) + if not should_use_file and is_supported_photo is not None: + should_use_file = not is_supported_photo if should_use_file: + if contain_nonfiles: + send_file_in_list() + contain_files = True di2['type'] = 'document' + if is_supported_photo is False or (self._rssbotLib is not None and fileEntry._fileSize >= MAX_PHOTO_SIZE): + if self._rssbotLib.convert_to_tg_thumbnail(fileEntry, 'jpeg'): + thumb_file = fileEntry.getSubFile('_thumbnail', 'jpeg') + if self._setting.sendFileURLScheme: + di2['thumb'] = thumb_file._localURI + else: + thumb_file.open() + di2['thumb'] = f'attach://file{ind2}' + di3[f'file{ind2}'] = (thumb_file._fullfn, fileEntry._f) + ind2 += 1 + else: + if contain_files: + send_file_in_list() + contain_nonfiles = True if self._setting.sendFileURLScheme: di2['media'] = fileEntry._localURI else: @@ -601,29 +729,14 @@ class main: di2['media'] = f'attach://file{ind2}' di3[f'file{ind2}'] = (fileEntry._fullfn, fileEntry._f) ind2 = ind2 + 1 - if ind % MAX_ITEM_IN_MEDIA_GROUP == 0: + if len(di['media']) == 0: di2['caption'] = text.tostr(1024) di2['parse_mode'] = 'HTML' di['media'].append(di2) ind = ind + 1 for i in content['videoList']: - if ind % MAX_ITEM_IN_MEDIA_GROUP == 0 and ind != 0: - for _ in range(self._setting.maxRetryCount + 1): - if not self._setting.downloadMediaFile or self._setting.sendFileURLScheme: - re = self._request('sendMediaGroup', 'post', json=di) - if re is not None and 'ok' in re and re['ok']: - di['reply_to_message_id'] = re['result']['message_id'] - di['media'] = [] - break - else: - re = self._request( - 'sendMediaGroup', 'post', json=di, files=di3) - if re is not None and 'ok' in re and re['ok']: - di['reply_to_message_id'] = re['result']['message_id'] - di['media'] = [] - di3 = {} - break - sleep(5) + if len(di['media']) == MAX_ITEM_IN_MEDIA_GROUP: + send_file_in_list() di2 = {'type': 'video', 'supports_streaming': True} if not self._setting.downloadMediaFile: di2['media'] = i['src'] @@ -638,6 +751,9 @@ class main: di2['media'] = f'attach://file{ind2}' di3[f'file{ind2}'] = (fileEntry._fullfn, fileEntry._f) ind2 = ind2 + 1 + if contain_files: + send_file_in_list() + contain_nonfiles = True if 'poster' in i and i['poster'] is not None and i['poster'] != '': if not self._setting.downloadMediaFile: di2['thumb'] = i['poster'] @@ -653,7 +769,7 @@ class main: di3[f'file{ind2}'] = ( fileEntry._fullfn, fileEntry._f) ind2 = ind2 + 1 - if ind % MAX_ITEM_IN_MEDIA_GROUP == 0: + if len(di['media']) == 0: di2['caption'] = text.tostr(1024) di2['parse_mode'] = 'HTML' if self._rssbotLib is not None: @@ -664,84 +780,8 @@ class main: continue di['media'].append(di2) ind = ind + 1 - if len(di['media']) > 1: - for _ in range(self._setting.maxRetryCount + 1): - if not self._setting.downloadMediaFile or self._setting.sendFileURLScheme: - re = self._request('sendMediaGroup', 'post', json=di) - else: - re = self._request( - 'sendMediaGroup', 'post', json=di, files=di3) - if re is not None and 'ok' in re and re['ok']: - break - sleep(5) - if len(di['media']) == 1: - if 'caption' in di['media'][0]: - di['caption'] = di['media'][0]['caption'] - di['parse_mode'] = 'HTML' - if di['media'][0]['type'] == 'photo': - if not self._setting.downloadMediaFile or self._setting.sendFileURLScheme: - di['photo'] = di['media'][0]['media'] - else: - mekey = di['media'][0]['media'][9:] - di3['photo'] = di3[mekey] - del di3[mekey] - del di['media'] - for _ in range(self._setting.maxRetryCount + 1): - if not self._setting.downloadMediaFile or self._setting.sendFileURLScheme: - re = self._request('sendPhoto', 'post', json=di) - else: - re = self._request( - 'sendPhoto', 'post', json=di, files=di3) - if re is not None and 'ok' in re and re['ok']: - break - sleep(5) - elif di['media'][0]['type'] == 'video': - if not self._setting.downloadMediaFile or self._setting.sendFileURLScheme: - di['video'] = di['media'][0]['media'] - if 'thumb' in di['media'][0]: - di['thumb'] = di['media'][0]['thumb'] - else: - mekey = di['media'][0]['media'][9:] - di3['video'] = di3[mekey] - del di3[mekey] - if 'thumb' in di['media'][0]: - mekey = di['media'][0]['thumb'][9:] - di3['thumb'] = di3[mekey] - del di3[mekey] - if 'duration' in di['media'][0]: - di['duration'] = di['media'][0]['duration'] - if 'width' in di['media'][0]: - di['width'] = di['media'][0]['width'] - if 'height' in di['media'][0]: - di['height'] = di['media'][0]['height'] - di['supports_streaming'] = di['media'][0]['supports_streaming'] - del di['media'] - for _ in range(self._setting.maxRetryCount + 1): - if not self._setting.downloadMediaFile or self._setting.sendFileURLScheme: - re = self._request('sendVideo', 'post', json=di) - else: - re = self._request( - 'sendVideo', 'post', json=di, files=di3) - if re is not None and 'ok' in re and re['ok']: - break - sleep(5) - elif di['media'][0]['type'] == 'document': - if not self._setting.downloadMediaFile or self._setting.sendFileURLScheme: - di['document'] = di['media'][0]['media'] - else: - mekey = di['media'][0]['media'][9:] - di3['document'] = di3[mekey] - del di3[mekey] - del di['media'] - for _ in range(self._setting.maxRetryCount + 1): - if not self._setting.downloadMediaFile or self._setting.sendFileURLScheme: - re = self._request('sendDocument', 'post', json=di) - else: - re = self._request( - 'sendDocument', 'post', json=di, files=di3) - if re is not None and 'ok' in re and re['ok']: - break - sleep(5) + if len(di['media']) > 0: + send_file_in_list() if len(text) > 0: di = {} di['chat_id'] = chatId