예제 #1
0
func parseAcousticIdReply(reply string, existingTag ...editor.Tag) (editor.Tag, string) {
	var fields map[string]interface{}
	err := json.Unmarshal([]byte(reply), &fields)

	if err != nil || fields["status"] != "ok" {
		return editor.Tag{}, ""
	}

	if fields["results"] == nil {
		return editor.Tag{}, ""
	}
	results := fields["results"].([]interface{})
	if len(results) == 0 {
		return editor.Tag{}, ""
	}
	result := results[0].(map[string]interface{})

	if result["releases"] == nil {
		return editor.Tag{}, ""
	}
	releases := result["releases"].([]interface{})
	if len(releases) == 0 {
		return editor.Tag{}, ""
	}
	release := pickRelease(releases, existingTag...)

	var tag editor.Tag
	tag.Year = getReleaseDate(release)
	tag.Artist = getReleaseArtist(release)
	tag.Album = getReleaseAlbum(release)
	tag.Title = getReleaseTitle(release)
	tag.Track = getReleaseTrack(release)

	return tag, release["id"].(string)
}
예제 #2
0
func pickRelease(releases []interface{}, existingTag ...editor.Tag) map[string]interface{} {
	var tag editor.Tag
	if len(existingTag) > 0 {
		tag.Title = strings.ToUpper(existingTag[0].Title)
		tag.Artist = strings.ToUpper(existingTag[0].Artist)
		tag.Album = strings.ToUpper(existingTag[0].Album)
	}

	var best int = 0
	for i := 1; i < len(releases); i++ {
		if isMoreSuitableRelease(releases[i].(map[string]interface{}), releases[best].(map[string]interface{}), tag) {
			best = i
		}
	}

	return releases[best].(map[string]interface{})
}