Skip to content
This repository has been archived by the owner on Aug 4, 2023. It is now read-only.

Commit

Permalink
Add trailing slash to Jamendo thumbnail URLs (#1038)
Browse files Browse the repository at this point in the history
* Add trailing slash to Jamendo thumbnail URLs

* Apply URL fix to all thumbnail URLs
  • Loading branch information
AetherUnbound authored Mar 10, 2023
1 parent ebb2e52 commit 261538c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
21 changes: 19 additions & 2 deletions openverse_catalog/dags/providers/provider_api_scripts/jamendo.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,24 @@ def _remove_trackid(self, thumbnail_url: str | None) -> str | None:
"""
if thumbnail_url is None:
return None
return self._remove_param_from_url(thumbnail_url, "trackid")
return self._remove_param_from_url(
self._add_trailing_slash(thumbnail_url), "trackid"
)

@staticmethod
def _add_trailing_slash(url: str | None) -> str | None:
"""
Jamendo image URLs are missing a trailing slash, which when viewed normally in
the browser get redirected to the correct URL. Example:
- https://usercontent.jamendo.com?type=album&id=100007&width=300 (before)
- https://usercontent.jamendo.com/?type=album&id=100007&width=300 (after)
Due to the way photon processes thumbnails, we need to add this trailing slash
to the url prior to the query params if it does not have one.
"""
if url and "/?" not in url:
url = url.replace("?", "/?")
return url

def _get_audio_url(self, data):
"""
Expand Down Expand Up @@ -178,7 +195,7 @@ def get_record_data(self, data):
if duration:
duration = int(duration) * 1000
title = data.get("name")
thumbnail = data.get("image")
thumbnail = self._add_trailing_slash(data.get("image"))
genres = data.get("musicinfo", {}).get("tags", {}).get("genres")
creator, creator_url = self._get_creator_data(data)
metadata = self._get_metadata(data)
Expand Down
24 changes: 22 additions & 2 deletions tests/dags/providers/provider_api_scripts/test_jamendo.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ def test_get_record_data():
"raw_tags": ["instrumental", "speed_medium"],
"set_foreign_id": "119",
"set_position": 6,
"set_thumbnail": "https://usercontent.jamendo.com?type=album&id=119&width=200",
"set_thumbnail": "https://usercontent.jamendo.com/?type=album&id=119&width=200",
"set_url": "https://www.jamendo.com/album/119/opera-i",
"thumbnail_url": "https://usercontent.jamendo.com?type=album&id=119&width=200&trackid=732",
"thumbnail_url": "https://usercontent.jamendo.com/?type=album&id=119&width=200&trackid=732",
"title": "Thoughtful",
}
assert actual == expected
Expand Down Expand Up @@ -152,3 +152,23 @@ def test_get_tags():
expected_tags = ["vocal", "male", "speed_medium", "engage"]
actual_tags = jamendo._get_tags(item_data)
assert expected_tags == actual_tags


@pytest.mark.parametrize(
"url, expected",
[
(None, None),
("", ""),
(
"https://usercontent.jamendo.com?type=album&id=100007&width=300",
"https://usercontent.jamendo.com/?type=album&id=100007&width=300",
),
(
"https://usercontent.jamendo.com/some-other-page/subpage?type=album&id=100007&width=300",
"https://usercontent.jamendo.com/some-other-page/subpage/?type=album&id=100007&width=300",
),
],
)
def test_add_trailing_slash(url, expected):
actual = jamendo._add_trailing_slash(url)
assert actual == expected

0 comments on commit 261538c

Please sign in to comment.