Skip to content

Instantly share code, notes, and snippets.

@barduinor
Last active February 15, 2023 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barduinor/bfcac8966d8d4433a748a6ad8d4a9275 to your computer and use it in GitHub Desktop.
Save barduinor/bfcac8966d8d4433a748a6ad8d4a9275 to your computer and use it in GitHub Desktop.
Metadata query using python and box-python-sdk
"""sample script for box metadata query"""
from boxsdk import Client, JWTAuth
from boxsdk.object.search import MetadataSearchFilter, MetadataSearchFilters
# configuration class
class Config:
"""configurations"""
SCOPE = "enterprise_877840855"
KEY = "demoMediaMetadata"
ATTRIBUTE = "duration"
AS_USER = "18622116055"
JWT_FILE = ".jwt.config.json"
def get_box_client() -> Client:
"""returns a box client impersonating the user"""
cfg = Config()
auth = JWTAuth.from_settings_file(cfg.JWT_FILE)
service_client = Client(auth)
as_user = service_client.user(user_id=cfg.AS_USER)
return service_client.as_user(as_user)
def main():
"""main function"""
cfg = Config()
# get a client inpersonating the user
client = get_box_client()
# initialize the metadata search class
mt_query = MetadataSearchFilters()
# create a metadata search filter for the specific template
mt_filter = MetadataSearchFilter(template_key=cfg.KEY, scope=cfg.SCOPE)
# add a range filter for the attribute
mt_filter.add_range_filter(field_key=cfg.ATTRIBUTE, gt_value=1000, lt_value=30000)
# add the filter to the query
mt_query.add_filter(mt_filter)
# search for items
items = client.search().query(None, limit=100, offset=0, metadata_filters=mt_query)
print("-" * 70)
print("type \t id \t\t name \t\t\t\t duration")
print("-" * 70)
for item in items:
item_metadata = item.metadata(scope=cfg.SCOPE, template=cfg.KEY).get()
print(
f"{item.type} \t {item.id} \t {item.name} \t {item_metadata.get('duration')}"
)
# Using SQL Like sintax
# create a query using parameters
md_query = "duration >= :min AND duration <= :max"
# set the parameter values
md_params = {"min": 100000, "max": 600000}
# set the order by
order_by = [{"field_key": "duration", "direction": "ASC"}]
# set the fields to return
fields = [
"type",
"id",
"name",
"metadata." + cfg.SCOPE + "." + cfg.KEY + "." + cfg.ATTRIBUTE,
]
# search for items
mt_items = client.search().metadata_query(
from_template=cfg.SCOPE + "." + cfg.KEY,
ancestor_folder_id=0, # root folder
query=md_query,
query_params=md_params,
order_by=order_by,
fields=fields,
)
print("\n\n\n")
print("-" * 70)
print("type \t id \t\t name \t\t\t\t duration")
print("-" * 70)
for mt_item in mt_items:
duration = mt_item.metadata[cfg.SCOPE][cfg.KEY][cfg.ATTRIBUTE]
print(f"{mt_item.type} \t {mt_item.id} \t {mt_item.name} \t {duration}")
if __name__ == "__main__":
main()
print("\n\n\n All done!!!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment