def _fingerprint_buffer(buf): """ Generate a fingerprint from a media file loaded in memory as a byte buffer. :param bytes buf: A byte buffer holding a media file. :raise: :class:`AEError` if the buffer holds invalid data. :rtype: Fingerprint """ lk = _AE_Lock.new(_lib) c_status = _AE_Status.new(_lib) c_buf = _AE_Buffer.new(_lib) c_ft = _AE_Buffer.new(_lib) _lib.AE_Buffer_Set(c_buf.get(), buf, len(buf)) _lib.AE_Fingerprint_Buffer(c_buf.get(), c_ft.get(), c_status.get()) AEError.check_status(c_status) data = _lib.AE_Buffer_GetData(c_ft.get()) size = _lib.AE_Buffer_GetSize(c_ft.get()) ft = ctypes.string_at(data, size) return Fingerprint(ft)
def _start_metadata_search(client, req): """ Starts a metadata search. This operation does not block until the search is finished, it does however perform a network operation to initiate the search on the backend service. :param MetadataSearchRequest req: search parameters. :raise: :class:`AEError` if the search couldn’t be initiated, e.g. because of network issues. :rtype: MetadataSearchFuture """ lock = _AE_Lock.new(_lib) c_status = _AE_Status.new(_lib) c_ft = _AE_Buffer.new(_lib) c_req = _AE_MetadataSearchRequest.new(_lib) c_lookup_id = ctypes.c_uint64(0) _lib.AE_Buffer_Set(c_ft.get(), req.fingerprint._ft, len(req.fingerprint._ft)) _lib.AE_MetadataSearchRequest_SetFingerprint(c_req.get(), c_ft.get(), c_status.get()) AEError.check_status(c_status) _lib.AE_MetadataSearch_Start(client._c_client.get(), c_req.get(), ctypes.byref(c_lookup_id), c_status.get()) AEError.check_status(c_status) return MetadataSearchFuture(client, c_lookup_id.value)
def mock_client(client): """ Reinitializes a client so that it talks to the MockServer rather than the AE service. :param string client: the client that's going to be reinitialized :raise: :class:`AEError` if the connection cannot be established or the provided authentication credentials are invalid. """ lock = _AE_Lock.new(_lib) c_status = _AE_Status.new(_lib) _lib.AE_Mockserver_InitClient(client._c_client.get(), None, c_status.get()) AEError.check_status(c_status)
def _fingerprint_file(path): """ Generate a fingerprint from a file stored on a disk. The parameter to the function must be a path to a valid file in supported format. :param str path: path to the media file we're trying to fingerprint. :raise: :class:`AEError` if the media file is missing or invalid. :rtype: Fingerprint """ lk = _AE_Lock.new(_lib) c_ft = _AE_Buffer.new(_lib) c_status = _AE_Status.new(_lib) _lib.AE_Fingerprint_File(path.encode(), c_ft.get(), c_status.get()) AEError.check_status(c_status) data = _lib.AE_Buffer_GetData(c_ft.get()) size = _lib.AE_Buffer_GetSize(c_ft.get()) ft = ctypes.string_at(data, size) return Fingerprint(ft)
def __init__(self, client_id, client_secret): self._c_client = None c_status_code = ctypes.c_int(0) c_status_message = ctypes.create_string_buffer(100) c_status_message_size = ctypes.sizeof(c_status_message) _lib.AE_Init(client_id.encode(), client_secret.encode(), ctypes.byref(c_status_code), c_status_message, c_status_message_size) AEError.check(c_status_code.value, c_status_message.value.decode()) lock = _AE_Lock.new(_lib) c_status = _AE_Status.new(_lib) c_client = _AE_Client.new(_lib) _lib.AE_Client_Init(c_client.get(), client_id.encode(), client_secret.encode(), c_status.get()) AEError.check_status(c_status) # TODO: if this raises, run AE_Cleanup self._c_client = c_client
def get(self): """ Blocks until the search result is ready and then returns it. :raise: :class:`AEError` if the search couldn't be performed, e.g. because of network issues. :rtype: LicenseSearchResult """ lock = _AE_Lock.new(_lib) c_status = _AE_Status.new(_lib) c_res = _AE_LicenseSearchResult.new(_lib) _lib.AE_LicenseSearch_Check(self._raw_c_client, self._lookup_id, c_res.get(), c_status.get()) AEError.check_status(c_status) c_match = _AE_LicenseSearchMatch.new(_lib) c_matches_pos = ctypes.c_int(0) c_asset = _AE_Asset.new(_lib) matches = [] while _lib.AE_LicenseSearchResult_NextMatch( c_res.get(), c_match.get(), ctypes.byref(c_matches_pos)): _lib.AE_LicenseSearchMatch_GetAsset(c_match.get(), c_asset.get()) matches.append(LicenseSearchMatch( asset=_extract_asset(c_asset), segments=_extract_license_search_segments(c_match), policies=_extract_license_search_policies(c_match))) return LicenseSearchResult( lookup_id=_lib.AE_LicenseSearchResult_GetLookupID(c_res.get()), ugc_id=_lib.AE_LicenseSearchResult_GetUGCID(c_res.get()), matches=matches)