Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Handle Freesound 404s when fetching audio sets (#928)
Staci Mullins9a7892236e1f
diff --git a/DAGs.md b/DAGs.mdindex 2905ca0a3..03547bea9 100644--- a/DAGs.md+++ b/DAGs.md@@ -261,8 +261,8 @@ ETL Process: Use the API to identify all CC-licensed images.Output: TSV file containing the image, the respective meta-data.-Notes: https://freesound.org/apiv2/search/text' No rate limit specified. This-script can be run either to ingest the full dataset or as a dated DAG.+Notes: https://freesound.org/docs/api/ Rate limit: No limit for our API key.+This script can be run either to ingest the full dataset or as a dated DAG.## `image_data_refresh`diff --git a/openverse_catalog/dags/providers/provider_api_scripts/freesound.py b/openverse_catalog/dags/providers/provider_api_scripts/freesound.pyindex 058306996..abf49993b 100644--- a/openverse_catalog/dags/providers/provider_api_scripts/freesound.py+++ b/openverse_catalog/dags/providers/provider_api_scripts/freesound.py@@ -6,8 +6,8 @@ ETL Process: Use the API to identify all CC-licensed images.Output: TSV file containing the image, the respectivemeta-data.-Notes: https://freesound.org/apiv2/search/text'- No rate limit specified.+Notes: https://freesound.org/docs/api/+ Rate limit: No limit for our API key.This script can be run either to ingest the full dataset oras a dated DAG."""@@ -20,6 +20,7 @@ from airflow.models import Variablefrom common import constantsfrom common.licenses.licenses import get_license_infofrom common.loader import provider_details as prov+from common.requester import RetriesExceededfrom providers.provider_api_scripts.provider_data_ingester import ProviderDataIngesterfrom requests.exceptions import ConnectionError, SSLErrorfrom retry import retry@@ -42,13 +43,14 @@ class FreesoundDataIngester(ProviderDataIngester):"preview-lq-ogg": 80000,}- headers = {- "Accept": "application/json",- }-def __init__(self, *args, **kwargs):- super().__init__(*args, **kwargs)self.api_key = Variable.get("API_KEY_FREESOUND")+ self.headers = {+ "Accept": "application/json",+ "Authorization": f"Token {self.api_key}",+ }++ super().__init__(*args, **kwargs)def get_media_type(self, record: dict) -> str:return constants.AUDIO@@ -64,7 +66,6 @@ class FreesoundDataIngester(ProviderDataIngester):)return {"format": "json",- "token": self.api_key,"query": "","page_size": self.batch_limit,"fields": ",".join(@@ -139,13 +140,20 @@ class FreesoundDataIngester(ProviderDataIngester):@functools.lru_cache(maxsize=1024)def _get_set_info(self, set_url):- response_json = self.get_response_json(- query_params={"token": self.api_key},- endpoint=set_url,- )- set_id = response_json.get("id")- set_name = response_json.get("name")- return set_id, set_name+ try:+ response_json = self.get_response_json(+ query_params={},+ endpoint=set_url,+ )+ set_id = response_json.get("id")+ set_name = response_json.get("name")+ return set_id, set_name+ except RetriesExceeded:+ # https://github.com/WordPress/openverse-catalog/issues/659+ # This should be temporary for the full run of Freesound, as+ # some historical audio sets 404.+ logger.warning("Unable to fetch audio_set information")+ return None, Nonedef _get_audio_set_info(self, media_data):# set id, set name, set urldiff --git a/tests/dags/providers/provider_api_scripts/test_freesound.py b/tests/dags/providers/provider_api_scripts/test_freesound.pyindex 0ac1b0db0..d93a698c6 100644--- a/tests/dags/providers/provider_api_scripts/test_freesound.py+++ b/tests/dags/providers/provider_api_scripts/test_freesound.py@@ -1,6 +1,6 @@import jsonfrom pathlib import Path-from unittest.mock import patch+from unittest.mock import MagicMock, patchimport pytestfrom common.licenses.licenses import LicenseInfo@@ -83,6 +83,24 @@ def test_get_items(file_size_patch):assert actual_audio_count == expected_audio_count+def test_handles_failure_to_get_set_info():+ fsd = FreesoundDataIngester()+ # Patch the sleep function so it doesn't take long+ with patch.object(fsd.delayed_requester, "get") as get_mock, patch("time.sleep"):+ error_response = MagicMock()+ error_response.status_code = 404+ get_mock.return_value = error_response++ actual_id, actual_name, actual_url = fsd._get_audio_set_info(+ {"pack": "https://freesound.org/apiv2/packs/35596/"}+ )++ assert get_mock.call_count == 4+ assert actual_url == "https://freesound.org/apiv2/packs/35596/"+ assert actual_id is None+ assert actual_name is None++def test_get_audio_files_handles_example_audio_data(audio_data, file_size_patch):actual = fsd._get_audio_files(audio_data)expected = (