def send_files(self, entity, files, delete_on_success=False): for file in files: bar = click.progressbar(label='Uploading {}'.format( os.path.basename(file)), length=os.path.getsize(file)) def progress(current, total): bar.pos = 0 bar.update(current) name = '.'.join(os.path.basename(file).split('.')[:-1]) thumb = get_file_thumb(file) try: self.send_file( entity, file, thumb=thumb, caption=(name[:CAPTION_MAX_LENGTH] + '..') if len(name) > CAPTION_MAX_LENGTH else name, progress_callback=progress, attributes=get_file_attributes(file)) except Exception: raise finally: if thumb: os.remove(thumb) click.echo() if delete_on_success: click.echo('Deleting {}'.format(file)) os.remove(file)
def send_files(self, entity, files, delete_on_success=False, print_file_id=False, force_file=False, forward=()): for file in files: progress = get_progress_bar('Uploading', os.path.basename(file), os.path.getsize(file)) name = '.'.join(os.path.basename(file).split('.')[:-1]) thumb = None try: thumb = get_file_thumb(file) except ThumbError as e: click.echo('{}'.format(e), err=True) caption = (name[:CAPTION_MAX_LENGTH] + '..') if len(name) > CAPTION_MAX_LENGTH else name try: if force_file: attributes = [DocumentAttributeFilename(file)] else: attributes = get_file_attributes(file) message = self.send_file(entity, file, thumb=thumb, caption=caption, force_document=force_file, progress_callback=progress, attributes=attributes) except Exception: raise finally: if thumb: os.remove(thumb) click.echo() if print_file_id: click.echo('Uploaded successfully "{}" (file_id {})'.format(file, pack_bot_file_id(message.media))) if delete_on_success: click.echo('Deleting {}'.format(file)) os.remove(file) self.forward_to(message, forward)
def send_files(self, entity, files, delete_on_success=False, print_file_id=False, force_file=False, forward=(), caption=None, no_thumbnail=False): for file in files: file_name = os.path.basename(file) file_size = os.path.getsize(file) progress, bar = get_progress_bar('Uploading', file_name, file_size) name = '.'.join(file_name.split('.')[:-1]) thumb = None if not no_thumbnail: try: thumb = get_file_thumb(file) except ThumbError as e: click.echo('{}'.format(e), err=True) file_caption = truncate(caption if caption is not None else name, CAPTION_MAX_LENGTH) try: if force_file: attributes = [DocumentAttributeFilename(file_name)] else: attributes = get_file_attributes(file) try: message = self.send_file(entity, file, thumb=thumb, caption=file_caption, force_document=force_file, progress_callback=progress, attributes=attributes) if hasattr(message.media, 'document' ) and file_size != message.media.document.size: raise TelegramUploadDataLoss( 'Remote document size: {} bytes (local file size: {} bytes)' .format(message.media.document.size, file_size)) finally: bar.render_finish() finally: if thumb: os.remove(thumb) if print_file_id: click.echo('Uploaded successfully "{}" (file_id {})'.format( file, pack_bot_file_id(message.media))) if delete_on_success: click.echo('Deleting "{}"'.format(file)) os.remove(file) self.forward_to(message, forward)
async def sendVid(path): atr = files.get_file_attributes(path) print(atr) d = atr[0].duration w = atr[0].w h = atr[0].h thumb = files.get_file_thumb(path) caption = os.path.basename(path) print(atr[0]) await client.send_video(username, path, caption=caption, duration=d, width=w, height=h, thumb=thumb, supports_streaming=True)