-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathaudiomuse_api.go
More file actions
81 lines (68 loc) · 2.74 KB
/
Copy pathaudiomuse_api.go
File metadata and controls
81 lines (68 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"github.com/navidrome/navidrome/plugins/pdk/go/host"
"github.com/navidrome/navidrome/plugins/pdk/go/pdk"
)
type SimilarArtistsResponse struct {
Artist string `json:"artist"`
ArtistID string `json:"artist_id"`
ComponentMatches []struct {
Artist1RepresentativeSongs []struct {
ItemID string `json:"item_id"`
Title string `json:"title"`
} `json:"artist1_representative_songs"`
Artist2RepresentativeSongs []struct {
ItemID string `json:"item_id"`
Title string `json:"title"`
} `json:"artist2_representative_songs"`
} `json:"component_matches"`
}
func getSimilarArtists(id string, includeComponentMatches bool) ([]SimilarArtistsResponse, error) {
apiBaseURL := getConfigString(configAPIUrl, defaultAPIUrl)
artistCount := getConfigInt("artistSimilarCount", defaultArtistSimilarCount)
params := url.Values{}
params.Set("artist", id)
params.Set("n", strconv.Itoa(artistCount))
params.Set("include_component_matches", strconv.FormatBool(includeComponentMatches))
if server := getConfigString(configServer, ""); server != "" {
params.Set("server", server)
}
apiURL := fmt.Sprintf("%s/api/similar_artists?%s", apiBaseURL, params.Encode())
pdk.Log(pdk.LogInfo, fmt.Sprintf("[AudioMuse] Calling GetSimilarArtists API for artist ID %s: %s", id, apiURL))
// Make HTTP GET request to AudioMuse-AI using the host HTTP service.
// This uses host.HTTPSend as recommended by Navidrome upstream (migrated from pdk.NewHTTPRequest).
resp, err := host.HTTPSend(host.HTTPRequest{
Method: "GET",
URL: apiURL,
Headers: authHeaders(),
})
if err != nil {
errMsg := fmt.Sprintf("[AudioMuse] ERROR: HTTP request failed: %v", err)
pdk.Log(pdk.LogError, errMsg)
return nil, fmt.Errorf("AudioMuse-AI HTTP request failed: %w", err)
}
if resp == nil {
errMsg := "[AudioMuse] ERROR: empty HTTP response"
pdk.Log(pdk.LogError, errMsg)
return nil, fmt.Errorf("AudioMuse-AI returned an empty HTTP response")
}
pdk.Log(pdk.LogInfo, fmt.Sprintf("[AudioMuse] API response status: %d", resp.StatusCode))
if resp.StatusCode != 200 {
errMsg := fmt.Sprintf("[AudioMuse] ERROR: AudioMuse-AI returned status %d", resp.StatusCode)
pdk.Log(pdk.LogError, errMsg)
return nil, fmt.Errorf("AudioMuse-AI returned status %d", resp.StatusCode)
}
var artists []SimilarArtistsResponse
body := resp.Body
if err := json.Unmarshal(body, &artists); err != nil {
errMsg := fmt.Sprintf("[AudioMuse] ERROR: Failed to parse artist response: %v", err)
pdk.Log(pdk.LogError, errMsg)
return nil, fmt.Errorf("failed to parse AudioMuse-AI artist response: %w", err)
}
pdk.Log(pdk.LogInfo, fmt.Sprintf("[AudioMuse] Successfully parsed %d similar artists", len(artists)))
return artists, nil
}