Skip to content

Commit

Permalink
error handling for fingerprinting and submitting (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
twynb authored Oct 11, 2023
1 parent 7da61a3 commit b528d64
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
23 changes: 18 additions & 5 deletions src/backend/modules/api_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

import modules.apis.acoustid
import modules.apis.shazam
from acoustid import FingerprintGenerationError, NoBackendError, WebServiceError
from acoustid import (
FingerprintGenerationError,
FingerprintSubmissionError,
NoBackendError,
WebServiceError,
)
from modules.audio_stream_io import read_audio_file_to_numpy
from requests import exceptions
from utils.env import get_env
Expand Down Expand Up @@ -81,10 +86,18 @@ def submit_to_services(file_name, metadata):
ACOUSTID_API_KEY = get_env("SERVICE_ACOUSTID_API_KEY")
ACOUSTID_USER_KEY = get_env("SERVICE_ACOUSTID_USER_KEY")
if ACOUSTID_API_KEY is not None and ACOUSTID_USER_KEY is not None:
if modules.apis.acoustid.submit(
file_name, metadata, ACOUSTID_API_KEY, ACOUSTID_USER_KEY
):
successful_submissions.append("AcoustID")
try:
if modules.apis.acoustid.submit(
file_name, metadata, ACOUSTID_API_KEY, ACOUSTID_USER_KEY
):
successful_submissions.append("AcoustID")
except NoBackendError as ex:
log_error(ex, "No fpcalc/chromaprint found")
except FingerprintGenerationError as ex:
log_error(ex, "AcoustID fingerprinting")
except FingerprintSubmissionError as ex:
log_error(ex, "AcoustID submission")

return successful_submissions


Expand Down
13 changes: 9 additions & 4 deletions src/backend/modules/apis/acoustid.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def create_fingerprint(song_data, samplerate):
in order to identify it using AcoustID.
As of current, this works by writing the data to a temporary file
and using the fpcalc command line tool to generate the fingerprint.
The temporary file is deleted immediately afterwards.
The temporary file is deleted immediately afterwards. It is also deleted if
an error is thrown during fingerprinting.
TODO: If it becomes feasible to build and distribute DLL versions of chromaprint
for all target platforms, this should be refactored to use that instead.
Expand All @@ -53,9 +54,13 @@ def create_fingerprint(song_data, samplerate):
save_numpy_as_audio_file(song_data, os.path.abspath(filename), "", rate=samplerate)

filename_with_path = os.path.abspath(filename + ".mp3")
fingerprint_duration, fingerprint = acoustid.fingerprint_file(
filename_with_path, force_fpcalc=True
)
try:
fingerprint_duration, fingerprint = acoustid.fingerprint_file(
filename_with_path, force_fpcalc=True
)
except Exception as e:
os.remove(filename_with_path)
raise e
os.remove(filename_with_path)
return (fingerprint_duration, fingerprint)

Expand Down

0 comments on commit b528d64

Please sign in to comment.