Simplify MusicBrainz Info type and support more tags.

This commit is contained in:
David Howden 2015-06-08 11:01:32 +10:00
parent c06066fd88
commit d3e0c4a33f

View File

@ -8,24 +8,18 @@ import (
"github.com/dhowden/tag"
)
// Info is a structure which contains MusicBrainz identifier information.
type Info struct {
AcoustID string
Album string
AlbumArtist string
Artist string
ReleaseGroup string
Track string
}
// Supported MusicBrainz tag names
// Supported MusicBrainz tag names.
const (
TagAcoustID = "acoustid_id"
TagAlbum = "musicbrainz_albumid"
TagAlbumArtist = "musicbrainz_albumartistid"
TagArtist = "musicbrainz_artistid"
TagReleaseGroup = "musicbrainz_releasegroupid"
TagTrack = "musicbrainz_recordingid"
AcoustID = "acoustid_id"
AcoustFingerprint = "acoustid_fingerprint"
Album = "musicbrainz_albumid"
AlbumArtist = "musicbrainz_albumartistid"
Artist = "musicbrainz_artistid"
Disc = "musicbrainz_discid"
Recording = "musicbrainz_recordingid"
ReleaseGroup = "musicbrainz_releasegroupid"
Track = "musicbrainz_trackid"
TRM = "musicbrainz_trmid"
)
// UFIDProviderURL is the URL that we match inside a UFID tag.
@ -33,41 +27,36 @@ const UFIDProviderURL = "http://musicbrainz.org"
// Mapping between the internal picard tag names and aliases.
var tags = map[string]string{
TagAcoustID: "Acoustid Id",
TagAlbum: "MusicBrainz Album Id",
TagAlbumArtist: "MusicBrainz Album Artist Id",
TagArtist: "MusicBrainz Artist Id",
TagReleaseGroup: "MusicBrainz Release Group Id",
TagTrack: "MusicBrainz Track Id",
AcoustID: "Acoustid Id",
AcoustFingerprint: "Acoustid Fingerprint",
Album: "MusicBrainz Album Id",
AlbumArtist: "MusicBrainz Album Artist Id",
Artist: "MusicBrainz Artist Id",
Disc: "MusicBrainz Disc Id",
Recording: "MusicBrainz Track Id",
ReleaseGroup: "MusicBrainz Release Group Id",
Track: "MusicBrainz Release Track Id",
TRM: "MusicBrainz TRM Id",
}
func (i *Info) set(t, v string) {
switch t {
case TagAcoustID:
i.AcoustID = v
case TagAlbum:
i.Album = v
case TagAlbumArtist:
i.AlbumArtist = v
case TagArtist:
i.Artist = v
case TagReleaseGroup:
i.ReleaseGroup = v
case TagTrack:
i.Track = v
}
// Info is a structure which contains MusicBrainz identifier information.
type Info map[string]string
// Get returns the value for the given MusicBrainz tag.
func (i Info) Get(tag string) string {
return i[tag]
}
// Set the MusicBrainz tag to the given value.
func (i *Info) Set(t, v string) {
// set the MusicBrainz tag to the given value.
func (i Info) set(t, v string) {
if _, ok := tags[t]; ok {
i.set(t, v)
i[t] = v
return
}
for k, tt := range tags {
if tt == t {
i.set(k, v)
i[k] = v
return
}
}
@ -75,7 +64,7 @@ func (i *Info) Set(t, v string) {
// extractID3 attempts to extract MusicBrainz Picard tags from m.Raw(), where m.Format
// is assumed to be a supported version of ID3.
func extractID3(m tag.Metadata) *Info {
func extractID3(m tag.Metadata) Info {
var txxx, ufid string
switch m.Format() {
case tag.ID3v2_2:
@ -84,17 +73,17 @@ func extractID3(m tag.Metadata) *Info {
txxx, ufid = "TXXX", "UFID"
}
i := &Info{}
i := Info{}
for k, v := range m.Raw() {
switch {
case strings.HasPrefix(k, txxx):
if str, ok := v.(*tag.Comm); ok {
i.Set(str.Description, str.Text)
i.set(str.Description, str.Text)
}
case strings.HasPrefix(k, ufid):
if id, ok := v.(*tag.UFID); ok {
if id.Provider == UFIDProviderURL {
i.Set(TagTrack, string(id.Identifier))
i.set(Recording, string(id.Identifier))
}
}
}
@ -104,11 +93,11 @@ func extractID3(m tag.Metadata) *Info {
// extractMP4Vorbis attempts to extract MusicBrainz Picard tags from m.Raw(), where m.Format
// is assumed to be MP4 or VORBIS.
func extractMP4Vorbis(m tag.Metadata) *Info {
i := &Info{}
func extractMP4Vorbis(m tag.Metadata) Info {
i := Info{}
for t, v := range m.Raw() {
if s, ok := v.(string); ok {
i.Set(t, s)
i.set(t, s)
}
}
return i
@ -116,7 +105,7 @@ func extractMP4Vorbis(m tag.Metadata) *Info {
// Extract tags created by MusicBrainz Picard which can be used with with the MusicBrainz and LastFM APIs.
// See https://picard.musicbrainz.org/docs/mappings/ for more information.
func Extract(m tag.Metadata) *Info {
func Extract(m tag.Metadata) Info {
switch m.Format() {
case tag.ID3v2_2, tag.ID3v2_3, tag.ID3v2_4:
return extractID3(m)