fix bug when only 1 photo/video remain in last media group

This commit is contained in:
2021-01-15 16:02:49 +08:00
parent 717be34130
commit 58844b13a9
4 changed files with 117 additions and 7 deletions

2
.gitignore vendored
View File

@@ -4,3 +4,5 @@ __pycache__/
!requirements.txt
*.json
Temp/
*.dll
*.so

View File

@@ -40,8 +40,11 @@ class settings:
self._maxRetryCount = int(d['maxRetryCount']) if 'maxRetryCount' in d and d['maxRetryCount'].isnumeric(
) and int(d['maxRetryCount']) >= 0 else 3
self._telegramBotApiServer = d['telegramBotApiServer'] if 'telegramBotApiServer' in d else 'https://api.telegram.org'
self._downloadMediaFile = bool(int(d['downloadMediaFile'])) if 'downloadMediaFile' in d and d['downloadMediaFile'].isnumeric() else False
self._sendFileURLScheme = bool(int(d['sendFileURLScheme'])) if 'sendFileURLScheme' in d and d['sendFileURLScheme'].isnumeric() else False
self._downloadMediaFile = bool(int(
d['downloadMediaFile'])) if 'downloadMediaFile' in d and d['downloadMediaFile'].isnumeric() else False
self._sendFileURLScheme = bool(int(
d['sendFileURLScheme'])) if 'sendFileURLScheme' in d and d['sendFileURLScheme'].isnumeric() else False
self._rssbotLib = d['rssbotLib'] if 'rssbotLib' in d and d['rssbotLib'] != '' else None
class commandline:

View File

@@ -35,6 +35,7 @@ from usercheck import checkUserPermissionsInChat, UserPermissionsInChatCheckResu
import sys
from fileEntry import FileEntries, remove
from dictdeal import json2data
from rssbotlib import loadRSSBotLib
def getMediaInfo(m: dict, config: RSSConfig = RSSConfig()) -> str:
@@ -321,11 +322,50 @@ class main:
di2['parse_mode'] = 'HTML'
di['media'].append(di2)
ind = ind + 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 len(di['media']) > 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 len(di['media']) == 1:
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']
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)
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']
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']:
if returnError:
return True, ''
@@ -379,6 +419,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._upi = None
self._updateThread = updateThread(self)
self._updateThread.start()

64
rssbotlib.py Normal file
View File

@@ -0,0 +1,64 @@
# (C) 2021 lifegpc
# This file is part of rssbot.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from ctypes import Structure, c_bool, c_uint64, c_char_p, CDLL, c_ushort, c_uint16, c_int
class BasicInfo(Structure):
_fields_ = [("ok", c_bool), ("duration", c_uint64), ("bit_rate", c_uint64), ("has_h264", c_bool), ("has_aac", c_bool), ("mime_type", c_char_p), ("type_long_name", c_char_p),
("type_name", c_char_p), ("video_stream_count", c_uint16), ("audio_stream_count", c_uint16), ("subtitle_stream_count", c_uint16), ("width", c_int), ("height", c_int)]
class BasicInfoC:
def __init__(self, data: BasicInfo):
self._duration = data.duration if data.duration > 0 else None
self._bitRate = data.bit_rate if data.bit_rate > 0 else None
self._hasH264 = data.has_h264
self._hasAAC = data.has_aac
self._mimeType = data.mime_type.decode() if data.mime_type is not None else None
self._typeLongName = data.type_long_name.decode(
) if data.type_long_name is not None else None
self._typeName = data.type_name.decode() if data.type_name is not None else None
self._videoStreamCount = data.video_stream_count
self._audioStreamCount = data.audio_stream_count
self._subtitleStreamCount = data.subtitle_stream_count
self._width = data.width if data.width > 0 else None
self._height = data.height if data.height > 0 else None
class RSSBotLib:
def __init__(self, lib: CDLL):
self._lib = lib
self._getBasicInfo = self._lib.getBasicInfo
self._getBasicInfo.restype = BasicInfo
def getBasicInfo(self, url: str) -> (bool, BasicInfoC):
try:
d: BasicInfo = self._getBasicInfo(url.encode())
if d.ok:
return True, BasicInfoC(d)
return False, None
except:
return False, None
def loadRSSBotLib(loc: str):
if loc is None:
return None
try:
lib = CDLL(loc)
return RSSBotLib(lib)
except:
return None