def test_should_include_context(self): results = Search().expression( "tags:{0}".format(UNIQUE_TAG)).with_field('context').execute() self.assertEqual(len(results['resources']), 3) for res in results['resources']: self.assertEqual([key for key in iterkeys(res['context'])], [u'stage'])
def test_should_paginate_resources_limited_by_tag_and_ordered_by_ascending_public_id(self): results = Search().max_results(1).expression("tags:{0}".format(UNIQUE_TAG)).sort_by('public_id', 'asc').execute() results = {'next_cursor': ''} for i in range(0, 3): # get one resource at a time results = Search()\ .max_results(1)\ .expression("tags:{0}".format(UNIQUE_TAG))\ .sort_by('public_id', 'asc')\ .next_cursor(results['next_cursor'])\ .execute() self.assertEqual(len(results['resources']), 1) self.assertEqual(results['resources'][0]['public_id'], public_ids[i], "{0} found public_id {1} instead of {2} ".format(i, results['resources'][0]['public_id'], public_ids[i])) self.assertEqual(results['total_count'], 3)
def query_cld_folder(folder): next_cursor = None items = {} while True: search_expr = "{}/*".format(folder) if expression: search_expr = "{0} AND {1}".format(search_expr, expression) res = Search().expression(search_expr).next_cursor(next_cursor).with_field( "image_analysis").max_results(500).execute() for item in res['resources']: items[item['public_id'][len(folder) + 1:]] = {"etag": item['image_analysis']['etag'], "resource_type": item['resource_type'], "public_id": item['public_id'], "type": item['type'], "format": item['format']} if 'next_cursor' not in res.keys(): break else: next_cursor = res['next_cursor'] return items
def test_should_add_sort_by_as_dict(self): query = Search().sort_by('created_at', 'asc').sort_by('updated_at', 'desc').as_dict() self.assertEqual( query, {"sort_by": [{ 'created_at': 'asc' }, { 'updated_at': 'desc' }]})
def query_cld_folder(folder): files = {} next_cursor = True expression = Search().expression(f"folder:{folder}/*").with_field( "image_analysis").max_results(500) while next_cursor: res = expression.execute() for asset in res['resources']: rel_path = path.relpath(asset['public_id'], folder) files[rel_path] = { "type": asset['type'], "resource_type": asset['resource_type'], "public_id": asset['public_id'], "format": asset['format'], "etag": asset.get('etag', '0'), "relative_path": rel_path, # save for inner use } # use := when switch to python 3.8 next_cursor = res.get('next_cursor') expression.next_cursor(next_cursor) return files
from cloudinary import Search import urllib.request cloudName = 'dth0ow8ry' api_key = 293352914274159 api_secret = 'wLi2XaqNePSx2f9jAFDX39vqTzg' target_directory = '/Users/Richard/Documents/UCI/HackUCI2020/unknown/' search = Search() search.expression('face') faces = search.execute(cloud_name=cloudName, api_key=api_key, api_secret=api_secret) for i, item in enumerate(faces['resources']): urllib.request.urlretrieve(item['url'], f"{target_directory}{item['filename']}{i}.{item['format']}") print('Faces downloaded')
def test_should_add_with_field_as_dict(self): query = Search().with_field('context').with_field('tags').as_dict() self.assertEqual(query, {"with_field": ["context", "tags"]})
def test_should_add_aggregations_arguments_as_array_as_dict(self): query = Search().aggregate('format').aggregate( 'size_category').as_dict() self.assertEqual(query, {"aggregate": ["format", "size_category"]})
def test_should_add_next_cursor_as_dict(self): cursor = 'ec471a97ba510904ab57460b3ba3150ec29b6f8563eb1c10f6925ed0c6813f33cfa62ec6cf5ad96be6d6fa3ac3a76ccb' query = Search().next_cursor(cursor).as_dict() self.assertEqual(query, {"next_cursor": cursor})
def test_should_add_max_results_as_dict(self): query = Search().max_results('10').as_dict() self.assertEqual(query, {"max_results": '10'})
def test_should_add_expression_as_dict(self): query = Search().expression('format:jpg').as_dict() self.assertEqual(query, {"expression": 'format:jpg'})
def test_should_create_empty_json(self): query_hash = Search().as_dict() self.assertEqual(query_hash, {})
def test_should_return_resource(self): results = Search().expression("public_id:{0}".format( public_ids[0])).execute() self.assertEqual(len(results['resources']), 1)
def test_should_return_all_images_tagged(self): results = Search().expression("tags:{0}".format(UNIQUE_TAG)).execute() self.assertEqual(len(results['resources']), 3)