#region ========================================================================= USING ===================================================================================== using Lumina.Contracts.DTO.Common; using Lumina.Contracts.DTO.MediaContributors; using Lumina.Contracts.DTO.MediaLibrary.AudioLibrary; using Lumina.Contracts.DTO.MediaLibrary.AudioLibrary.MusicLibrary; using Lumina.Domain.SharedKernel.Common.Enums.AudioLibrary; using Lumina.Domain.SharedKernel.Common.Enums.Common; using Lumina.Domain.SharedKernel.Common.Enums.MediaContributors; using Lumina.Plugins.MusicBrainz.Common.Models.Contracts.Responses; using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text.RegularExpressions; #endregion namespace Lumina.Plugins.MusicBrainz.Core.Mapping; /// /// Maps MusicBrainz web service responses into the application's media metadata DTOs. /// internal static partial class MusicBrainzMapper { /// /// The neutral cultures of the process, indexed by both their two letter and three letter ISO 639 codes. /// Built once, because the available cultures do not change during the lifetime of the process. /// private static readonly IReadOnlyDictionary s_neutralCulturesByCode = BuildNeutralCultureLookup(); /// /// Matches a four digit year. /// [GeneratedRegex(@"\b(?:1[0-9]{3}|20[0-9]{2}|2100)\b")] private static partial Regex YearPattern(); /// /// Maps a MusicBrainz artist response into artist metadata. /// /// The artist response to map. /// The mapped artist metadata. public static ArtistMetadataDto MapArtist(MusicBrainzArtistResponse artist) { (List contributors, string? website) = MapArtistRelations(artist.Relations); return new ArtistMetadataDto( Name: NullIfWhiteSpace(artist.Name), SortName: NullIfWhiteSpace(artist.SortName), Disambiguation: NullIfWhiteSpace(artist.Disambiguation), Type: MapArtistType(artist.Type), Gender: MapArtistGender(artist.Gender), Country: NullIfWhiteSpace(artist.Country), Area: MapArea(artist.Area), BeginArea: MapArea(artist.BeginArea), EndArea: MapArea(artist.EndArea), LifeSpanBegin: ParseDate(artist.LifeSpan?.Begin).Date, LifeSpanEnd: ParseDate(artist.LifeSpan?.End).Date, IsEnded: artist.LifeSpan?.IsEnded ?? false, Website: website, MusicBrainzArtistId: ParseGuid(artist.Id), Ipis: [.. artist.Ipis.Where(ipi => !string.IsNullOrWhiteSpace(ipi))], Isnis: [.. artist.Isnis.Where(isni => !string.IsNullOrWhiteSpace(isni))], Aliases: [.. artist.Aliases.Select(MapAlias)], Genres: MapGenres(artist.Genres), Tags: MapTags(artist.Tags), Ratings: MapRating(artist.Rating), Contributors: contributors); } /// /// Maps a MusicBrainz release group, and optionally one of its releases, into album metadata. /// /// The release group response to map. /// The selected release of the release group, or when no release was selected. /// The mapped album metadata. public static AlbumMetadataDto MapAlbum(MusicBrainzReleaseGroupResponse releaseGroup, MusicBrainzReleaseResponse? release) { (DateOnly? originalDate, int? originalYear) = ParseDate(releaseGroup.FirstReleaseDate); (DateOnly? reReleaseDate, int? reReleaseYear) = ParseDate(release?.Date); if (reReleaseDate is not null && originalDate is not null && reReleaseDate.Value.Year == originalDate.Value.Year) { reReleaseDate = null; reReleaseYear = null; } string? releaseArtistId = releaseGroup.ArtistCredit.Select(credit => credit.Artist?.Id).FirstOrDefault(id => !string.IsNullOrWhiteSpace(id)) ?? release?.ArtistCredit.Select(credit => credit.Artist?.Id).FirstOrDefault(id => !string.IsNullOrWhiteSpace(id)); List contributors = MapArtistCredit(releaseGroup.ArtistCredit); if (contributors.Count == 0 && release is not null) contributors = MapArtistCredit(release.ArtistCredit); if (release is not null) AddContributors(contributors, MapContributorsFromRelations(release.Relations)); List media = release?.Media ?? []; int totalTracks = media.Sum(medium => medium.TrackCount); // A MusicBrainz release can carry several labels and catalog numbers: the label stores the first one, while every catalog number is kept. string? label = release?.LabelInfo.FirstOrDefault()?.Label?.Name; List catalogNumbers = [.. (release?.LabelInfo ?? []) .Select(labelInfo => NullIfWhiteSpace(labelInfo.CatalogNumber)) .Where(catalogNumber => catalogNumber is not null) .Select(catalogNumber => catalogNumber!) .Distinct(StringComparer.OrdinalIgnoreCase)]; return new AlbumMetadataDto( Title: NullIfWhiteSpace(releaseGroup.Title) ?? NullIfWhiteSpace(release?.Title), OriginalTitle: null, Description: null, Disambiguation: NullIfWhiteSpace(releaseGroup.Disambiguation), ReleaseInfo: new ReleaseInfoDto( OriginalReleaseDate: originalDate, OriginalReleaseYear: originalYear, ReReleaseDate: reReleaseDate, ReReleaseYear: reReleaseYear, ReleaseCountry: MapReleaseCountry(release?.Country), ReleaseVersion: NullIfWhiteSpace(release?.Disambiguation)), Language: MapLanguage(release?.TextRepresentation?.Language), OriginalLanguage: null, Tags: MapTags(releaseGroup.Tags), Genres: MapGenres(releaseGroup.Genres), Script: NullIfWhiteSpace(release?.TextRepresentation?.Script), ReleaseTypes: MapReleaseTypes(releaseGroup.PrimaryType, releaseGroup.SecondaryTypes), ReleaseStatus: MapReleaseStatus(release?.Status), MediaFormat: MapMediaFormat(media.FirstOrDefault()?.Format), Packaging: MapPackaging(release?.Packaging), TotalDiscs: media.Count > 0 ? media.Count : null, TotalTracks: totalTracks > 0 ? totalTracks : null, Barcode: NullIfWhiteSpace(release?.Barcode), CatalogNumbers: catalogNumbers, Label: NullIfWhiteSpace(label), ASIN: NullIfWhiteSpace(release?.Asin), MusicBrainzReleaseId: ParseGuid(release?.Id), MusicBrainzReleaseGroupId: ParseGuid(releaseGroup.Id), MusicBrainzReleaseArtistId: ParseGuid(releaseArtistId), Contributors: contributors, Ratings: MapRating(releaseGroup.Rating), ReleaseTitle: NullIfWhiteSpace(release?.Title)); } /// /// Maps a MusicBrainz recording, and optionally its work and release, into track metadata. /// /// The recording response to map. /// The work the recording is an instance of, or when the recording has no work. /// The release the recording appears on, or when it is not known. /// The mapped track metadata. public static AudioMetadataDto MapTrack(MusicBrainzRecordingResponse recording, MusicBrainzWorkResponse? work, MusicBrainzReleaseResponse? release) { (DateOnly? releaseDate, int? releaseYear) = ParseDate(recording.FirstReleaseDate); // A recording embedded in a release lookup does not carry its own first release date, so the date of the release the track appears on is used // instead, rather than leaving the track without any release date at all. if (releaseYear is null) (releaseDate, releaseYear) = ParseDate(release?.Date); List contributors = MapArtistCredit(recording.ArtistCredit); AddContributors(contributors, MapContributorsFromRelations(recording.Relations)); if (work is not null) AddContributors(contributors, MapContributorsFromRelations(work.Relations)); return new AudioMetadataDto( Title: NullIfWhiteSpace(recording.Title), OriginalTitle: null, Description: null, Disambiguation: NullIfWhiteSpace(recording.Disambiguation), ReleaseInfo: new ReleaseInfoDto( OriginalReleaseDate: releaseDate, OriginalReleaseYear: releaseYear, ReReleaseDate: null, ReReleaseYear: null, ReleaseCountry: null, ReleaseVersion: null), Language: MapLanguage(release?.TextRepresentation?.Language), OriginalLanguage: null, Tags: MapTags(recording.Tags), Genres: MapGenres(recording.Genres), Script: NullIfWhiteSpace(release?.TextRepresentation?.Script), Key: null, Bpm: null, IsVideo: recording.IsVideo ?? false, Work: work is null ? null : MapWork(work), Isrcs: [.. recording.Isrcs.Where(isrc => !string.IsNullOrWhiteSpace(isrc)).Select(isrc => new IsrcDto(isrc))], Moods: null, DurationInSeconds: recording.Length is long length && length > 0 ? (int)(length / 1000) : null, SampleRate: null, Channels: null, BitDepth: null, AudioCodec: null, Bitrate: null, MusicBrainzRecordingId: ParseGuid(recording.Id), MusicBrainzTrackId: null, Contributors: contributors, Ratings: MapRating(recording.Rating)); } /// /// Maps a MusicBrainz work into a work DTO. /// /// The work response to map. /// The mapped work. public static MusicWorkDto MapWork(MusicBrainzWorkResponse work) { return new MusicWorkDto( MusicBrainzWorkId: ParseGuid(work.Id), Title: NullIfWhiteSpace(work.Title), Type: NullIfWhiteSpace(work.Type), Languages: [.. work.Languages.Select(MapLanguage).Where(language => language is not null).Select(language => language!)], Iswcs: [.. work.Iswcs.Where(iswc => !string.IsNullOrWhiteSpace(iswc))]); } /// /// Maps a MusicBrainz area into an area DTO. /// /// The area response to map. /// The mapped area, or when no area is provided. public static MusicAreaDto? MapArea(MusicBrainzAreaResponse? area) { if (area is null) return null; string? isoCode = area.Iso3166Part1Codes.FirstOrDefault() ?? area.Iso3166Part2Codes.FirstOrDefault(); return new MusicAreaDto( MusicBrainzAreaId: ParseGuid(area.Id), Name: NullIfWhiteSpace(area.Name), SortName: NullIfWhiteSpace(area.SortName), Disambiguation: NullIfWhiteSpace(area.Disambiguation), Type: NullIfWhiteSpace(area.Type), Iso3166Code: NullIfWhiteSpace(isoCode)); } /// /// Maps a MusicBrainz alias into an alias DTO. /// /// The alias response to map. /// The mapped alias. private static MusicArtistAliasDto MapAlias(MusicBrainzAliasResponse alias) { return new MusicArtistAliasDto( Name: NullIfWhiteSpace(alias.Name), SortName: NullIfWhiteSpace(alias.SortName), Type: NullIfWhiteSpace(alias.Type), Locale: NullIfWhiteSpace(alias.Locale), IsPrimary: alias.IsPrimary ?? false, BeginDate: ParseDate(alias.Begin).Date, EndDate: ParseDate(alias.End).Date, IsEnded: alias.IsEnded ?? false); } /// /// Maps the genres of a MusicBrainz entity into genre DTOs. /// /// The genre responses to map. /// The mapped genre DTOs. private static List MapGenres(IReadOnlyCollection genres) { return [.. genres .Where(genre => !string.IsNullOrWhiteSpace(genre.Name)) .Select(genre => new GenreDto(genre.Name)) .DistinctBy(genre => genre.Name, StringComparer.OrdinalIgnoreCase)]; } /// /// Maps the tags of a MusicBrainz entity into tag DTOs. /// /// The tag responses to map. /// The mapped tag DTOs. private static List MapTags(IReadOnlyCollection tags) { return [.. tags .Where(tag => !string.IsNullOrWhiteSpace(tag.Name)) .Select(tag => new TagDto(tag.Name)) .DistinctBy(tag => tag.Name, StringComparer.OrdinalIgnoreCase)]; } /// /// Maps the rating of a MusicBrainz entity into a rating DTO. /// /// The rating response to map. /// The mapped rating DTO, or an empty list when no rating is provided. private static List MapRating(MusicBrainzRatingResponse? rating) { if (rating is null || (rating.Value is null && rating.VotesCount is null)) return []; return [new AudioRatingDto(rating.Value, 5m, AudioRatingSource.MusicBrainz, rating.VotesCount)]; } /// /// Maps an ISO 639-3 language code into a language DTO. /// /// The code to map. /// The mapped language, or when the code is empty. private static LanguageInfoDto? MapLanguage(string? rawCode) { if (string.IsNullOrWhiteSpace(rawCode)) return null; string code = rawCode.Trim().ToLowerInvariant(); // A language code can list several languages, such as "eng/ara"; the first one is the language of the release. string primaryCode = code.Split('/').Select(part => part.Trim()).FirstOrDefault(part => part.Length > 0) ?? code; // These ISO 639-3 codes describe a language absence rather than a culture, so they cannot be resolved through a culture. switch (primaryCode) { case "zxx": return new LanguageInfoDto(primaryCode, "No linguistic content", null); case "mul": return new LanguageInfoDto(primaryCode, "Multiple languages", null); case "und": return new LanguageInfoDto(primaryCode, "Undetermined", null); default: break; } // Only neutral cultures are matched, because a specific culture would resolve to an arbitrary regional name such as "German (Austria)". return s_neutralCulturesByCode.TryGetValue(primaryCode, out CultureInfo? culture) ? new LanguageInfoDto(culture.TwoLetterISOLanguageName, culture.EnglishName, culture.NativeName) : new LanguageInfoDto(primaryCode, primaryCode, null); } /// /// Builds the lookup of the process' neutral cultures, indexed by both their two letter and three letter ISO 639 codes. /// /// The neutral cultures, indexed by their ISO 639 codes. private static IReadOnlyDictionary BuildNeutralCultureLookup() { Dictionary culturesByCode = new(StringComparer.OrdinalIgnoreCase); foreach (CultureInfo culture in CultureInfo.GetCultures(CultureTypes.NeutralCultures)) { // A culture without a two letter ISO 639 code is not a language, so it is skipped; the two letter code is the one carried by the DTOs. if (culture.TwoLetterISOLanguageName.Length != 2) continue; culturesByCode.TryAdd(culture.ThreeLetterISOLanguageName, culture); culturesByCode.TryAdd(culture.TwoLetterISOLanguageName, culture); } return culturesByCode; } /// /// Maps the artist credit of a MusicBrainz entity into attributed performing contributors. /// /// The artist credit to map. /// The mapped contributors. private static List MapArtistCredit(IReadOnlyCollection artistCredit) { List contributors = []; foreach (MusicBrainzArtistCreditResponse credit in artistCredit) { string? name = NullIfWhiteSpace(credit.Name) ?? NullIfWhiteSpace(credit.Artist?.Name); if (name is null) continue; AddContributor(contributors, name, MediaContributorRole.Performer); } return contributors; } /// /// Maps the artist relations of a MusicBrainz entity into contributors, excluding the URL relations. /// /// The relations to map. /// The mapped contributors. private static List MapContributorsFromRelations(IReadOnlyCollection relations) { List contributors = []; foreach (MusicBrainzRelationResponse relation in relations) { if (!string.Equals(relation.TargetType, "artist", StringComparison.OrdinalIgnoreCase)) continue; string? name = NullIfWhiteSpace(relation.Artist?.Name); if (name is null) continue; AddContributor(contributors, name, MapRole(relation.Type, relation.Attributes)); } return contributors; } /// /// Maps the relations of an artist into its contributing members, and its official homepage. /// /// The relations of the artist. /// The mapped contributors, and the official homepage of the artist. private static (List Contributors, string? Website) MapArtistRelations(IReadOnlyCollection relations) { List contributors = []; string? website = null; foreach (MusicBrainzRelationResponse relation in relations) { if (string.Equals(relation.TargetType, "url", StringComparison.OrdinalIgnoreCase)) { if (website is null && string.Equals(relation.Type, "official homepage", StringComparison.OrdinalIgnoreCase)) website = NullIfWhiteSpace(relation.Url?.Resource); continue; } if (!string.Equals(relation.TargetType, "artist", StringComparison.OrdinalIgnoreCase)) continue; string? name = NullIfWhiteSpace(relation.Artist?.Name); if (name is null) continue; AddContributor(contributors, name, MapRole(relation.Type, relation.Attributes)); } return (contributors, website); } /// /// Adds a contributor to the provided list, unless an identical name and role pair is already present. /// /// The contributors collected so far. /// The display name of the contributor. /// The role the contributor played. private static void AddContributor(List contributors, string displayName, MediaContributorRole role) { if (contributors.Any(contributor => string.Equals(contributor.Name?.DisplayName, displayName, StringComparison.OrdinalIgnoreCase) && contributor.Role == role)) return; contributors.Add(new MediaContributorDto(new MediaContributorNameDto(displayName, null), role)); } /// /// Adds the contributors to , skipping the duplicate name and role pairs. /// /// The contributors collected so far. /// The contributors to add. private static void AddContributors(List contributors, IReadOnlyCollection incoming) { foreach (MediaContributorDto contributor in incoming) { if (contributor.Name?.DisplayName is not string displayName) continue; AddContributor(contributors, displayName, contributor.Role ?? MediaContributorRole.Other); } } /// /// Maps a MusicBrainz relationship type, and its attributes, into a canonical media contributor role. /// /// The relationship type to map. /// The attributes of the relationship. /// The mapped contributor role. private static MediaContributorRole MapRole(string? relationType, IReadOnlyCollection attributes) { // An instrument attribute is more specific than the generic relationship type, so it is preferred. foreach (string attribute in attributes) { MediaContributorRole? attributed = MapInstrument(attribute); if (attributed is not null) return attributed.Value; } if (string.IsNullOrWhiteSpace(relationType)) return MediaContributorRole.Other; string type = relationType.Trim().ToLowerInvariant(); if (type.Contains("executive producer", StringComparison.Ordinal)) return MediaContributorRole.ExecutiveProducer; if (type.Contains("producer", StringComparison.Ordinal)) return MediaContributorRole.Producer; if (type.Contains("remixer", StringComparison.Ordinal)) return MediaContributorRole.Remixer; if (type.Contains("mix", StringComparison.Ordinal)) return MediaContributorRole.Mixer; if (type.Contains("engineer", StringComparison.Ordinal) || type.Contains("mastering", StringComparison.Ordinal)) return MediaContributorRole.Engineer; if (type.Contains("composer", StringComparison.Ordinal)) return MediaContributorRole.Composer; if (type.Contains("writer", StringComparison.Ordinal)) return MediaContributorRole.Author; if (type.Contains("lyricist", StringComparison.Ordinal)) return MediaContributorRole.Lyricist; if (type.Contains("librettist", StringComparison.Ordinal)) return MediaContributorRole.Lyricist; if (type.Contains("arranger", StringComparison.Ordinal)) return MediaContributorRole.Arranger; if (type.Contains("conductor", StringComparison.Ordinal)) return MediaContributorRole.Conductor; if (type.Contains("orchestrator", StringComparison.Ordinal)) return MediaContributorRole.Orchestrator; if (type.Contains("backing vocals", StringComparison.Ordinal)) return MediaContributorRole.BackingVocals; if (type.Contains("vocal", StringComparison.Ordinal)) return MediaContributorRole.Vocals; if (type.Contains("member of band", StringComparison.Ordinal) || type.Contains("performer", StringComparison.Ordinal) || type.Contains("orchestra", StringComparison.Ordinal) || type.Contains("choir", StringComparison.Ordinal)) return MediaContributorRole.Performer; return MediaContributorRole.Other; } /// /// Maps a MusicBrainz instrument attribute into a canonical media contributor role. /// /// The instrument attribute to map. /// The mapped contributor role, or when the attribute is not an instrument. private static MediaContributorRole? MapInstrument(string? attribute) { if (string.IsNullOrWhiteSpace(attribute)) return null; string value = attribute.Trim().ToLowerInvariant(); if (value.Contains("backing vocal", StringComparison.Ordinal)) return MediaContributorRole.BackingVocals; if (value.Contains("vocal", StringComparison.Ordinal)) return MediaContributorRole.Vocals; if (value.Contains("bass guitar", StringComparison.Ordinal)) return MediaContributorRole.BassGuitar; if (value.Contains("guitar", StringComparison.Ordinal)) return MediaContributorRole.Guitar; if (value.Contains("drum", StringComparison.Ordinal)) return MediaContributorRole.Drums; if (value.Contains("percussion", StringComparison.Ordinal)) return MediaContributorRole.Percussion; if (value.Contains("keyboard", StringComparison.Ordinal)) return MediaContributorRole.Keyboards; if (value.Contains("piano", StringComparison.Ordinal)) return MediaContributorRole.Piano; if (value.Contains("synthesizer", StringComparison.Ordinal) || value.Contains("synth", StringComparison.Ordinal)) return MediaContributorRole.Synthesizer; if (value.Contains("string", StringComparison.Ordinal) || value.Contains("violin", StringComparison.Ordinal) || value.Contains("cello", StringComparison.Ordinal) || value.Contains("viola", StringComparison.Ordinal)) return MediaContributorRole.Strings; if (value.Contains("brass", StringComparison.Ordinal) || value.Contains("trumpet", StringComparison.Ordinal) || value.Contains("trombone", StringComparison.Ordinal) || value.Contains("horn", StringComparison.Ordinal)) return MediaContributorRole.Brass; if (value.Contains("woodwind", StringComparison.Ordinal) || value.Contains("flute", StringComparison.Ordinal) || value.Contains("saxophone", StringComparison.Ordinal) || value.Contains("clarinet", StringComparison.Ordinal)) return MediaContributorRole.Woodwinds; return null; } /// /// Maps a MusicBrainz artist type into the application enum. /// /// The artist type to map. /// The mapped artist type, or when the type is unknown. private static MusicArtistType? MapArtistType(string? type) { if (string.IsNullOrWhiteSpace(type)) return null; return type.Trim().ToLowerInvariant() switch { "person" => MusicArtistType.Person, "group" => MusicArtistType.Group, "orchestra" => MusicArtistType.Orchestra, "choir" => MusicArtistType.Choir, "character" => MusicArtistType.Character, "other" => MusicArtistType.Other, _ => null }; } /// /// Maps a MusicBrainz gender into the application enum. /// /// The gender to map. /// The mapped gender, or when the gender is unknown. private static MusicArtistGender? MapArtistGender(string? gender) { if (string.IsNullOrWhiteSpace(gender)) return null; return gender.Trim().ToLowerInvariant() switch { "male" => MusicArtistGender.Male, "female" => MusicArtistGender.Female, "other" => MusicArtistGender.Other, "not applicable" => MusicArtistGender.NotApplicable, _ => null }; } /// /// Maps the primary and secondary types of a release group into the application enum. /// /// The primary type of the release group. /// The secondary types of the release group. /// The mapped release types. private static List MapReleaseTypes(string? primaryType, IReadOnlyCollection secondaryTypes) { List releaseTypes = []; MusicReleaseType? primary = MapReleaseType(primaryType); if (primary is not null) releaseTypes.Add(primary.Value); foreach (string secondaryType in secondaryTypes) { MusicReleaseType? mapped = MapReleaseType(secondaryType); if (mapped is not null && !releaseTypes.Contains(mapped.Value)) releaseTypes.Add(mapped.Value); } return releaseTypes; } /// /// Maps a single MusicBrainz release type into the application enum. /// /// The release type to map. /// The mapped release type, or when the type is unknown. private static MusicReleaseType? MapReleaseType(string? type) { if (string.IsNullOrWhiteSpace(type)) return null; return type.Trim().ToLowerInvariant() switch { "album" => MusicReleaseType.Album, "single" => MusicReleaseType.Single, "ep" => MusicReleaseType.Ep, "broadcast" => MusicReleaseType.Broadcast, "other" => MusicReleaseType.Other, "compilation" => MusicReleaseType.Compilation, "live" => MusicReleaseType.Live, "soundtrack" => MusicReleaseType.Soundtrack, "spokenword" => MusicReleaseType.SpokenWord, "audio drama" => MusicReleaseType.AudioDrama, "audiobook" => MusicReleaseType.AudioBook, "demo" => MusicReleaseType.Demo, "dj-mix" => MusicReleaseType.DjMix, "field recording" => MusicReleaseType.FieldRecording, "interview" => MusicReleaseType.Interview, "mixtape/street" => MusicReleaseType.Mixtape, "remix" => MusicReleaseType.Remix, _ => null }; } /// /// Maps a MusicBrainz release status into the application enum. /// /// The release status to map. /// The mapped release status, or when the status is unknown. private static MusicReleaseStatus? MapReleaseStatus(string? status) { if (string.IsNullOrWhiteSpace(status)) return null; return status.Trim().ToLowerInvariant() switch { "official" => MusicReleaseStatus.Official, "promotion" => MusicReleaseStatus.Promotion, "bootleg" => MusicReleaseStatus.Bootleg, "pseudo-release" => MusicReleaseStatus.PseudoRelease, "withdrawn" => MusicReleaseStatus.Withdrawn, "cancelled" => MusicReleaseStatus.Cancelled, "expunged" => MusicReleaseStatus.Expunged, _ => null }; } /// /// Maps a MusicBrainz medium format into the application enum. /// /// The format to map. /// The mapped media format, or when the format is unknown. private static MusicMediaFormat? MapMediaFormat(string? format) { if (string.IsNullOrWhiteSpace(format)) return null; return format.Trim().ToLowerInvariant() switch { "cd" => MusicMediaFormat.CD, "vinyl" => MusicMediaFormat.Vinyl, "12\" vinyl" => MusicMediaFormat.Inch12Vinyl, "10\" vinyl" => MusicMediaFormat.Inch10Vinyl, "7\" vinyl" => MusicMediaFormat.Inch7Vinyl, "digital media" => MusicMediaFormat.DigitalMedia, "cassette" => MusicMediaFormat.Cassette, "dvd" => MusicMediaFormat.DVD, "dvd-video" => MusicMediaFormat.DVDVideo, "dvd-audio" => MusicMediaFormat.DVDAudio, "sacd" => MusicMediaFormat.SACD, "hybrid sacd" => MusicMediaFormat.HybridSACD, "blu-ray" => MusicMediaFormat.BluRay, "minidisc" => MusicMediaFormat.MiniDisc, "8cm cd" => MusicMediaFormat.Cm8CD, "dat" => MusicMediaFormat.DAT, "other" => MusicMediaFormat.Other, _ => MusicMediaFormat.Other }; } /// /// Maps a MusicBrainz release packaging into the application enum. /// /// The packaging to map. /// The mapped packaging, or when the packaging is unknown. private static MusicReleasePackaging? MapPackaging(string? packaging) { if (string.IsNullOrWhiteSpace(packaging)) return null; return packaging.Trim().ToLowerInvariant() switch { "book" => MusicReleasePackaging.Book, "box" => MusicReleasePackaging.Box, "cardboard/paper sleeve" => MusicReleasePackaging.CardboardPaperSleeve, "cassette case" => MusicReleasePackaging.CassetteCase, "clamshell case" => MusicReleasePackaging.ClamshellCase, "digibook" => MusicReleasePackaging.Digibook, "digifile" => MusicReleasePackaging.Digifile, "digipak" => MusicReleasePackaging.Digipak, "discbox slider" => MusicReleasePackaging.DiscboxSlider, "fatbox" => MusicReleasePackaging.Fatbox, "gatefold cover" => MusicReleasePackaging.GatefoldCover, "jewel case" => MusicReleasePackaging.JewelCase, "keep case" => MusicReleasePackaging.KeepCase, "longbox" => MusicReleasePackaging.Longbox, "metal tin" => MusicReleasePackaging.MetalTin, "plastic sleeve" => MusicReleasePackaging.PlasticSleeve, "slidepack" => MusicReleasePackaging.Slidepack, "slim jewel case" => MusicReleasePackaging.SlimJewelCase, "slipcase" => MusicReleasePackaging.Slipcase, "snap case" => MusicReleasePackaging.SnapCase, "snappack" => MusicReleasePackaging.SnapPack, "super jewel box" => MusicReleasePackaging.SuperJewelBox, "none" => MusicReleasePackaging.None, "other" => MusicReleasePackaging.Other, _ => MusicReleasePackaging.Other }; } /// /// Maps an ISO 3166-1 alpha-2 country code into the application enum. /// /// The country code to map. /// The mapped release country, or when the code is unknown. private static ReleaseCountry? MapReleaseCountry(string? country) { if (string.IsNullOrWhiteSpace(country)) return null; return Enum.TryParse(country.Trim(), ignoreCase: true, out ReleaseCountry releaseCountry) ? releaseCountry : null; } /// /// Parses a MusicBrainz identifier into a . /// /// The MusicBrainz identifier to parse. /// The parsed identifier, or when the value is not a valid identifier. private static Guid? ParseGuid(string? value) { return Guid.TryParse(value, out Guid parsed) ? parsed : null; } /// /// Parses a MusicBrainz date string into a date and a year. /// /// The date string to parse. /// The parsed date and year, or values when the string could not be parsed. private static (DateOnly? Date, int? Year) ParseDate(string? value) { if (string.IsNullOrWhiteSpace(value)) return (null, null); if (DateOnly.TryParse(value.Trim(), CultureInfo.InvariantCulture, DateTimeStyles.None, out DateOnly exact)) return (exact, exact.Year); Match yearMatch = YearPattern().Match(value); return yearMatch.Success && int.TryParse(yearMatch.Value, CultureInfo.InvariantCulture, out int year) ? (null, year) : (null, null); } /// /// Returns the trimmed value, or when the value is empty or white space. /// /// The value to process. /// The trimmed value, or . private static string? NullIfWhiteSpace(string? value) { return string.IsNullOrWhiteSpace(value) ? null : value.Trim(); } }