|
| 1 | +package metadata |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/GerardPolloRebozado/navifetch/src/model" |
| 11 | + "github.com/GerardPolloRebozado/navifetch/src/util" |
| 12 | + "github.com/twoscott/gobble-fm/api" |
| 13 | + "github.com/twoscott/gobble-fm/lastfm" |
| 14 | +) |
| 15 | + |
| 16 | +type LastFMProvider struct { |
| 17 | + client *api.Client |
| 18 | + limit int |
| 19 | +} |
| 20 | + |
| 21 | +func NewLastFMProvider(apiKey string, limit int) *LastFMProvider { |
| 22 | + return &LastFMProvider{ |
| 23 | + client: api.NewClientKeyOnly(apiKey), |
| 24 | + limit: limit, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func (p *LastFMProvider) SearchSongs(_ context.Context, query string) ([]model.SubsonicSong, error) { |
| 29 | + params := lastfm.TrackSearchParams{ |
| 30 | + Track: query, |
| 31 | + Limit: uint(p.limit), |
| 32 | + } |
| 33 | + res, err := p.client.Track.Search(params) |
| 34 | + if err != nil { |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + |
| 38 | + songs := make([]model.SubsonicSong, 0) |
| 39 | + for _, t := range res.Tracks { |
| 40 | + songs = append(songs, p.toSubsonicSong(t.Title, t.Artist, t.MBID, "", "", 0)) |
| 41 | + } |
| 42 | + return songs, nil |
| 43 | +} |
| 44 | + |
| 45 | +func (p *LastFMProvider) SearchAlbums(_ context.Context, query string) ([]model.SubsonicAlbum, error) { |
| 46 | + params := lastfm.AlbumSearchParams{ |
| 47 | + Album: query, |
| 48 | + Limit: uint(p.limit), |
| 49 | + } |
| 50 | + res, err := p.client.Album.Search(params) |
| 51 | + if err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + |
| 55 | + albums := make([]model.SubsonicAlbum, 0) |
| 56 | + for _, a := range res.Albums { |
| 57 | + mbid := a.MBID |
| 58 | + if mbid == "" && a.Artist != "" && a.Title != "" { |
| 59 | + info, err := p.client.Album.Info(lastfm.AlbumInfoParams{ |
| 60 | + Artist: a.Artist, |
| 61 | + Album: a.Title, |
| 62 | + }) |
| 63 | + if err == nil && info != nil { |
| 64 | + mbid = info.MBID |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if mbid != "" { |
| 69 | + albums = append(albums, p.toSubsonicAlbum(a.Title, a.Artist, mbid, 0)) |
| 70 | + } |
| 71 | + } |
| 72 | + return albums, nil |
| 73 | +} |
| 74 | + |
| 75 | +func (p *LastFMProvider) GetAlbumSongs(_ context.Context, albumID string) ([]model.SubsonicSong, error) { |
| 76 | + albumID = strings.TrimPrefix(albumID, "external-") |
| 77 | + albumRes, err := p.client.Album.InfoByMBID(lastfm.AlbumInfoMBIDParams{MBID: albumID}) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + |
| 82 | + songs := make([]model.SubsonicSong, len(albumRes.Tracks)) |
| 83 | + var wg sync.WaitGroup |
| 84 | + for i, t := range albumRes.Tracks { |
| 85 | + wg.Add(1) |
| 86 | + go func(idx int, trackTitle, trackArtist string) { |
| 87 | + defer wg.Done() |
| 88 | + trackRes, err := p.client.Track.Info(lastfm.TrackInfoParams{ |
| 89 | + Artist: trackArtist, |
| 90 | + Track: trackTitle, |
| 91 | + }) |
| 92 | + if err == nil && trackRes != nil { |
| 93 | + songs[idx] = p.toSubsonicSong(trackRes.Title, trackRes.Artist.Name, trackRes.MBID, trackRes.Album.Title, trackRes.Album.MBID, int64(trackRes.Duration.Unwrap().Seconds())) |
| 94 | + } else { |
| 95 | + songs[idx] = p.toSubsonicSong(trackTitle, trackArtist, "", albumRes.Title, albumRes.MBID, 0) |
| 96 | + } |
| 97 | + }(i, t.Title, t.Artist.Name) |
| 98 | + } |
| 99 | + wg.Wait() |
| 100 | + |
| 101 | + return songs, nil |
| 102 | +} |
| 103 | + |
| 104 | +func (p *LastFMProvider) GetSong(_ context.Context, id string) (*model.SubsonicSong, error) { |
| 105 | + id = strings.TrimPrefix(id, "external-") |
| 106 | + res, err := p.client.Track.InfoByMBID(lastfm.TrackInfoMBIDParams{MBID: id}) |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + |
| 111 | + song := p.toSubsonicSong(res.Title, res.Artist.Name, res.MBID, res.Album.Title, res.Album.MBID, int64(res.Duration.Unwrap().Seconds())) |
| 112 | + return &song, nil |
| 113 | +} |
| 114 | + |
| 115 | +func (p *LastFMProvider) GetAlbum(_ context.Context, id string) (*model.SubsonicAlbum, error) { |
| 116 | + id = strings.TrimPrefix(id, "external-") |
| 117 | + res, err := p.client.Album.InfoByMBID(lastfm.AlbumInfoMBIDParams{MBID: id}) |
| 118 | + if err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + |
| 122 | + a := p.toSubsonicAlbum(res.Title, res.Artist, res.MBID, int64(len(res.Tracks))) |
| 123 | + return &a, nil |
| 124 | +} |
| 125 | + |
| 126 | +func (p *LastFMProvider) GetCoverArt(ctx context.Context, id string, _ int64) ([]byte, string, error) { |
| 127 | + id = strings.TrimPrefix(id, "external-") |
| 128 | + var imageURL string |
| 129 | + tInfo, err := p.client.Track.InfoByMBID(lastfm.TrackInfoMBIDParams{MBID: id}) |
| 130 | + if err == nil && tInfo != nil && tInfo.Album.Image.OriginalURL() != "" { |
| 131 | + imageURL = tInfo.Album.Image.OriginalURL() |
| 132 | + } else { |
| 133 | + aInfo, err := p.client.Album.InfoByMBID(lastfm.AlbumInfoMBIDParams{MBID: id}) |
| 134 | + if err == nil && aInfo != nil && aInfo.Image.OriginalURL() != "" { |
| 135 | + imageURL = aInfo.Image.OriginalURL() |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + if imageURL == "" { |
| 140 | + return nil, "", fmt.Errorf("no cover art found for ID: %s", id) |
| 141 | + } |
| 142 | + |
| 143 | + body, _, contentType, err := util.HTTPGet(ctx, imageURL, nil) |
| 144 | + return body, contentType, err |
| 145 | +} |
| 146 | + |
| 147 | +func (p *LastFMProvider) toSubsonicSong(title, artist, mbid, album, albumMBID string, duration int64) model.SubsonicSong { |
| 148 | + coverArtID := albumMBID |
| 149 | + if coverArtID == "" { |
| 150 | + coverArtID = mbid |
| 151 | + } |
| 152 | + |
| 153 | + return model.SubsonicSong{ |
| 154 | + ID: "external-" + mbid, |
| 155 | + Title: title + " (external)", |
| 156 | + Artist: artist, |
| 157 | + DisplayArtist: artist, |
| 158 | + Album: album, |
| 159 | + AlbumID: "external-" + albumMBID, |
| 160 | + CoverArt: "external-" + coverArtID, |
| 161 | + Duration: duration, |
| 162 | + IsDir: false, |
| 163 | + ContentType: "audio/mpeg", |
| 164 | + Suffix: "mp3", |
| 165 | + TranscodedSuffix: "mp3", |
| 166 | + TranscodedContentType: "audio/mpeg", |
| 167 | + Type: "music", |
| 168 | + MediaType: "song", |
| 169 | + Created: time.Now(), |
| 170 | + MusicBrainzId: mbid, |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +func (p *LastFMProvider) toSubsonicAlbum(title, artist, mbid string, songCount int64) model.SubsonicAlbum { |
| 175 | + return model.SubsonicAlbum{ |
| 176 | + ID: "external-" + mbid, |
| 177 | + Album: title, |
| 178 | + Title: title, |
| 179 | + Name: title, |
| 180 | + Artist: artist, |
| 181 | + CoverArt: "external-" + mbid, |
| 182 | + SongCount: songCount, |
| 183 | + IsDir: true, |
| 184 | + Created: time.Now(), |
| 185 | + } |
| 186 | +} |
0 commit comments