Skip to content

Sample Code

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

Performing a search against your Custom Database

#!/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):
    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 not err.is_retryable:
                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()
package main

import (
    "encoding/json"
    "fmt"

    pex "github.com/Pexeso/pex-sdk-go/v4"
)

const (
    clientID     = "#YOUR_CLIENT_ID_HERE"
    clientSecret = "#YOUR_CLIENT_SECRET_HERE"
    inputFile    = "/path/to/file.mp3"
)

func main() {
    // Initialize and authenticate the client
    client, err := pex.NewPrivateSearchClient(clientID, clientSecret)
    if err != nil {
        panic(err)
    }
    defer client.Close()

    // Create an audio fingerprint from input file
    ft, err := client.FingerprintFile(inputFile, pex.FingerprintTypeAudio)
    if err != nil {
        panic(err)
    }

    // Build a search request
    req := &pex.PrivateSearchRequest{
        Fingerprint: ft,
    }

    // Perform the search against your custom database
    res, err := doLookup(client, req)
    if err != nil {
        panic(err)
    }

    // Print the result
    j, err := json.MarshalIndent(res, "", "  ")
    if err != nil {
        panic(err)
    }
    fmt.Println(string(j))
}

func doLookup(client *pex.PexSearchClient, req *pex.PexSearchRequest) (*pex.PexSearchResult, error) {
  for {
    // Start the search
    fut, err := client.StartSearch(req)
    if err != nil {
      if isFatal(err) {
        return nil, err
      }

      // Retry the lookup
      continue
    }

    // Retrieve the result
    res, err := fut.Get()
    if err != nil {
      if isFatal(err) {
        return nil, err
      }

      // Retry the lookup
      continue
    }

    return res, nil
  }
}

func isFatal(err error) bool {
  pexErr, ok := err.(*pex.Error)
  if !ok {
    return true
  }

  if !pexErr.IsRetryable {
    return true
  }

  // Log the error
  fmt.Println(err)
  fmt.Println("retrying lookup...")

  // Sleep for 1s or use exponential backoff
  time.Sleep(time.Second)

  return false
}
<?php

require __DIR__ . '/vendor/autoload.php';

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

function main() {
  // Initialize and authenticate the client
  $client = new Pex\PrivateSearchClient(CLIENT_ID, CLIENT_SECRET);

  // Create an audio fingerprint from input file
  $ft = $client->fingerprintFile(INPUT_FILE, [Pex\FingerprintType::Audio]);

  // Build a search request
  $req = new Pex\PrivateSearchRequest($ft);

  // Perform the search against your custom database
  $res = doLookup($client, $req);

  // Print the result
  var_dump($res);
}

function doLookup($client, $req) {
  while (true) {
    try {
      // Start the search
      $fut = $client->startSearch($req);

      // Retrieve the result
      return $fut->get();
    } catch (Pex\Error $err) {
      // Rethrow the error if it's not retryable
      if (!$err->isRetryable()) {
        throw $err;
      }

      // Log the error
      echo $err, "\n";
      echo "retrying lookup...\n";

      // Sleep for 1s or use exponential backoff
      sleep(1);

      // Retry the lookup
      continue;
    }
  }
}

main();
const pex = require("pex-sdk");

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

async function main() {
  // Initialize and authenticate the client
  let client = new pex.PrivateSearchClient(CLIENT_ID, CLIENT_SECRET)

  // Connect to the PrivateSearch server. This operation is asynchronous and
  // returns a promise, just like most operations in this package.
  await client.connect()

  // Create an audio fingerprint from input file
  let ft = await client.fingerprintFile(INPUT_FILE, [pex.AUDIO])

  // Perform the search against your custom database
  let result = await doLookup(client, {
    fingerprint: ft,
  })

  // Pretty print the result
  console.log(JSON.stringify(result, null, 2))
}

async function doLookup(client, req) {
  while(true) {
    try {
      // Start the lookup and await the result
      return await client.startSearch(req)
    } catch(err) {
      // Rethrow the error if it's not retryable
      if (!err.isRetryable) {
        throw err
      }

      // Log the error
      console.log(err)
      console.log("retrying lookup...")

      // Sleep for 1s or use exponential backoff
      await new Promise(r => setTimeout(r, 1000))

      // Retry the lookup
      continue
    }
  }
}

main()

Ingesting an Asset into your Custom Database

#!/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()
package main

import (
    "encoding/json"
    "fmt"

    pex "github.com/Pexeso/pex-sdk-go/v4"
)

const (
    clientID     = "#YOUR_CLIENT_ID_HERE"
    clientSecret = "#YOUR_CLIENT_SECRET_HERE"
    inputFile    = "/path/to/file.mp3"
    providedID   = "PROVIDED_ID"
)

func main() {
    // Initialize and authenticate the client
    client, err := pex.NewPrivateSearchClient(clientID, clientSecret)
    if err != nil {
        panic(err)
    }
    defer client.Close()

    // Create an audio fingerprint from input file
    ft, err := client.FingerprintFile(inputFile, pex.FingerprintTypeAudio)
    if err != nil {
        panic(err)
    }

    // Ingest FT into your custom database
        if err := client.Ingest(providedID, ft); err != nil {
            panic(err)
    }
}
<?php

