diff --git a/rssbot.py b/rssbot.py index 1f54c3d..84c3b9d 100644 --- a/rssbot.py +++ b/rssbot.py @@ -35,7 +35,7 @@ from usercheck import checkUserPermissionsInChat, UserPermissionsInChatCheckResu import sys from fileEntry import FileEntries, remove from dictdeal import json2data -from rssbotlib import loadRSSBotLib +from rssbotlib import loadRSSBotLib, AddVideoInfoResult def getMediaInfo(m: dict, config: RSSConfig = RSSConfig()) -> str: @@ -239,10 +239,27 @@ class main: fileEntry.open() di2['thumb'] = (fileEntry._fullfn, fileEntry._f) di['supports_streaming'] = True - 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=di2) + isOk = True + if self._rssbotLib is not None: + loc = self._tempFileEntries.get(content['videoList'][0]['src'])._abspath if self._setting._downloadMediaFile and self._tempFileEntries.get( + content['videoList'][0]['src']) is not None else None + addre = self._rssbotLib.addVideoInfo( + content['videoList'][0]['src'], di, loc) + if addre == AddVideoInfoResult.IsHLS: + isOk = False + del di['video'] + del di['thumb'] + del di['supports_streaming'] + di['text'] = di['caption'] + del di['caption'] + if config.disable_web_page_preview: + di['disable_web_page_preview'] = True + re = self._request('sendMessage', 'post', json=di) + if isOk: + 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=di2) else: ind = 0 if self._setting._downloadMediaFile and not self._setting._sendFileURLScheme: @@ -320,6 +337,12 @@ class main: if ind == 0: di2['caption'] = text.tostr() di2['parse_mode'] = 'HTML' + if self._rssbotLib is not None: + loc = self._tempFileEntries.get(i['src'])._abspath if self._setting._downloadMediaFile and self._tempFileEntries.get( + i['src']) is not None else None + addre = self._rssbotLib.addVideoInfo(i['src'], di2, loc) + if addre == AddVideoInfoResult.IsHLS: + continue di['media'].append(di2) ind = ind + 1 if len(di['media']) > 1: @@ -340,7 +363,8 @@ class main: 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) + re = self._request( + 'sendPhoto', 'post', json=di, files=di3) elif di['media'][0]['type'] == 'video': if not self._setting._downloadMediaFile or self._setting._sendFileURLScheme: di['video'] = di['media'][0]['media'] @@ -365,7 +389,8 @@ class main: 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) + re = self._request( + 'sendVideo', 'post', json=di, files=di3) if re is not None and 'ok' in re and re['ok']: if returnError: return True, '' @@ -419,7 +444,7 @@ class main: if self._me is None or 'ok' not in self._me or not self._me['ok']: print('无法读取机器人信息') self._me = self._me['result'] - self._rssbotLib = loadRSSBotLib(self._setting._rssbotLib) + self._rssbotLib = loadRSSBotLib(self._setting._rssbotLib, self) self._upi = None self._updateThread = updateThread(self) self._updateThread.start() diff --git a/rssbotlib.py b/rssbotlib.py index 071977a..2a97bb8 100644 --- a/rssbotlib.py +++ b/rssbotlib.py @@ -14,6 +14,14 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . from ctypes import Structure, c_bool, c_uint64, c_char_p, CDLL, c_ushort, c_uint16, c_int +from enum import Enum, unique + + +@unique +class AddVideoInfoResult(Enum): + OK = 0 + ERROR = 1 + IsHLS = 2 class BasicInfo(Structure): @@ -39,26 +47,47 @@ class BasicInfoC: class RSSBotLib: - def __init__(self, lib: CDLL): + def __init__(self, lib: CDLL, m): self._lib = lib - self._getBasicInfo = self._lib.getBasicInfo - self._getBasicInfo.restype = BasicInfo + from rssbot import main + self._main: main = m + self.__getBasicInfo = self._lib.getBasicInfo + self.__getBasicInfo.restype = BasicInfo def getBasicInfo(self, url: str) -> (bool, BasicInfoC): try: - d: BasicInfo = self._getBasicInfo(url.encode()) + d: BasicInfo = self.__getBasicInfo(url.encode()) if d.ok: return True, BasicInfoC(d) return False, None except: return False, None + def addVideoInfo(self, url: str, data: dict, loc: str = None) -> AddVideoInfoResult: + if loc is not None: + re, info = self.getBasicInfo(loc) + if not re: + re, info = self.getBasicInfo(url) + else: + re, info = self.getBasicInfo(url) + if not re: + return AddVideoInfoResult.ERROR + if info._typeName is not None and info._typeName == 'hls': + return AddVideoInfoResult.IsHLS + if info._duration is not None: + data['duration'] = max(round(info._duration / (10 ** 6)), 1) + if info._width is not None: + data['width'] = info._width + if info._height is not None: + data['height'] = info._height + return AddVideoInfoResult.OK -def loadRSSBotLib(loc: str): + +def loadRSSBotLib(loc: str, m): if loc is None: return None try: lib = CDLL(loc) - return RSSBotLib(lib) + return RSSBotLib(lib, m) except: return None