def get_text_from_image(image_file: File) -> str: """Get text from image with google vision api""" # Get temp file path with tempfile library fd, temp_file_path = tempfile.mkstemp() # Download image image_file.download(temp_file_path) # Read image file as binary with open(temp_file_path, "rb") as image_file: content = image_file.read() # Create vision.Image object to use it in document_text_detection function image = vision.Image(content=content) # Get response from google vision api # client field will be add to function in telegram_bot.py script response = Ocr.client.document_text_detection(image=image) # Get text from response text = response.full_text_annotation.text # Close file, automatic deletion will occur os.close(fd) # Return text return text
def get_file(file: telegram.File) -> BytesIO: attachment = BytesIO() attachment.name = str(uuid.uuid4()) + '.' + path.splitext(file.file_path)[1] file.download(out=attachment) attachment.seek(0, 0) return attachment
def append_photo(self, photo: File): extension_of_photo = photo.file_path[photo.file_path.rfind(".")::].lower() name_photo = f"ph{self.index_of_photo}" path = self.path_to_directory + name_photo + extension_of_photo photo.download(custom_path=path) self.photo_to_convert.append(path) self.index_of_photo += 1 if self.index_of_photo == self.amount_of_photo: return True else: return False
def download_file(self, file: File, content_type: str): custom_path = os.environ['TG_BOT_TEMP'] if self.media_group: custom_path = os.path.join(custom_path, str(self.message.chat_id), str(self.message.media_group_id)) custom_path = os.path.realpath(custom_path) os.makedirs(custom_path, exist_ok=True) lock_file = os.path.join(custom_path, '.lock') if not os.path.exists(lock_file): open(lock_file, 'wt').close() basename = file.file_path and os.path.basename( file.file_path) or EXT[content_type] file_path = os.path.join(custom_path, f'{file.file_id}_{basename}') filename = file.download(custom_path=file_path) log.debug('File downloaded to dir `%s` filename `%s`', custom_path, filename) data = { 'type': content_type, 'filename': filename, 'caption': self.caption, } if self.media_group: json_file_path = os.path.join(custom_path, MEDIA_GROUP_FILE) json_dump = [] if os.path.exists(json_file_path): with open(json_file_path, 'rt') as json_file: json_dump = json.load(json_file) with open(json_file_path, 'wt') as json_file: json_dump.append(data) json.dump(json_dump, json_file, indent=4) setattr(self, content_type, [data]) os.remove(lock_file)
def download_file(file: File) -> Text: """Request the download of a file""" file_path = os.path.join(downloads_dir, file.file_unique_id) with open(file_path, 'wb') as out: file.download(custom_path=None, out=out) return file_path
def getPhoto(bot, update): logger.info('Someone had send me photo') update.message.reply_text("Thanks") picture = bot.getFile(update.message.photo[-1]['file_id']) filename = basename(picture['file_path']) File.download(picture, CONF['BOT']['DOWNLOADFOLDER'] + filename)
def save_xls(update, context): f = File() name = f.download() return SAVING_RESUME