require __DIR__ . '/vendor/autoload.php';

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

// Initialize and authenticate the client
$client = new Pex\PrivateSearchClient(CLIENT_ID, CLIENT_SECRET);

// Create an audio fingerprint from input file
$ft = $client->fingerprintFile(INPUT_FILE, [Pex\FingerprintType::Audio]);

// Ingest FT into Custom Database
$client->ingest(PROVIDED_ID, $ft);

const pex = require("pex-sdk");

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

async function main() { try { // Initialize and authenticate the client let client = new pex.PrivateSearchClient(CLIENT_ID, CLIENT_SECRET)

// Connect to the PrivateSearch server. This operation is asynchronous and
// returns a promise, just like most operations in this package.
await client.connect()

// Create an audio fingerprint from input file
let ft = await client.fingerprintFile(INPUT_FILE, &#91;pex.AUDIO&#93;)

// Ingest Asset
await client.ingest(PROVIDED_ID, ft)

} catch (error) { console.error("An error occurred:", error); } }

main()

Archiving an Asset in your Custom Database

#!/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()
package main

import (
    "encoding/json"
    "fmt"

    pex "github.com/Pexeso/pex-sdk-go/v4"
)

const (
    clientID     = "#YOUR_CLIENT_ID_HERE"
    clientSecret = "#YOUR_CLIENT_SECRET_HERE"
    providedID   = "PROVIDED_ID"
)

func main() {
    // Initialize and authenticate the client
    client, err := pex.NewPrivateSearchClient(clientID, clientSecret)
    if err != nil {
        panic(err)
    }
    defer client.Close()

    // Archive FT
        if err := client.Archive(providedID); err != nil {
            panic(err)
        }
}
<?php

require __DIR__ . '/vendor/autoload.php';

const CLIENT_ID = "#YOUR_CLIENT_ID_HERE";
const CLIENT_SECRET = "#YOUR_CLIENT_SECRET_HERE";
const PROVIDED_ID = "PROVIDED_ID";

// Initialize and authenticate the client
$client = new Pex\PrivateSearchClient(CLIENT_ID, CLIENT_SECRET);

// Create an audio fingerprint from input file
$ft = $client->fingerprintFile(INPUT_FILE, [Pex\FingerprintType::Audio]);

// Ingest FT into Custom Database
$client->archive(PROVIDED_ID);
const pex = require("pex-sdk");

const CLIENT_ID = "#YOUR_CLIENT_ID_HERE";
const CLIENT_SECRET = "#YOUR_CLIENT_SECRET_HERE";
const PROVIDED_ID = "PROVIDED_ID";

async function main() {
  try {
    // Initialize and authenticate the client
    let client = new pex.PrivateSearchClient(CLIENT_ID, CLIENT_SECRET)

    // Connect to the PrivateSearch server. This operation is asynchronous and
    // returns a promise, just like most operations in this package.
    await client.connect()

    // Archive Asset
    await client.archive(PROVIDED_ID)
  } catch (error) {
      console.error("An error occurred:", error);
  }
}

main()

Listing Assets in your Custom Database

#!/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()
package main

import (
    "encoding/json"
    "fmt"

    pex "github.com/Pexeso/pex-sdk-go/v4"
)

const (
    clientID     = "#YOUR_CLIENT_ID_HERE"
    clientSecret = "#YOUR_CLIENT_SECRET_HERE"
)

func main() {
    // Initialize and authenticate the client
    client, err := pex.NewPrivateSearchClient(clientID, clientSecret)
    if err != nil {
        panic(err)
    }
    defer client.Close()

    // List FTs
    lister := client.ListEntries(&pex.ListEntriesRequest{})
        for lister.HasNextPage {
            entries, err := lister.List()
            if err != nil {
                    panic(err)
            }
            for _, entry := range entries {
                    fmt.Println(entry)
            }
        }
}
<?php

require __DIR__ . '/vendor/autoload.php';

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

// Initialize and authenticate the client
$client = new Pex\PrivateSearchClient(CLIENT_ID, CLIENT_SECRET);

// Create an audio fingerprint from input file
$ft = $client->fingerprintFile(INPUT_FILE, [Pex\FingerprintType::Audio]);

// List Assets
$lister = $client->listEntries(new Pex\ListEntriesRequest());
while ($lister->hasNextPage()) {
    $entries = $lister->list();
    foreach ($entries as $entry) {
        echo json_encode($entry, JSON_PRETTY_PRINT) . PHP_EOL;
    }
}
const pex = require("pex-sdk");

const CLIENT_ID = "#YOUR_CLIENT_ID_HERE";
const CLIENT_SECRET = "#YOUR_CLIENT_SECRET_HERE";

async function main() {
  try {
    // Initialize and authenticate the client
    let client = new pex.PrivateSearchClient(CLIENT_ID, CLIENT_SECRET)

    // Connect to the PrivateSearch server. This operation is asynchronous and
    // returns a promise, just like most operations in this package.
    await client.connect()

    // List entries
    let lister = client.listEntries({});
    while (await lister.hasNextPage()) {
      let entries = await lister.list();
      for (const entry of entries) {
        console.log(entry);
      }
    }
  } catch (error) {
      console.error("An error occurred:", error);
  }
}

main()