def download_sticker_animated_async(self, file_id, update=None, context=None): if not update or not context: return -1 # language locale = update.effective_user.language_code message = update.effective_message try: # parse update and context bot = context.bot sticker = bot.get_file(file_id) sticker_pack_name = sticker.file_path.split('/')[-1].split('.')[0] sticker_unique_id = random_string() # sticker.file_unique_id callback = download_sticker_animated_pack args = (sticker, sticker_pack_name) self.base_send_zip(sticker_pack_name, callback, args, update=update, context=context, pack_name=sticker_unique_id) except Exception as e: message.reply_text(l10n('exec_error', locale)) self.logger.error('Exception msg: %s', str(e), exc_info=True)
def download_sticker_async(self, file_id, update=None, context=None): if not update or not context: return -1 # language locale = update.effective_user.language_code # parse update and context bot = context.bot sticker = bot.get_file(file_id) sticker_name = sticker.file_path.split('/')[-1].split('.')[0] sticker_unique_id = random_string() # sticker.file_unique_id chat = update.effective_chat message = update.effective_message # send action chat.send_action('upload_document') # download sticker file_dir = os.path.join(_temp_dir, sticker_name) file_path = os.path.join(_temp_dir, sticker_name, sticker_name + '.png') if os.path.isfile(file_path): pass elif os.path.isdir(file_dir): # wait 10 times, 10 seconds for each for _ in range(10): if not os.path.isfile(file_path): time.sleep(10) else: break else: message.reply_text(l10n('zip_timeout', locale)) return -1 else: # make dir `sticker_name` os.path.isdir(file_dir) or os.makedirs(file_dir) _, file_path = download_sticker(sticker, save_dir=file_dir) # send document sticker_set_name = message.sticker.set_name callback_data = ''.join([STICKER_SET, ':', sticker_set_name]) keyboard = InlineKeyboardMarkup([[ InlineKeyboardButton(text=l10n('kb_sticker_set', locale), callback_data=callback_data), ]]) with open(file_path, 'rb') as f: chat.send_document(f, reply_markup=keyboard, filename=sticker_unique_id + '.png', reply_to_message_id=message.message_id) # count usage set_usage(context, file_path=file_path)
def download_sticker(self, file_id, save_dir=None, random_name=False): '''''' sticker = file_id if not isinstance( file_id, str) else self.bot.get_file(file_id) # use default `temp` path save_dir = save_dir or _temp_dir file_name = sticker.file_path.split('/')[-1] if random_name: file_name = random_string() + '.webp' file_path = os.path.join(save_dir, file_name) out_path = file_path.replace('webp', 'png') # download and convert sticker.download(custom_path=file_path) self.webp2png(file_path, out_path) return (file_path, out_path)
def download_gif(self, file_id, save_dir=None, random_name=False): '''''' gif = file_id if not isinstance(file_id, str) else self.bot.get_file(file_id) # use default `temp` path save_dir = save_dir or _temp_dir file_name = gif.file_path.split('/')[-1] if random_name: file_name = random_string() + '.mp4' file_path = os.path.join(save_dir, file_name) out_path = file_path.replace('mp4', 'gif') # download and convert gif.download(custom_path=file_path) self.mp42gif(file_path, out_path) return (file_path, out_path)
def download_sticker_animated(self, file_id, save_dir=None, random_name=False): '''''' sticker = file_id if not isinstance( file_id, str) else self.bot.get_file(file_id) # use default `temp` path save_dir = save_dir or _temp_dir file_name = sticker.file_path.split('/')[-1] if random_name: file_name = random_string() + '.tgs' file_path = os.path.join(save_dir, file_name) out_path_mp4 = file_path.replace('tgs', 'mp4') out_path_gif = file_path.replace('tgs', 'gif') # download and convert sticker.download(custom_path=file_path) self.tgs2mp4(file_path, out_path_mp4) self.mp42gif(out_path_mp4, out_path_gif) return (file_path, out_path_mp4, out_path_gif)