|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/url" |
| 7 | + "sort" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + "github.com/navidrome/navidrome/plugins/pdk/go/metadata" |
| 11 | + "github.com/navidrome/navidrome/plugins/pdk/go/pdk" |
| 12 | +) |
| 13 | + |
| 14 | +// Configuration keys (must match manifest.json) |
| 15 | +const ( |
| 16 | + configAPIUrl = "apiUrl" |
| 17 | + configTrackCount = "trackCount" |
| 18 | + configEliminateDuplicates = "eliminateDuplicates" |
| 19 | + configRadiusSimilarity = "radiusSimilarity" |
| 20 | +) |
| 21 | + |
| 22 | +// Default values |
| 23 | +const ( |
| 24 | + defaultAPIUrl = "http://192.168.3.203:8000" |
| 25 | + defaultTrackCount = 200 |
| 26 | + defaultEliminateDuplicates = true |
| 27 | + defaultRadiusSimilarity = true |
| 28 | +) |
| 29 | + |
| 30 | +// audioMuseResponse represents a single track from AudioMuse-AI API |
| 31 | +type audioMuseResponse struct { |
| 32 | + ItemID string `json:"item_id"` |
| 33 | + Title string `json:"title"` |
| 34 | + Author string `json:"author"` |
| 35 | + Album string `json:"album"` |
| 36 | + Distance float64 `json:"distance"` |
| 37 | +} |
| 38 | + |
| 39 | +const pluginID = "audiomuseai" |
| 40 | + |
| 41 | +type audioMusePlugin struct{} |
| 42 | + |
| 43 | +func init() { |
| 44 | + metadata.Register(&audioMusePlugin{}) |
| 45 | + pdk.Log(pdk.LogInfo, fmt.Sprintf("[AudioMuse] Plugin registered successfully (id: %s)", pluginID)) |
| 46 | +} |
| 47 | + |
| 48 | +// Compile-time check that we implement the interface |
| 49 | +var _ metadata.SimilarSongsByTrackProvider = (*audioMusePlugin)(nil) |
| 50 | + |
| 51 | +// getConfigString retrieves a string config value with a default fallback |
| 52 | +func getConfigString(key, defaultValue string) string { |
| 53 | + if value, ok := pdk.GetConfig(key); ok && value != "" { |
| 54 | + return value |
| 55 | + } |
| 56 | + return defaultValue |
| 57 | +} |
| 58 | + |
| 59 | +// getConfigInt retrieves an integer config value with a default fallback |
| 60 | +func getConfigInt(key string, defaultValue int) int { |
| 61 | + if value, ok := pdk.GetConfig(key); ok && value != "" { |
| 62 | + if intVal, err := strconv.Atoi(value); err == nil { |
| 63 | + return intVal |
| 64 | + } |
| 65 | + } |
| 66 | + return defaultValue |
| 67 | +} |
| 68 | + |
| 69 | +// getConfigBool retrieves a boolean config value with a default fallback |
| 70 | +func getConfigBool(key string, defaultValue bool) bool { |
| 71 | + if value, ok := pdk.GetConfig(key); ok && value != "" { |
| 72 | + return value == "true" |
| 73 | + } |
| 74 | + return defaultValue |
| 75 | +} |
| 76 | + |
| 77 | +func (p *audioMusePlugin) GetSimilarSongsByTrack(input metadata.SimilarSongsByTrackRequest) (*metadata.SimilarSongsResponse, error) { |
| 78 | + pdk.Log(pdk.LogInfo, fmt.Sprintf("[AudioMuse] GetSimilarSongsByTrack called for track ID: %s, Name: %s, Artist: %s", input.ID, input.Name, input.Artist)) |
| 79 | + |
| 80 | + // Read configuration |
| 81 | + apiBaseURL := getConfigString(configAPIUrl, defaultAPIUrl) |
| 82 | + trackCount := getConfigInt(configTrackCount, defaultTrackCount) |
| 83 | + eliminateDuplicates := getConfigBool(configEliminateDuplicates, defaultEliminateDuplicates) |
| 84 | + radiusSimilarity := getConfigBool(configRadiusSimilarity, defaultRadiusSimilarity) |
| 85 | + |
| 86 | + pdk.Log(pdk.LogDebug, fmt.Sprintf("[AudioMuse] Config - API URL: %s, TrackCount: %d, EliminateDuplicates: %v, RadiusSimilarity: %v", |
| 87 | + apiBaseURL, trackCount, eliminateDuplicates, radiusSimilarity)) |
| 88 | + |
| 89 | + // Build the API URL with query parameters |
| 90 | + params := url.Values{} |
| 91 | + params.Set("item_id", input.ID) |
| 92 | + params.Set("n", strconv.Itoa(trackCount)) |
| 93 | + params.Set("eliminate_duplicates", strconv.FormatBool(eliminateDuplicates)) |
| 94 | + params.Set("radius_similarity", strconv.FormatBool(radiusSimilarity)) |
| 95 | + |
| 96 | + apiURL := fmt.Sprintf("%s/api/similar_tracks?%s", apiBaseURL, params.Encode()) |
| 97 | + |
| 98 | + pdk.Log(pdk.LogInfo, fmt.Sprintf("[AudioMuse] Calling API: %s", apiURL)) |
| 99 | + |
| 100 | + // Make HTTP GET request to AudioMuse-AI using PDK |
| 101 | + req := pdk.NewHTTPRequest(pdk.MethodGet, apiURL) |
| 102 | + resp := req.Send() |
| 103 | + |
| 104 | + pdk.Log(pdk.LogInfo, fmt.Sprintf("[AudioMuse] API response status: %d", resp.Status())) |
| 105 | + |
| 106 | + if resp.Status() != 200 { |
| 107 | + errMsg := fmt.Sprintf("[AudioMuse] ERROR: AudioMuse-AI returned status %d", resp.Status()) |
| 108 | + pdk.Log(pdk.LogError, errMsg) |
| 109 | + return nil, fmt.Errorf("AudioMuse-AI returned status %d", resp.Status()) |
| 110 | + } |
| 111 | + |
| 112 | + // Parse JSON response |
| 113 | + var tracks []audioMuseResponse |
| 114 | + body := resp.Body() |
| 115 | + pdk.Log(pdk.LogDebug, fmt.Sprintf("[AudioMuse] Response body length: %d bytes", len(body))) |
| 116 | + |
| 117 | + if err := json.Unmarshal(body, &tracks); err != nil { |
| 118 | + errMsg := fmt.Sprintf("[AudioMuse] ERROR: Failed to parse response: %v", err) |
| 119 | + pdk.Log(pdk.LogError, errMsg) |
| 120 | + return nil, fmt.Errorf("failed to parse AudioMuse-AI response: %w", err) |
| 121 | + } |
| 122 | + |
| 123 | + pdk.Log(pdk.LogInfo, fmt.Sprintf("[AudioMuse] Successfully parsed %d similar tracks", len(tracks))) |
| 124 | + |
| 125 | + |
| 126 | + // Sort tracks by distance ascending (smaller distance = more similar) |
| 127 | + sort.Slice(tracks, func(i, j int) bool { return tracks[i].Distance < tracks[j].Distance }) |
| 128 | + |
| 129 | + // Convert to Navidrome SongRef format preserving order |
| 130 | + songs := make([]metadata.SongRef, 0, len(tracks)) |
| 131 | + for _, track := range tracks { |
| 132 | + songs = append(songs, metadata.SongRef{ |
| 133 | + ID: track.ItemID, |
| 134 | + Name: track.Title, |
| 135 | + Artist: track.Author, |
| 136 | + Album: track.Album, |
| 137 | + }) |
| 138 | + } |
| 139 | + |
| 140 | + pdk.Log(pdk.LogInfo, fmt.Sprintf("[AudioMuse] Returning %d songs to Navidrome", len(songs))) |
| 141 | + |
| 142 | + return &metadata.SimilarSongsResponse{ |
| 143 | + Songs: songs, |
| 144 | + }, nil |
| 145 | +} |
| 146 | + |
| 147 | +func main() {} |
0 commit comments