Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PICARD-2607: Add a “_genre” variable #2545

Merged
merged 13 commits into from
Jan 1, 2025
14 changes: 11 additions & 3 deletions picard/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ def __init__(self, obj_id=None):
self.iter_children_items_metadata_ignore_attrs = {}
self.suspend_metadata_images_update = IgnoreUpdatesContext()
self._genres = Counter()
self._folksonomy_tags = Counter()

@property
def tagger(self):
Expand Down Expand Up @@ -309,17 +310,24 @@ def add_genre(self, name, count):
if name:
self._genres[name] += count

@property
def folksonomy_tags(self):
return self._folksonomy_tags

def add_folksonomy_tag(self, name, count):
if name:
self._folksonomy_tags[name] += count

@staticmethod
def set_genre_inc_params(inc, config=None):
require_authentication = False
config = config or get_config()
if config.setting['use_genres']:
use_folksonomy = config.setting['folksonomy_tags']
if config.setting['only_my_genres']:
require_authentication = True
inc |= {'user-tags'} if use_folksonomy else {'user-genres'}
inc |= {'user-tags', 'user-genres'}
else:
inc |= {'tags'} if use_folksonomy else {'genres'}
inc |= {'tags', 'genres'}
return require_authentication


Expand Down
18 changes: 14 additions & 4 deletions picard/mbjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,14 +636,14 @@ def add_secondary_release_types(node, m):
def add_genres_from_node(node, obj):
if obj is None:
return
if 'tags' in node:
add_tags(node['tags'], obj)
if 'user-tags' in node:
add_user_tags(node['user-tags'], obj)
if 'genres' in node:
add_genres(node['genres'], obj)
if 'tags' in node:
add_genres(node['tags'], obj)
if 'user-genres' in node:
add_user_genres(node['user-genres'], obj)
if 'user-tags' in node:
add_user_genres(node['user-tags'], obj)


def add_genres(node, obj):
Expand All @@ -656,6 +656,16 @@ def add_user_genres(node, obj):
obj.add_genre(tag['name'], 1)


def add_tags(node, obj):
for tag in node:
obj.add_folksonomy_tag(tag['name'], tag['count'])


def add_user_tags(node, obj):
for tag in node:
obj.add_folksonomy_tag(tag['name'], 1)


def add_isrcs_to_metadata(node, metadata):
for isrc in node:
metadata.add('isrc', isrc)
Expand Down
28 changes: 26 additions & 2 deletions picard/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ def _customize_metadata(self):

if config.setting['use_genres']:
self._convert_folksonomy_tags_to_genre()
self._delete_genres_from_tags()
self._add_folksonomy_tags()
self._add_genres()

# Convert Unicode punctuation
if config.setting['convert_punctuation']:
Expand Down Expand Up @@ -346,9 +349,13 @@ def _genres_to_metadata(genres, limit=None, minusage=0, filters='', join_with=No

def _convert_folksonomy_tags_to_genre(self):
config = get_config()
# Combine release and track genres
genres = Counter(self.genres)
genres += self.album.genres
use_folksonomy = config.setting['folksonomy_tags']
if use_folksonomy:
genres += self.album._folksonomy_tags
zas marked this conversation as resolved.
Show resolved Hide resolved
else:
genres += self.album._genres
zas marked this conversation as resolved.
Show resolved Hide resolved
# Combine release and track genres
if self.album.release_group:
genres += self.album.release_group.genres
if not genres and config.setting['artists_genres']:
Expand All @@ -368,6 +375,23 @@ def _convert_folksonomy_tags_to_genre(self):
join_with=config.setting['join_genres']
)

def _add_tags(self, tags, name):
self.metadata[name] = tags.keys()

def _add_folksonomy_tags(self):
tags = Counter(self._folksonomy_tags)
tags += self.album._folksonomy_tags
zas marked this conversation as resolved.
Show resolved Hide resolved
self._add_tags(tags, '_folksonomy_tags')

def _add_genres(self):
genre = Counter(self._genres)
genre += self.album._genres
zas marked this conversation as resolved.
Show resolved Hide resolved
self._add_tags(genre, '_genres')

def _delete_genres_from_tags(self):
zas marked this conversation as resolved.
Show resolved Hide resolved
for genre in self.album.genres:
del self.album._folksonomy_tags[genre]


class NonAlbumTrack(Track):

Expand Down
28 changes: 17 additions & 11 deletions test/test_mbjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,14 @@ def test_release(self):
self.assertEqual(m['~release_seriesid'], '7421b602-a413-4151-bcf4-d831debc3f27')
self.assertEqual(m['~release_seriescomment'], 'Pink Floyed special editions')
self.assertEqual(m['~release_seriesnumber'], '')
self.assertEqual(a.genres, {
'genre1': 6, 'genre2': 3,
'tag1': 6, 'tag2': 3})
self.assertEqual(a._genres, {
zas marked this conversation as resolved.
Show resolved Hide resolved
'genre1': 6, 'genre2': 3
})
self.assertEqual(a._folksonomy_tags, {
zas marked this conversation as resolved.
Show resolved Hide resolved
'tag1': 6, 'tag2': 3
})
for artist in a._album_artists:
self.assertEqual(artist.genres, {
self.assertEqual(artist._folksonomy_tags, {
zas marked this conversation as resolved.
Show resolved Hide resolved
'british': 2,
'progressive rock': 10})

Expand All @@ -166,11 +169,14 @@ def test_release_without_release_relationships(self):
self.assertEqual(m['~albumartists_sort'], 'Pink Floyd')
self.assertEqual(m['~releaselanguage'], 'eng')
self.assertEqual(m.getall('~releasecountries'), ['GB', 'NZ'])
self.assertEqual(a.genres, {
'genre1': 6, 'genre2': 3,
'tag1': 6, 'tag2': 3})
self.assertEqual(a._genres, {
zas marked this conversation as resolved.
Show resolved Hide resolved
'genre1': 6, 'genre2': 3
})
self.assertEqual(a._folksonomy_tags, {
zas marked this conversation as resolved.
Show resolved Hide resolved
'tag1': 6, 'tag2': 3
})
for artist in a._album_artists:
self.assertEqual(artist.genres, {
self.assertEqual(artist._folksonomy_tags, {
zas marked this conversation as resolved.
Show resolved Hide resolved
'british': 2,
'progressive rock': 10})

Expand Down Expand Up @@ -293,11 +299,11 @@ def test_recording(self):
self.assertEqual(m['~video'], '')
self.assertNotIn('originaldate', m)
self.assertNotIn('originalyear', m)
self.assertEqual(t.genres, {
self.assertEqual(t._folksonomy_tags, {
zas marked this conversation as resolved.
Show resolved Hide resolved
'blue-eyed soul': 1,
'pop': 3})
for artist in t._track_artists:
self.assertEqual(artist.genres, {
self.assertEqual(artist._folksonomy_tags, {
zas marked this conversation as resolved.
Show resolved Hide resolved
'dance-pop': 1,
'guitarist': 0})

Expand Down Expand Up @@ -770,7 +776,7 @@ def test_release_group(self):
self.assertEqual(m['releasetype'], 'album')
self.assertEqual(m['~primaryreleasetype'], 'album')
self.assertEqual(m['~releasegroup'], 'The Dark Side of the Moon')
self.assertEqual(r.genres, {'test2': 3, 'test': 6})
self.assertEqual(r._folksonomy_tags, {'test2': 3, 'test': 6})
zas marked this conversation as resolved.
Show resolved Hide resolved


class NullReleaseGroupTest(MBJSONTest):
Expand Down
Loading