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 @@ -314,14 +315,21 @@ 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

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

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

rakim0 marked this conversation as resolved.
Show resolved Hide resolved

class FileListItem(MetadataItem):

Expand Down
28 changes: 20 additions & 8 deletions picard/mbjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,14 +636,16 @@ def add_secondary_release_types(node, m):
def add_genres_from_node(node, obj):
if obj is None:
return
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)
config = get_config()
if config.setting['use_genres']:
rakim0 marked this conversation as resolved.
Show resolved Hide resolved
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 'user-genres' in node:
add_user_genres(node['user-genres'], obj)


def add_genres(node, obj):
Expand All @@ -656,6 +658,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
22 changes: 20 additions & 2 deletions picard/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ def _customize_metadata(self):
tm['~silence'] = '1'

if config.setting['use_genres']:
self.add_folksonomy_tags()
self.add_genres()
self._convert_folksonomy_tags_to_genre()

# Convert Unicode punctuation
Expand Down Expand Up @@ -346,9 +348,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 +374,18 @@ def _convert_folksonomy_tags_to_genre(self):
join_with=config.setting['join_genres']
)

def add_folksonomy_tags(self):
config = get_config()
tags = Counter(self._folksonomy_tags)
tags += self.album._folksonomy_tags
zas marked this conversation as resolved.
Show resolved Hide resolved
self.metadata['_folksonomy_tags'] = self._genres_to_metadata(tags, join_with=config.setting['join_genres'])
phw marked this conversation as resolved.
Show resolved Hide resolved

def add_genres(self):
config = get_config()
_genres = Counter(self._genres)
_genres += self.album._genres
self.metadata['_genres'] = self._genres_to_metadata(_genres, join_with=config.setting['join_genres'])
rakim0 marked this conversation as resolved.
Show resolved Hide resolved


class NonAlbumTrack(Track):

Expand Down
Loading