def zip(self, destination_path, files, security=None): """ Takes array of handles and downloads a compressed ZIP archive to provided path Args: destination_path (str): path where the ZIP file should be stored file (list): list of filelink handles and/or URLs security (:class:`filestack.Security`): Security object that will be used for this API call Returns: int: ZIP archive size in bytes """ url_parts = [config.CDN_URL, self.apikey, 'zip', '[{}]'.format(','.join(files))] sec = security or self.security if sec is not None: url_parts.insert(3, sec.as_url_string()) zip_url = '/'.join(url_parts) total_bytes = 0 with open(destination_path, 'wb') as f: response = requests.get(zip_url, stream=True) for chunk in response.iter_content(5 * 1024 ** 2): f.write(chunk) total_bytes += len(chunk) return total_bytes
def get_content(self, security=None): """ Returns the raw byte content of a given object Returns: `bytes`: file content """ sec = security or self.security response = requests.get(self._build_url(security=sec)) return response.content
def upload_external_url(url, apikey, store_params=None, security=None): store_task = build_store_task(store_params or {}) encoded_url = 'b64://{}'.format( base64.urlsafe_b64encode(url.encode()).decode()) url_elements = [config.CDN_URL, apikey, store_task, encoded_url] if security is not None: url_elements.insert(3, security.as_url_string()) # TODO: use processing endpoint and "store" task for uploading external urls response = requests.get('/'.join(url_elements)) return response.json()['handle']
def status(self): """ Returns the status of the AV conversion (makes a GET request) *returns* [String] ```python av_convert= filelink.av_convert(width=100, height=100) while av_convert.status != 'completed': print(av_convert.status) ``` """ return requests.get(self.url).json()['status']
def download(self, path, security=None): """ Downloads a file to the given local path and returns the size of the downloaded file if successful """ sec = security or self.security total_bytes = 0 with open(path, 'wb') as f: response = requests.get(self._build_url(security=sec), stream=True) for data_chunk in response.iter_content(5 * 1024**2): f.write(data_chunk) total_bytes += len(data_chunk) return total_bytes
def ocr(self, security=None): """ Performs OCR on current object (image) Args: security (:class:`filestack.Security`): Security object that will be used to run OCR Returns: `dict`: dictionary containing OCR data """ obj = self._add_transform_task('ocr', params={'self': None}) response = requests.get(obj.signed_url(security=security)) return response.json()
def sfw(self, security=None): """ Performs Safe for Work detection on current object (image). Args: security (:class:`filestack.Security`): Security object that will be used to perform image tagging Returns: `dict`: dictionary containing SFW result """ obj = self._add_transform_task('sfw', params={'self': None}) response = requests.get(obj.signed_url(security=security)) return response.json()
def tags(self, security=None): """ Performs image tagging operation on current object Args: security (:class:`filestack.Security`): Security object that will be used to perform image tagging Returns: `dict`: dictionary containing image tags """ obj = self._add_transform_task('tags', params={'self': None}) response = requests.get(obj.signed_url(security=security)) return response.json()
def to_filelink(self): """ Checks is the status of the conversion is complete and, if so, converts to a Filelink *returns* [Filestack.Filelink] ```python filelink = av_convert.to_filelink() ``` """ if self.status != 'completed': raise Exception('Audio/video conversion not complete!') response = requests.get(self.url).json() handle = response['data']['url'].split('/')[-1] return filestack.models.Filelink(handle, apikey=self.apikey, security=self.security)
def metadata(self, attributes_list=None, security=None): """ Retrieves filelink's metadata. Args: attributes_list (list): list of attributes that you wish to receive. When not provided, default set of parameters will be returned (may differ depending on your storage settings) security (:class:`filestack.Security`): Security object that will be used to retrieve metadata >>> filelink.metadata(['size', 'filename']) {'filename': 'envelope.jpg', 'size': 171832} Returns: `dict`: A buffered writable file descriptor """ attributes_list = attributes_list or [] params = {} for item in attributes_list: params[item] = 'true' sec = security or self.security if sec is not None: params.update({'policy': sec.policy_b64, 'signature': sec.signature}) return requests.get(self.url + '/metadata', params=params).json()