#Improve country detection/registration from MusicBrainz API responses

22 messages · Page 1 of 1 (latest)

quaint beacon
#

often, artists on MusicBrainz get tagged with the city of origin rather than just the country. this changes the API responses slightly, and it appears that the country field is omitted from these artists.
case in point: https://musicbrainz.org/ws/2/artist/416a8fc2-076b-4d9f-b972-a067e6b80e04
compare with an artist whose area is defined as a country: https://musicbrainz.org/ws/2/artist/c24d285d-1397-42c1-8372-65724294ecfa

fmbot appears to only look at the country field in API responses (https://github.com/fmbot-discord/fmbot/blob/dev/src/FMBot.Bot/Services/MusicBrainzService.cs#L46).
this makes commands such as .from or .countries (needlessly?) inaccurate.
i believe there is a relatively simple way to use the MusicBrainz API responses we're already requesting, to do a tiny bit more to search for the correct country and improve the accuracy of the aforementioned commands.

i would like to suggest that we first look for a non-null field at (excuse the notation) area.iso-3166-2-code-list.iso-3166-2-code, split by - and take the first element to get the country code.
https://en.wikipedia.org/wiki/ISO_3166-2 states that

The first part is the ISO 3166-1 alpha-2 code of the country;
so i believe this should be relatively safe to grab in the case of a non-null field, and that it'll always be a valid iso 3166-1 code which is what we desire.

if this field doesn't exist, one can fall back to country as we do today (there's also a field named area.iso-3166-1-code-list.iso-3166-1-code, but i think this is only set when country is also set, making it redundant).

one aspect i haven't thought of here is if there's any area in the MusicBrainz database with multiple iso 3166-2 codes. i've got the impression that such a location makes little sense, but it would be interesting to see if anyone's got an example of that occurring

summer cosmos
#

It's mainly due to a very old bug in the musicbrainz api: #help-legacy message

bright stirrupBOT
summer cosmos
quaint beacon
#

right, but could one not check these fields too as part of a strategy to determine a country? or is that too rickety somehow already (if there's a bunch of potential fields to look at if no country's set, or something like that)?

quaint beacon
#

something like ```csharp
#nullable enable
private string? GetCountryCodeForMusicBrainsArtist(MetaBrainz.MusicBrainz.Interfaces.Entities.IArtist musicBrainzArtist) {
var country = musicBrainzArtist.Country
?? musicBrainzArtist.Area?.Iso31662Codes?.FirstOrDefault()
?? musicBrainzArtist.Area?.Iso31661Codes?.FirstOrDefault()
?? musicBrainzArtist.BeginArea?.Iso31662Codes?.FirstOrDefault()
?? musicBrainzArtist.BeginArea?.Iso31661Codes?.FirstOrDefault();

    if (country == null) return null;
    
    // case for ISO 3166-2 code to turn it into ISO 3166-1; the other possible values in country can't have a -
    if (country.Contains('-')) return country.Split("-").First();

    return country;
}
and then (where applicable)
```diff
- artist.CountryCode = musicBrainzArtist.Country;
+ artist.CountryCode = GetCountryCodeForMusicBrainsArtist(musicBrainzArtist);

(untested + idk c# 😌 )

quaint beacon
summer cosmos
#

I'll leave that up to Frikandel I think. I have no idea about coding the bot 🙂

quaint beacon
#

yeah, no worries!

quaint beacon
#

i'm risking an annoying @ here, but i noticed there might have been a little bit of testing last night. since i made a lot of assumptions when writing the i'm curious if the logic seems to work as intended? @molten tusk

molten tusk
#

Yeah it was deployed to the develop bot

#

It seems to be working

quaint beacon
#

very cool

molten tusk
#

I moved it here from the extension

quaint beacon
#

yeah, i noticed! that was my first plan but i'd heard c# likes extensions so i made one haha

molten tusk
#

Hmm yeah I usually only do extensions if they would be used in multiple services

quaint beacon
#

yeah, in hindsight it makes little sense to have an extension to use in one file. so i agree on the change you made

molten tusk
#

Tbf the project is not 100% consistent in that but this made more sense for me

quaint beacon
#

slightly unrelated question: AddMusicBrainzDataToArtistAsync seems to only be referenced in SpotifyService. does that mean that if an artist isn't on spotify, we won't look into musicbrainz for any info either?

#

or wait, i think i understand the logic. it looks like it should commit musicBrainzUpdated.Artist as a base even if it's not found on spotify

molten tusk
#

Should be moved out of the spotifyservice