Skip to content

Commit

Permalink
Missing file
Browse files Browse the repository at this point in the history
  • Loading branch information
lalinsky committed Feb 21, 2024
1 parent 27add8f commit e7d20f7
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions acoustid/fpstore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from dataclasses import dataclass
from typing import List

import requests

from acoustid.config import FpstoreConfig


@dataclass(frozen=True)
class FpstoreSearchResult:
fingerprint_id: int
score: float


class FpstoreClient:
def __init__(self, cfg: FpstoreConfig) -> None:
self.base_url = f"http://{cfg.host}:{cfg.port}/v1/fingerprint"
self.session = requests.Session()

def __enter__(self) -> "FpstoreClient":
return self

def __exit__(self, exc_type, exc_value, traceback) -> None:
self.close()

def close(self) -> None:
self.session.close()

def search(
self, query: List[int], limit: int = 10, fast_mode: bool = True
) -> List[FpstoreSearchResult]:
url = f"{self.base_url}/_search"
request_body = {
"fingerprint": {
"version": 1,
"hashes": query,
},
"limit": limit,
"fast_mode": fast_mode,
}

response = self.session.get(url, json=request_body)
response.raise_for_status()

results = []
for result in response.json()["results"]:
results.append(
FpstoreSearchResult(
fingerprint_id=result["id"],
score=result["score"],
)
)
return results

0 comments on commit e7d20f7

Please sign in to comment.