diff --git a/fileEntry.py b/fileEntry.py index f9b1070..1409973 100644 --- a/fileEntry.py +++ b/fileEntry.py @@ -48,6 +48,7 @@ class SubFileEntry: self._fileSize = getsize(self._path) self._localURI = f"file://{self._path}" if self._path[0] == '/' else f"file:///{self._path}" self._f = None + self._fullfn = basename(self._abspath) def delete(self): if not self._fileExist: diff --git a/rssbot.py b/rssbot.py index b9bb564..b87d4ca 100644 --- a/rssbot.py +++ b/rssbot.py @@ -283,12 +283,21 @@ class main: if not fileEntry.ok: 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: + is_supported_photo = self._rssbotLib.is_supported_photo(fileEntry._abspath) + if is_supported_photo is not None: + should_use_file = not is_supported_photo if self._setting.sendFileURLScheme: if not should_use_file: di['photo'] = fileEntry._localURI re = self._request('sendPhoto', 'post', json=di) else: di['document'] = fileEntry._localURI + if is_supported_photo is False: + if self._rssbotLib.convert_to_tg_thumbnail(fileEntry, 'jpeg'): + thumb_file = fileEntry.getSubFile('_thumbnail', 'jpeg') + di['thumb'] = thumb_file._localURI re = self._request('sendDocument', 'post', json=di) else: fileEntry.open() @@ -296,8 +305,13 @@ class main: re = self._request('sendPhoto', 'post', json=di, files={ 'photo': (fileEntry._fullfn, fileEntry._f)}) else: - re = self._request('sendDocument', 'post', json=di, files={ - 'document': (fileEntry._fullfn, fileEntry._f)}) + send_files = {'document': (fileEntry._fullfn, fileEntry._f)} + if is_supported_photo is False: + if self._rssbotLib.convert_to_tg_thumbnail(fileEntry, 'jpeg'): + thumb_file = fileEntry.getSubFile('_thumbnail', 'jpeg') + thumb_file.open() + send_files['thumb'] = (thumb_file._fullfn, thumb_file._f) + re = self._request('sendDocument', 'post', json=di, files=send_files) else: re = self._request('sendMessage', 'post', json=di) if re is not None and 'ok' in re and re['ok']: @@ -544,6 +558,9 @@ 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 diff --git a/rssbotlib.py b/rssbotlib.py index ee92cd1..a21b6c9 100644 --- a/rssbotlib.py +++ b/rssbotlib.py @@ -15,8 +15,9 @@ # along with this program. If not, see . from enum import unique, Enum from traceback import print_exc +from typing import Optional try: - from _rssbotlib import version, VideoInfo, convert_ugoira_to_mp4, AVDict + from _rssbotlib import version, VideoInfo, convert_ugoira_to_mp4, AVDict, convert_to_tg_thumbnail have_rssbotlib = True except ImportError: have_rssbotlib = False @@ -37,7 +38,7 @@ if have_rssbotlib: from rssbot import main self._main: main = m self._version = version() - if self._version is None or self._version != [1, 0, 0, 1]: + if self._version is None or self._version > [1, 0, 0, 2]: raise ValueError('RSSBotLib Version unknown or not supported.') def addVideoInfo(self, url: str, data: dict, loc: str = None) -> AddVideoInfoResult: @@ -95,6 +96,43 @@ if have_rssbotlib: print_exc() return False + def convert_to_tg_thumbnail(self, f: FileEntry, format: str = 'webp') -> bool: + try: + if f.getSubFile('_thumbnail', format) is not None: + return True + dst = f.getSubPath('_thumbnail', format) + if not convert_to_tg_thumbnail(f._abspath, dst, format): + return False + f.addSubFile('_thumbnail', format) + return True + except Exception: + print_exc() + try: + remove(dst) + except Exception: + print_exc() + return False + + def is_supported_photo(self, f: str) -> Optional[bool]: + try: + v = VideoInfo() + if not v.parse(f): + return None + streams = v.streams + width = None + height = None + for stream in streams: + if stream.is_video: + width = stream.width + height = stream.height + break + if width is None or height is None: + return None + return False if width + height > 10000 or width / height > 20 or height / width > 20 else True + except Exception: + print_exc() + return None + def loadRSSBotLib(m): if have_rssbotlib: