Slice albums larger than 10 items and allow mixing docs

This commit is contained in:
Lonami Exo 2018-03-15 10:17:45 +01:00
parent d7ef0f5e09
commit 3b42bc9991

View File

@ -1342,6 +1342,10 @@ class TelegramClient(TelegramBareClient):
photo or similar) so that it can be resent without the need photo or similar) so that it can be resent without the need
to download and re-upload it again. to download and re-upload it again.
If a list or similar is provided, the files in it will be
sent as an album in the order in which they appear, sliced
in chunks of 10 if more than 10 are given.
caption (:obj:`str`, optional): caption (:obj:`str`, optional):
Optional caption for the sent media message. Optional caption for the sent media message.
@ -1387,23 +1391,33 @@ class TelegramClient(TelegramBareClient):
# First check if the user passed an iterable, in which case # First check if the user passed an iterable, in which case
# we may want to send as an album if all are photo files. # we may want to send as an album if all are photo files.
if utils.is_list_like(file): if utils.is_list_like(file):
# Convert to tuple so we can iterate several times # TODO Fix progress_callback
file = tuple(x for x in file) images = []
if all(utils.is_image(x) for x in file): documents = []
return self._send_album( for x in file:
entity, file, caption=caption, if utils.is_image(x):
images.append(x)
else:
documents.append(x)
result = []
while images:
result += self._send_album(
entity, images[:10], caption=caption,
progress_callback=progress_callback, reply_to=reply_to, progress_callback=progress_callback, reply_to=reply_to,
parse_mode=parse_mode parse_mode=parse_mode
) )
# Not all are images, so send all the files one by one images = images[10:]
return [
result.extend(
self.send_file( self.send_file(
entity, x, allow_cache=False, entity, x, allow_cache=False,
caption=caption, force_document=force_document, caption=caption, force_document=force_document,
progress_callback=progress_callback, reply_to=reply_to, progress_callback=progress_callback, reply_to=reply_to,
attributes=attributes, thumb=thumb, **kwargs attributes=attributes, thumb=thumb, **kwargs
) for x in file ) for x in documents
] )
return result
entity = self.get_input_entity(entity) entity = self.get_input_entity(entity)
reply_to = self._get_message_id(reply_to) reply_to = self._get_message_id(reply_to)
@ -1543,6 +1557,10 @@ class TelegramClient(TelegramBareClient):
# we need to produce right now to send albums (uploadMedia), and # we need to produce right now to send albums (uploadMedia), and
# cache only makes a difference for documents where the user may # cache only makes a difference for documents where the user may
# want the attributes used on them to change. # want the attributes used on them to change.
#
# In theory documents can be sent inside the albums but they appear
# as different messages (not inside the album), and the logic to set
# the attributes/avoid cache is already written in .send_file().
entity = self.get_input_entity(entity) entity = self.get_input_entity(entity)
if not utils.is_list_like(caption): if not utils.is_list_like(caption):
caption = (caption,) caption = (caption,)