def validate_array(array): """ Builds a new array with valid values for the InputMediaAudio constructor. :return: new array with valid values :rtype: dict """ assert_type_or_raise(array, dict, parameter_name="array") from pytgbot.api_types.sendable.files import InputFile data = InputMediaWithThumb.validate_array(array) data['type'] = u(array.get('type')) data['media'] = u(array.get('media')) if array.get('thumb') is None: data['thumb'] = None elif isinstance(array.get('thumb'), InputFile): data['thumb'] = InputFile.from_array(array.get('thumb')) elif isinstance(array.get('thumb'), str): data['thumb'] = u(array.get('thumb')) else: raise TypeError( 'Unknown type, must be one of InputFile, str or None.') # end ifdata['caption'] = u(array.get('caption')) if array.get('caption') is not None else None data['parse_mode'] = u(array.get('parse_mode')) if array.get( 'parse_mode') is not None else None data['duration'] = int(array.get('duration')) if array.get( 'duration') is not None else None data['performer'] = u(array.get('performer')) if array.get( 'performer') is not None else None data['title'] = u( array.get('title')) if array.get('title') is not None else None
def from_array(array): """ Deserialize a new InputMediaDocument from a given dictionary. :return: new InputMediaDocument instance. :rtype: InputMediaDocument """ if array is None or not array: return None # end if assert_type_or_raise(array, dict, parameter_name="array") from pytgbot.api_types.sendable.files import InputFile data = {} data['type'] = u(array.get('type')) data['media'] = u(array.get('media')) if array.get('thumb') is None: data['thumb'] = None elif isinstance(array.get('thumb'), InputFile): data['thumb'] = InputFile.from_array(array.get('thumb')) elif isinstance(array.get('thumb'), str): data['thumb'] = u(array.get('thumb')) else: raise TypeError( 'Unknown type, must be one of InputFile, str or None.') # end if data['caption'] = u( array.get('caption')) if array.get('caption') is not None else None data['parse_mode'] = u(array.get('parse_mode')) if array.get( 'parse_mode') is not None else None instance = InputMediaDocument(**data) instance._raw = array return instance
def prepare_file(self): """ This sets `self.file` to a fitting :class:`InputFile` or a fitting sublcass (:class:`InputFileFromDisk`, :class:`InputFileFromURL`) :return: Nothing """ if self.file_content: file_name = "file" file_suffix = ".blob" if self.file_path: file_name = os.path.basename(os.path.normpath( self.file_path)) # http://stackoverflow.com/a/3925147 file_name, file_suffix = os.path.splitext( file_name) # http://stackoverflow.com/a/541394/3423324 elif self.file_url: from urllib.parse import urlparse # http://stackoverflow.com/a/18727481/3423324 url = urlparse(self.file_url) file_name = os.path.basename(url.path) file_name, file_suffix = os.path.splitext(file_name) # end if if self.file_mime: import mimetypes file_suffix = mimetypes.guess_extension(self.file_mime) file_suffix = '.jpg' if file_suffix == '.jpe' else file_suffix # .jpe -> .jpg # end if if not file_suffix or not file_suffix.strip().lstrip("."): logger.debug("file_suffix was empty. Using '.blob'") file_suffix = ".blob" # end if file_name = "{filename}{suffix}".format(filename=file_name, suffix=file_suffix) self.file = InputFile(self.file_content, file_name=file_name, file_mime=self.file_mime) elif self.file_path: self.file = InputFileFromDisk(self.file_path, file_mime=self.file_mime) elif self.file_url: self.file = InputFileFromURL(self.file_url, file_mime=self.file_mime)