Skip to content

Commit

Permalink
Matcher: Do not fetch album genres if settings say so
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthi-chaud committed Dec 29, 2024
1 parent 50647a3 commit 118484b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
15 changes: 11 additions & 4 deletions matcher/matcher/matcher/album.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def match_and_post_album(album_id: int, album_name: str):
def match_album(
album_id: int, album_name: str, artist_name: str | None, type: AlbumType
) -> tuple[ExternalMetadataDto | None, date | None, AlbumType | None, List[str]]:
need_genres = Context.get().settings.push_genres
context = Context.get()
release_date: date | None = None
genres: List[str] = []
Expand Down Expand Up @@ -110,7 +111,11 @@ def match_album(
or (not release_date and provider.has_feature(GetAlbumReleaseDateFeature))
or (not rating and provider.has_feature(GetAlbumRatingFeature))
or (not album_type and provider.has_feature(GetAlbumTypeFeature))
or (len(genres) == 0 and provider.has_feature(GetAlbumGenresFeature))
or (
need_genres
and len(genres) == 0
and provider.has_feature(GetAlbumGenresFeature)
)
)
if not is_useful:
continue
Expand All @@ -128,9 +133,11 @@ def match_album(
description = provider.get_album_description(album)
if not release_date:
release_date = provider.get_album_release_date(album)
genres = genres + [
g for g in provider.get_album_genres(album) or [] if g not in genres
]
genres = genres + (
[g for g in provider.get_album_genres(album) or [] if g not in genres]
if need_genres
else []
)
if description and release_date and rating and genres:
break
return (
Expand Down
8 changes: 5 additions & 3 deletions matcher/matcher/matcher/song.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ def match_song(
description = provider.get_song_description(song)
if not lyrics:
lyrics = provider.get_song_lyrics(song)
genres = genres + [
g for g in provider.get_song_genres(song) or [] if g not in genres
]
genres = genres + (
[g for g in provider.get_song_genres(song) or [] if g not in genres]
if need_genres
else []
)
if description and lyrics and genres:
break
return (
Expand Down
17 changes: 16 additions & 1 deletion matcher/tests/matcher/album.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from matcher.matcher.album import match_album
from matcher.providers.domain import AlbumType
from tests.matcher.common import MatcherTestUtils
from matcher.context import Context


class TestMatchAlbum(unittest.TestCase):
Expand Down Expand Up @@ -120,7 +121,9 @@ def test_get_album2(self):
self.assertEqual(allmusic.url, "https://www.allmusic.com/album/mw0004210541")

def test_get_album_no_rating(self):
[matches, date, type, genres] = match_album(1, "Aéromusical", "Superbus", AlbumType.STUDIO)
[matches, date, type, genres] = match_album(
1, "Aéromusical", "Superbus", AlbumType.STUDIO
)
# Rating
self.assertIsNone(matches.rating)
# Release date
Expand Down Expand Up @@ -149,3 +152,15 @@ def test_get_album_no_rating(self):
### Allmusic
[allmusic] = [p for p in matches.sources if "allmusic" in p.url]
self.assertEqual(allmusic.url, "https://www.allmusic.com/album/mw0000770491")

def test_get_album_ignore_genres(self):
# Setup
context = Context.get()
context.settings.push_genres = False
[matches, date, type, genres] = match_album(
1, "Confessions on a Dancefloor", "Madonna", AlbumType.STUDIO
)
# Teardown
context.settings.push_genres = True
# Genres
self.assertEqual(len(genres), 0)

0 comments on commit 118484b

Please sign in to comment.