Use Cover Art Archive for albums with MusicBrainz IDs #12
+33
-37
@@ -109,6 +109,20 @@ type subsonicGetSongResponse struct {
|
|||||||
} `json:"subsonic-response"`
|
} `json:"subsonic-response"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getAlbumIDFromTrackID(username, trackID string) (string, error) {
|
||||||
|
data, err := host.SubsonicAPICall(fmt.Sprintf("getSong?u=%s&id=%s", username, trackID))
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("getSong failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var response subsonicGetSongResponse
|
||||||
|
if err := json.Unmarshal([]byte(data), &response); err != nil {
|
||||||
|
return "", fmt.Errorf("failed to parse getSong response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.Data.Song.AlbumID, nil
|
||||||
|
}
|
||||||
|
|
||||||
type subsonicGetAlbumResponse struct {
|
type subsonicGetAlbumResponse struct {
|
||||||
Data struct {
|
Data struct {
|
||||||
Album struct {
|
Album struct {
|
||||||
@@ -117,6 +131,20 @@ type subsonicGetAlbumResponse struct {
|
|||||||
} `json:"subsonic-response"`
|
} `json:"subsonic-response"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getMusicBrainzIDFromAlbumID(username, albumID string) (string, error) {
|
||||||
|
data, err := host.SubsonicAPICall(fmt.Sprintf("getAlbum?u=%s&id=%s", username, albumID))
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("getAlbum failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var response subsonicGetAlbumResponse
|
||||||
|
if err := json.Unmarshal([]byte(data), &response); err != nil {
|
||||||
|
return "", fmt.Errorf("failed to parse getAlbum response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.Data.Album.MusicBrainzId, nil
|
||||||
|
}
|
||||||
|
|
||||||
// https://musicbrainz.org/doc/Cover_Art_Archive/API
|
// https://musicbrainz.org/doc/Cover_Art_Archive/API
|
||||||
type caaResponse struct {
|
type caaResponse struct {
|
||||||
Images []struct {
|
Images []struct {
|
||||||
@@ -134,49 +162,19 @@ type caaResponse struct {
|
|||||||
ReleaseURL string `json:"release"`
|
ReleaseURL string `json:"release"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAlbumIDFromTrackID(username, trackID string) (string, error) {
|
|
||||||
data, err := host.SubsonicAPICall(fmt.Sprintf("getSong?u=%s&id=%s", username, trackID))
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("getSong failed: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var response subsonicGetSongResponse
|
|
||||||
if err := json.Unmarshal([]byte(data), &response); err != nil {
|
|
||||||
return "", fmt.Errorf("failed to parse getSong response: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return response.Data.Song.AlbumID, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getMusicBrainzIDFromAlbumID(username, albumID string) (string, error) {
|
|
||||||
data, err := host.SubsonicAPICall(fmt.Sprintf("getAlbum?u=%s&id=%s", username, albumID))
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("getAlbum failed: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var response subsonicGetAlbumResponse
|
|
||||||
if err := json.Unmarshal([]byte(data), &response); err != nil {
|
|
||||||
return "", fmt.Errorf("failed to parse getAlbum response: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return response.Data.Album.MusicBrainzId, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func fetchImageFromCAA(username, trackID string) (string, error) {
|
func fetchImageFromCAA(username, trackID string) (string, error) {
|
||||||
albumID, err := getAlbumIDFromTrackID(username, trackID)
|
albumID, err := getAlbumIDFromTrackID(username, trackID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("failed to get album ID from track %s: %w", trackID, err)
|
return "", fmt.Errorf("failed to get album ID from track %s: %w", trackID, err)
|
||||||
}
|
} else if albumID == "" {
|
||||||
if albumID == "" {
|
pdk.Log(pdk.LogDebug, fmt.Sprintf("No album for track %s", trackID))
|
||||||
pdk.Log(pdk.LogDebug, fmt.Sprintf("No Album ID for track %s", trackID))
|
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
musicBrainzID, err := getMusicBrainzIDFromAlbumID(username, albumID)
|
musicBrainzID, err := getMusicBrainzIDFromAlbumID(username, albumID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("failed to get MusicBrainz ID from album %s: %w", trackID, err)
|
return "", fmt.Errorf("failed to get MusicBrainz ID from album %s: %w", trackID, err)
|
||||||
}
|
} else if musicBrainzID == "" {
|
||||||
if musicBrainzID == "" {
|
|
||||||
pdk.Log(pdk.LogDebug, fmt.Sprintf("No MusicBrainz ID for album %s", albumID))
|
pdk.Log(pdk.LogDebug, fmt.Sprintf("No MusicBrainz ID for album %s", albumID))
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
@@ -184,12 +182,10 @@ func fetchImageFromCAA(username, trackID string) (string, error) {
|
|||||||
req := pdk.NewHTTPRequest(pdk.MethodGet, fmt.Sprintf("https://coverartarchive.org/release/%s", musicBrainzID))
|
req := pdk.NewHTTPRequest(pdk.MethodGet, fmt.Sprintf("https://coverartarchive.org/release/%s", musicBrainzID))
|
||||||
resp := req.Send()
|
resp := req.Send()
|
||||||
|
|
||||||
status := resp.Status()
|
if status := resp.Status(); status == 404 {
|
||||||
if status == 404 {
|
|
||||||
pdk.Log(pdk.LogDebug, fmt.Sprintf("No cover art for MusicBrainz ID %s", musicBrainzID))
|
pdk.Log(pdk.LogDebug, fmt.Sprintf("No cover art for MusicBrainz ID %s", musicBrainzID))
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
} else if status >= 400 {
|
||||||
if status >= 400 {
|
|
||||||
return "", fmt.Errorf("Cover Art Archive request failed: HTTP %d", resp.Status())
|
return "", fmt.Errorf("Cover Art Archive request failed: HTTP %d", resp.Status())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user