Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Always use Jamendo's "streaming" audio (#706)
Madison Swain-Bowdene9fe5b9648e7
diff --git a/openverse_catalog/dags/providers/provider_api_scripts/jamendo.py b/openverse_catalog/dags/providers/provider_api_scripts/jamendo.pyindex eb4e4362a..e4c76b9e9 100644--- a/openverse_catalog/dags/providers/provider_api_scripts/jamendo.py+++ b/openverse_catalog/dags/providers/provider_api_scripts/jamendo.py@@ -130,7 +130,7 @@ def _extract_audio_data(media_data):foreign_landing_url = media_data["shareurl"]except (TypeError, KeyError, AttributeError):return None- audio_url, duration, download_url = _get_audio_info(media_data)+ audio_url, duration = _get_audio_info(media_data)if audio_url is None:return Noneitem_license = _get_license(media_data)@@ -184,27 +184,42 @@ def _get_foreign_identifier(media_data):return None-def _get_audio_info(media_data):- """Parses audio URL, audio download URL, audio duration- If the audio does not allow download, we save the 'streaming'- URL as the `audio_url`+def _remove_param_from_url(url: str, param: str) -> str:+ """+ Remove a parameter from a provided URL.+ """+ parsed_url = urlsplit(url)+ query = parse_qs(parsed_url.query)+ query.pop(param, None)+ return parsed_url._replace(query=urlencode(query, doseq=True)).geturl()+++def _get_audio_info(media_data: dict) -> tuple[Optional[str], Optional[int]]:+ """Parse audio URL & audio duration+ Also remove the "from" parameter from an audio URL. Audio URLs have a "from" param+ which seems to encapsulate information about the calling application.+ Example from the API:+ https://prod-1.storage.jamendo.com/?trackid=1532771&format=mp31&from=app-devsite+ This information looks like an API key or secret when returned, so we remove it+ since it's not necessary for serving the audio files.+ >>> base_url = "https://prod-1.storage.jamendo.com/"+ >>> url = f"{base_url}?trackid=1532771&format=mp31&from=app-devsite"+ >>> _remove_param_from_url(url, "from")+ 'https://prod-1.storage.jamendo.com/?trackid=1532771&format=mp31':return: Tuple with main audio file information:- audio_url- - download_url- duration (in milliseconds)"""- audio_url = media_data.get("audio")- download_url = None- if media_data.get("audiodownload_allowed") and media_data.get("audiodownload"):- audio_url = media_data.get("audiodownload")- download_url = media_data.get("audiodownload")+ if (audio_url := media_data.get("audio")) is None:+ return None, None+ audio_url = _remove_param_from_url(audio_url, "from")duration = media_data.get("duration")if duration:duration = int(duration) * 1000- return audio_url, duration, download_url+ return audio_url, duration-def _remove_trackid(thumbnail_url: Optional[str]):+def _remove_trackid(thumbnail_url: Optional[str]) -> Optional[str]:"""``audio_set`` data is used to create a separate database table in the API.To make sure that any given ``audio_set`` appears in that table only once,@@ -221,10 +236,7 @@ def _remove_trackid(thumbnail_url: Optional[str]):"""if thumbnail_url is None:return- parsed_url = urlsplit(thumbnail_url)- query = parse_qs(parsed_url.query)- query.pop("trackid", None)- return parsed_url._replace(query=urlencode(query, doseq=True)).geturl()+ return _remove_param_from_url(thumbnail_url, "trackid")def _get_audio_set_info(media_data):diff --git a/tests/dags/providers/provider_api_scripts/test_jamendo.py b/tests/dags/providers/provider_api_scripts/test_jamendo.pyindex e11951a61..a51f96ecd 100644--- a/tests/dags/providers/provider_api_scripts/test_jamendo.py+++ b/tests/dags/providers/provider_api_scripts/test_jamendo.py@@ -24,6 +24,23 @@ def cleanse_url():yield+ "url, param, expected",+ [+ ("", "", ""),+ ("https://example.com?a=1&b=2", "a", "https://example.com?b=2"),+ ("https://example.com?a=1", "a", "https://example.com"),+ ("https://example.com/?a=1", "a", "https://example.com/"),+ ("https://example.com?a=1&a=2&b=3", "a", "https://example.com?b=3"),+ ("https://example.com?a=1&a=2", "a", "https://example.com"),+ ("https://example.com?a=1&b=2", "notexist", "https://example.com?a=1&b=2"),+ ],+)+def test_remove_param_from_url(url, param, expected):+ actual = jamendo._remove_param_from_url(url, param)+ assert actual == expected++def test_get_image_pages_returns_correctly_with_none_json():expect_result = Nonewith patch.object(@@ -71,7 +88,7 @@ def test_process_item_batch_handles_example_batch():_, actual_call_args = mock_add.call_args_list[0]expected_call_args = {"audio_set": "Opera I",- "audio_url": "https://mp3d.jamendo.com/download/track/732/mp32/",+ "audio_url": "https://mp3d.jamendo.com/?trackid=732&format=mp32","category": "music","creator": "Haeresis","creator_url": "https://www.jamendo.com/artist/92/haeresis",@@ -118,39 +135,10 @@ def test_extract_audio_data_returns_none_when_no_foreign_id():assert actual_image_info is expected_image_info-def test_extract_audio_data_falls_back_on_audio_url_when_download_not_available():- """- We use the URL from `audiodownload` only if download is allowed, and there is- download URL. Otherwise, we try to get URL from `audio_url`- """- AUDIO_D_URL = "https://mp3d.jamendo.com/download/track/732/mp32/"- AUDIO_URL = (- "https://mp3d.jamendo.com/?trackid=732&format=mp32&from="- "WftSCtIfbfXP90c1jNqsfw%3D%3D%7CNzgGctbCMB1xAW8bJE5uEw%3D%3D"- )- with open(RESOURCES / "audio_data_example.json") as f:- audio_data = json.load(f)- actual_image_info = jamendo._extract_audio_data(audio_data)["audio_url"]- expected_image_download_url = AUDIO_D_URL- assert actual_image_info == expected_image_download_url-- audio_data["audiodownload_allowed"] = False- actual_image_info = jamendo._extract_audio_data(audio_data)["audio_url"]- expected_image_download_url = AUDIO_URL- assert actual_image_info == expected_image_download_url-- audio_data["audiodownload_allowed"] = True- audio_data.pop("audiodownload", None)- actual_image_info = jamendo._extract_audio_data(audio_data)["audio_url"]- expected_image_download_url = AUDIO_URL- assert actual_image_info == expected_image_download_url--def test_extract_audio_data_returns_none_when_no_audio_url():with open(RESOURCES / "audio_data_example.json") as f:audio_data = json.load(f)audio_data.pop("audio", None)- audio_data["audiodownload_allowed"] = Falseactual_audio_info = jamendo._extract_audio_data(audio_data)assert actual_audio_info is None@@ -216,7 +204,7 @@ def test_extract_audio_data_handles_example_dict():actual_image_info = jamendo._extract_audio_data(audio_data)expected_image_info = {"audio_set": "Opera I",- "audio_url": "https://mp3d.jamendo.com/download/track/732/mp32/",+ "audio_url": "https://mp3d.jamendo.com/?trackid=732&format=mp32","category": "music","creator": "Haeresis","creator_url": "https://www.jamendo.com/artist/92/haeresis",