def send_image(self, update: Update, image: InputMediaPhoto, queue: MessageQueue): file = None message = update.message if os.path.isfile(image.media): file = open(image.media, mode='rb') sent_media = None if image.media.endswith(('.png', '.jpg')): sent_media = message.reply_photo( photo=file or image.media, caption=image.caption, disable_notification=True, reply_to_message_id=message.message_id, ) if file: file.seek(0) message.chat.send_document( document=file or image.media, disable_notification=True, caption=image.caption, reply_to_message_id=sent_media.message_id if sent_media else None, ) queue.report()
def send_group(self, bot: Bot, update: Update, group: Iterable[InputMediaPhoto], queue: MessageQueue): for item in group: if os.path.isfile(item.media): with open(item.media, 'rb') as file_: item.media = InputFile(file_, attach=True) message = update.message bot.send_media_group(chat_id=message.chat_id, media=group, reply_to_message_id=message.message_id, disable_notification=True) for image in group: queue.report()
def moebooru_real_search(self, bot: Bot, update: Update, service: MoebooruService, query: dict, group_size: bool = False, zip_it: bool = False): message = update.message posts = service.client.post_list(**query) if not posts: message.reply_text('Nothing found on page {page}'.format(**query)) return progress_bar = TelegramProgressBar( bot=bot, chat_id=message.chat_id, pre_message=('Gathering' if group_size or zip_it else 'Sending') + ' files\n{current} / {total}', se_message='This could take some time.') message_queue = MessageQueue(total=len(posts), message=message, group_size=group_size) group = [] parsed_posts = [] for index, post_dict in progress_bar.enumerate(posts): post = self.moebooru_get_image(post=post_dict, service=service, download=zip_it) parsed_posts.append(post) if zip_it: continue if group_size: if post.is_video(): message_queue.report( PostError(code=PostError.WRONG_FILE_TYPE, post=post)) continue if index and index % group_size == 0: self.send_group(group=group, bot=bot, update=update, queue=message_queue) group = [] group.append(post.telegram) continue else: self.send_image(update=update, image=post.telegram, queue=message_queue) if zip_it: self.send_zip(update=update, posts=parsed_posts) return if group: self.send_group(group=group, bot=bot, update=update, queue=message_queue)
def danbooru_real_search(self, bot: Bot, update: Update, service: DanbooruService, query: dict, group_size: bool = False, zip_it: bool = False): """Send Danbooru API Service queried images to user Args: bot (:obj:`telegram.bot.Bot`): Telegram Api Bot Object. update (:obj:`telegram.update.Update`): Telegram Api Update Object query (:obj:`dict`): Query with keywords for post_list see: https://pybooru.readthedocs.io/en/stable/api_danbooru.html#pybooru.api_danbooru.DanbooruApi_Mixin.post_list group_size (:obj:`bool`): If the found items shall be grouped to a media group """ message = update.message posts = service.client.post_list(**query) if not posts: message.reply_text('Nothing found on page {page}'.format(**query)) return progress_bar = TelegramProgressBar( bot=bot, chat_id=message.chat_id, pre_message=('Gathering' if group_size or zip_it else 'Sending') + ' files\n{current} / {total}', se_message='This could take some time.') message_queue = MessageQueue(total=len(posts), message=message, group_size=group_size) parsed_posts = [] group = [] for index, post_dict in progress_bar.enumerate(posts): try: post = self.danbooru_get_image(post=post_dict, service=service) parsed_posts.append(post) except PostError as error: message_queue.report(error) continue if zip_it: continue if group_size: if post.is_video(): message_queue.report( PostError(code=PostError.WRONG_FILE_TYPE, post=post)) continue if index and index % group_size == 0: self.send_group(group=group, bot=bot, update=update, queue=message_queue) group = [] group.append(post.telegram) continue bot.send_chat_action(chat_id=message.chat_id, action=ChatAction.UPLOAD_PHOTO) self.send_image(update=update, image=post.telegram, queue=message_queue) if zip_it: self.send_zip(update=update, posts=parsed_posts) return if group: self.send_group(group=group, bot=bot, update=update, queue=message_queue)