Skip to content

Commit

Permalink
refactor based on review
Browse files Browse the repository at this point in the history
  • Loading branch information
TypicalFence committed Apr 3, 2023
1 parent 82441c3 commit 4b8b870
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
65 changes: 36 additions & 29 deletions beets/autotag/mb.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def _preferred_alias(aliases: List):
matches = []
for a in aliases:
if a['locale'] == locale and 'primary' in a and \
a.get('type', '').lower() not in ignored_alias_types:
a.get('type', '').lower() not in ignored_alias_types:
matches.append(a)

# Skip to the next locale if we have no matches
Expand Down Expand Up @@ -520,10 +520,10 @@ def album_info(release: Dict) -> beets.autotag.hooks.AlbumInfo:


def match_album(
artist: str,
album: str,
tracks: Optional[int] = None,
extra_tags: Optional[Dict[str, Any]] = None,
artist: str,
album: str,
tracks: Optional[int] = None,
extra_tags: Optional[Dict[str, Any]] = None,
) -> Iterator[beets.autotag.hooks.AlbumInfo]:
"""Searches for a single album ("release" in MusicBrainz parlance)
and returns an iterator over AlbumInfo objects. May raise a
Expand Down Expand Up @@ -607,29 +607,33 @@ def _parse_id(s: str) -> Optional[str]:
return None


# this was defined within the function below but pep8 made me move it here
_trans_key = 'transl-tracklisting'
_is_trans = lambda r: r['type'] == _trans_key and r['direction'] == "backward"
def _is_translation(r):
_trans_key = 'transl-tracklisting'
return r['type'] == _trans_key and r['direction'] == "backward"


def _find_actual_release_from_pseudo_release(pseudo_rel: Dict)\
-> Optional[Dict]:
def _find_actual_release_from_pseudo_release(pseudo_rel: Dict) \
-> Optional[Dict]:
relations = pseudo_rel['release']["release-relation-list"]

# currently we only support trans(liter)ation's
actual_id = next(filter(_is_trans, relations), {'target': None})['target']
translations = [r for r in relations if _is_translation(r)]

actual_id = next(filter(_is_translation, relations), {'target': None})['target']

if actual_id is None:
if not translations:
return None

actual_id = translations[0]['target']

return musicbrainzngs.get_release_by_id(actual_id,
RELEASE_INCLUDES)


def _merge_pseudo_and_actual_album(
pseudo: beets.autotag.hooks.AlbumInfo,
actual: beets.autotag.hooks.AlbumInfo
) -> Optional[beets.autotag.hooks.AlbumInfo]:
) -> Optional[beets.autotag.hooks.AlbumInfo]:
"""
Merges a pseudo release with its actual release.
Expand All @@ -643,22 +647,23 @@ def _merge_pseudo_and_actual_album(
hence why we did not implement that for now.
"""
merged = pseudo.copy()
merged.update({
"media": actual.media,
"mediums": actual.mediums,
"country": actual.country,
"catalognum": actual.catalognum,
"year": actual.year,
"month": actual.month,
"day": actual.day,
"original_year": actual.original_year,
"original_month": actual.original_month,
"original_day": actual.original_day,
"label": actual.label,
"asin": actual.asin,
"style": actual.style,
"genre": actual.genre,
})
from_actual = {k: actual[k] for k in [
"media",
"mediums",
"country",
"catalognum",
"year",
"month",
"day",
"original_year",
"original_month",
"original_day",
"label",
"asin",
"style",
"genre"
]}
merged.update(from_actual)
return merged


Expand Down Expand Up @@ -689,8 +694,10 @@ def album_for_id(releaseid: str) -> Optional[beets.autotag.hooks.AlbumInfo]:
raise MusicBrainzAPIError(exc, 'get release by ID', albumid,
traceback.format_exc())

# release is potentially a pseudo release
release = album_info(res['release'])

# should be None unless we're dealing with a pseudo release
if actual_res is not None:
actual_release = album_info(actual_res['release'])
return _merge_pseudo_and_actual_album(release, actual_release)
Expand Down
2 changes: 1 addition & 1 deletion beets/ui/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def disambig_string(info):
disambig.append(info.catalognum)
if info.albumdisambig:
disambig.append(info.albumdisambig)
# pseudo releases can't be differentiated from real release otherwise
# Let the user differentiate between pseudo and actual releases.
if info.albumstatus == 'Pseudo-Release':
disambig.append(info.albumstatus)

Expand Down

0 comments on commit 4b8b870

Please sign in to comment.