def combine_tracks(base, meta): """Combine the metadata for the tracks of two different sources.""" btracks = iter(chain.from_iterable([d.values() for d in base.values()])) for disc, tracks in meta.items(): for num, track in tracks.items(): try: btrack = next(btracks) except StopIteration: raise TrackCombineError( f"Disc {disc} track {num} does not exist.") if re_strip(track["title"]) != re_strip(btrack["title"]): continue base_artists = {(re_strip(a[0]), a[1]) for a in btrack["artists"]} btrack["artists"] = list(btrack["artists"]) for a in track["artists"]: if (re_strip(a[0]), a[1]) not in base_artists: btrack["artists"].append(a) btrack["artists"] = check_for_artist_fragments(btrack["artists"]) if track["explicit"]: btrack["explicit"] = True if not btrack["format"]: btrack["format"] = track["format"] if not btrack["isrc"]: btrack["isrc"] = track["isrc"] if not btrack["replay_gain"]: btrack["replay_gain"] = track["replay_gain"] btrack["title"] = track["title"] if track["tracktotal"] and track["disctotal"]: btrack["tracktotal"] = track["tracktotal"] btrack["disctotal"] = track["disctotal"] base[btrack["disc#"]][btrack["track#"]] = btrack return base
def _compare_albums(one, two): one, two = normalize_accents(one, two) if re_strip(one) == re_strip(two): return True elif re_strip( re.sub(r" \(?(mix|feat|with|incl|prod).+", "", one, flags=re.IGNORECASE)) == re_strip( re.sub(r" \(?(mix|feat|with|incl|prod).+", "", two, flags=re.IGNORECASE)): return True return False
def clean_metadata(metadata): for disc, tracks in metadata["tracks"].items(): for num, track in tracks.items(): for artist, importance in copy(track["artists"]): guest_artists = { re_strip(a) for a, i in track["artists"] if i in {"guest", "remixer"} } if re_strip(artist) in guest_artists and importance == "main": metadata["tracks"][disc][num]["artists"].remove( (artist, importance)) if metadata["catno"] and metadata["catno"].replace(" ", "") == str( metadata["upc"]): metadata["catno"] = None return metadata
def filter_results(results, artists, album): filtered = {} for rls_id, result in (results or {}).items(): if artists: split_artists = [] for a in artists: split_artists += re_split(re_strip(normalize_accents(a))) stripped_rls_artist = re_strip(normalize_accents(result[0].artist)) if "Various" in result[0].artist: if len(artists) == 1: continue elif not any(a in stripped_rls_artist for a in split_artists): continue elif not any(a in stripped_rls_artist.split() for a in chain.from_iterable( [a.split() for a in split_artists])): continue if album: if not _compare_albums(album, result[0].album): continue filtered[rls_id] = result return filtered