Sample Code

The following is some sample code that provides examples on performing Search: Custom Database functions:

Performing a search against your Custom Database

main.py
#!/usr/bin/env python3

import json
import pex

CLIENT_ID = "#YOUR_CLIENT_ID_HERE"
CLIENT_SECRET = "#YOUR_CLIENT_SECRET_HERE"
INPUT_FILE = "/path/to/file.mp3"

def main():
    # Initialize and authenticate the client
    client = pex.PrivateSearchClient(CLIENT_ID, CLIENT_SECRET)

    # Create an audio fingerprint from input file
    ft = client.fingerprint_file(INPUT_FILE, pex.FingerprintType.AUDIO)

    # Build a search request
    req = pex.PrivateSearchRequest(fingerprint=ft)

    # Perform the search against your custom database
    result = do_lookup(client, req)

    # Print the result
    print(json.dumps(result, indent=2))
    
    
def do_lookup(client, req):
    # These errors are outside of the client's
    # control and often resolve after retrying
    retryable_errs = (
        pex.Code.DEADLINE_EXCEEDED,
        pex.Code.INTERNAL_ERROR,
        pex.Code.LOOKUP_FAILED,
        pex.Code.LOOKUP_TIMED_OUT,
    )

    while True:
        try:
            # Start the search
            future = client.start_search(req)

            # Return the result
            return future.get()

        except pex.Error as err:
            # Raise the error if it's not retryable
            if err.code not in retryable_errs:
                raise err

            # Log the error
            print(err)
            print("retrying lookup...")

            # Sleep for 1s or use exponential backoff
            time.sleep(1)

            # Retry the lookup
            continue


if __name__ == '__main__':
    main()

Ingesting an Asset into your Custom Database

main.py
#!/usr/bin/env python3

import json
import pex

CLIENT_ID = "#YOUR_CLIENT_ID_HERE"
CLIENT_SECRET = "#YOUR_CLIENT_SECRET_HERE"
INPUT_FILE = "/path/to/file.mp3"
PROVIDED_ID = "provided_id"

def main():
    # Initialize and authenticate the client
    client = pex.PrivateSearchClient(CLIENT_ID, CLIENT_SECRET)

    # Create an audio fingerprint from input file
    ft = client.fingerprint_file(INPUT_FILE, pex.FingerprintType.AUDIO)

    # Build a search request
    req = pex.PrivateSearchRequest(fingerprint=ft)

    # Ingest FT in your Custom Database
    client.ingest(PROVIDED_ID, ft)

if __name__ == '__main__':
    main()

Archiving an Asset in your Custom Database

main.py
#!/usr/bin/env python3

import json
import pex

CLIENT_ID = "#YOUR_CLIENT_ID_HERE"
CLIENT_SECRET = "#YOUR_CLIENT_SECRET_HERE"
PROVIDED_ID = "provided_id"

def main():
    # Initialize and authenticate the client
    client = pex.PrivateSearchClient(CLIENT_ID, CLIENT_SECRET)

    # Archive a FT
    client.archive(PROVIDED_ID)

if __name__ == '__main__':
    main()

Listing Assets in your Custom Database

main.py
#!/usr/bin/env python3

import json
import pex

CLIENT_ID = "#YOUR_CLIENT_ID_HERE"
CLIENT_SECRET = "#YOUR_CLIENT_SECRET_HERE"

def main():
    # Initialize and authenticate the client
    client = pex.PrivateSearchClient(CLIENT_ID, CLIENT_SECRET)

    # List FTs
    lister = client.list_entries(pex.ListEntriesRequest())
    while lister.has_next_page:
      entries = lister.list()
      for entry in entries:
        print(entry)

if __name__ == '__main__':
    main()

Last updated