Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Freesound SSLError fix (#330)
Madison Swain-Bowdencadc9d3e7a25
diff --git a/openverse_catalog/dags/providers/provider_api_scripts/freesound.py b/openverse_catalog/dags/providers/provider_api_scripts/freesound.pyindex 08e130ada..c90f41234 100644--- a/openverse_catalog/dags/providers/provider_api_scripts/freesound.py+++ b/openverse_catalog/dags/providers/provider_api_scripts/freesound.py@@ -20,6 +20,8 @@ from common.licenses.licenses import get_license_infofrom common.loader import provider_details as provfrom common.requester import DelayedRequesterfrom common.storage.audio import AudioStore+from requests.exceptions import SSLError+from retry import retryLIMIT = 150@@ -165,7 +167,11 @@ def _extract_audio_data(media_data):# for playing on the frontend,# and the actual uploaded file as an alt_file that is available# for download (and requires a user to be authenticated to download)- main_audio, alt_files = _get_audio_files(media_data)+ try:+ main_audio, alt_files = _get_audio_files(media_data)+ except SSLError:+ logger.warning(f"Unable to get file size for {foreign_landing_url}, skipping")+ return Noneif main_audio is None:return None@@ -222,6 +228,16 @@ def _get_preview_filedata(preview_type, preview_url):}+@retry(SSLError, tries=3, delay=1, backoff=2)+def _get_audio_file_size(url):+ """+ Get the content length of a provided URL.+ Freesound can be a bit finicky, so we want to retry it a few times+ """+ # TODO(obulat): move filesize detection to the polite crawler+ return int(requests.head(url).headers["content-length"])++def _get_audio_files(media_data):# This is the original file, needs auth for downloading.# bit_rate in kilobytes, converted to bytes@@ -240,9 +256,7 @@ def _get_audio_files(media_data):return Nonemain_file = _get_preview_filedata("preview-hq-mp3", previews["preview-hq-mp3"])main_file["audio_url"] = main_file.pop("url")- # TODO(obulat): move filesize detection to the polite crawler- filesize = requests.head(main_file["audio_url"]).headers["content-length"]- main_file["filesize"] = int(filesize)+ main_file["filesize"] = _get_audio_file_size(main_file["audio_url"])return main_file, alt_filesdiff --git a/requirements_prod.txt b/requirements_prod.txtindex f62d92eba..7f09214b3 100644--- a/requirements_prod.txt+++ b/requirements_prod.txt@@ -3,4 +3,5 @@ lxmlpsycopg2-binaryrequests-file==1.5.1requests-oauthlib+retry==0.9.2tldextract==3.1.0diff --git a/tests/dags/providers/provider_api_scripts/test_freesound.py b/tests/dags/providers/provider_api_scripts/test_freesound.pyindex 5073d1fe4..3f65ed7b3 100644--- a/tests/dags/providers/provider_api_scripts/test_freesound.py+++ b/tests/dags/providers/provider_api_scripts/test_freesound.py@@ -5,6 +5,7 @@ from unittest.mock import patchimport pytestfrom common.licenses.licenses import LicenseInfofrom providers.provider_api_scripts import freesound+from requests.exceptions import SSLErrorRESOURCES = Path(__file__).parent.resolve() / "resources/freesound"@@ -43,6 +44,16 @@ def test_get_audio_pages_returns_correctly_with_no_results():assert actual_result == expect_result+def test_get_audio_file_size_retries_and_does_not_raise(audio_data):+ expected_result = None+ with patch("requests.head") as head_patch:+ head_patch.side_effect = SSLError("whoops")+ actual_result = freesound._extract_audio_data(audio_data)++ assert head_patch.call_count == 3+ assert actual_result == expected_result++def test_get_query_params_adds_page_number():actual_qp = freesound._get_query_params(page_number=2)assert actual_qp["page"] == str(2)