Skip to content

Commit

Permalink
fix: improve album art grabbing
Browse files Browse the repository at this point in the history
now that i actually read the docs for the spotify api, clematis
will match the album art based on the track title and artist
instead of based on album. this will also work for singles!

there should no longer be any panics.
  • Loading branch information
TorchedSammy committed Oct 29, 2023
1 parent 1516da9 commit cf4017a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
2 changes: 1 addition & 1 deletion artfetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package main
import "github.com/godbus/dbus/v5"

type artFetcher interface{
getAlbumArt(artist, album string, metadata map[string]dbus.Variant) string
getAlbumArt(artist, album, title string, metadata map[string]dbus.Variant) string
}
2 changes: 1 addition & 1 deletion discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var receivedAssets []Asset

type discordFetcher struct{}

func (discordFetcher) getAlbumArt(artist, album string, mdata map[string]dbus.Variant) string {
func (discordFetcher) getAlbumArt(artist, album, title string, mdata map[string]dbus.Variant) string {
artFile := ""
if artUrl, ok := mdata["mpris:artUrl"].Value().(string); ok {
artFile, _ = url.PathUnescape(artUrl)
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,13 @@ func setPresence(metadata map[string]dbus.Variant, songstamp time.Time, player *

artistsStr := ""
if artistsArr, ok := metadata["xesam:artist"].Value().([]string); ok {
artistsStr = "by " + strings.Join(artistsArr, ", ")
artistsStr = strings.Join(artistsArr, ", ")
}

url := fetcher.getAlbumArt(artistsStr, albumName, metadata)
url := fetcher.getAlbumArt(artistsStr, albumName, title, metadata)

args := []string{
"{artist}", artistsStr,
"{artist}", "by " + artistsStr,
"{title}", title,
"{album}", album,
}
Expand Down
42 changes: 29 additions & 13 deletions spotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"fmt"
"io"
"os"
"net/http"
"net/url"
// "sort"
Expand All @@ -31,14 +32,18 @@ type spotifyAccess struct{
}

type spotifySeach struct{
Albums spotifyAlbums
Tracks spotifyTrack `json:"tracks"`
}

type spotifyAlbums struct{
Items []spotifyItem
type spotifyTrack struct{
Items []spotifyTrackObject `json:"items"`
}

type spotifyItem struct{
type spotifyTrackObject struct{
Album spotifyAlbum `json:"album"`
}

type spotifyAlbum struct{
Images []spotifyArt
}

Expand All @@ -48,27 +53,32 @@ type spotifyArt struct {
URL string
}

func (spotifyFetcher) getAlbumArt(artist, album string, mdata map[string]dbus.Variant) string {
artUrl, _ := url.Parse(fmt.Sprintf("%s/search?q=%s&type=album&limit=1", art_endpoint, url.QueryEscape(artist + " " + album)))
func handleSpotErr(err error) string {
fmt.Fprintln(os.Stderr, err)
return "music"
}

func (spotifyFetcher) getAlbumArt(artist, album, title string, mdata map[string]dbus.Variant) string {
spotSearchQuery := url.PathEscape(url.QueryEscape(fmt.Sprintf("track:%s artist:%s", title, artist)))
artUrl, _ := url.Parse(fmt.Sprintf("%s/search?q=%s&type=track&limit=1", art_endpoint, spotSearchQuery))
authUrl, _ := url.Parse(auth_endpoint)

req, err := http.NewRequest("POST", authUrl.String(), strings.NewReader("grant_type=client_credentials"))
if err != nil {
// TODO: dont panic
panic(err)
return handleSpotErr(err)
}
req.Header.Add("Authorization", "Basic " + base64.StdEncoding.EncodeToString([]byte(art_api_auth)))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
return handleSpotErr(err)
}

body, err := io.ReadAll(resp.Body)
spot := &spotifyAccess{}
if err := json.Unmarshal(body, &spot); err != nil {
panic(err)
return handleSpotErr(err)
}

req, err = http.NewRequest("GET", artUrl.String(), strings.NewReader(""))
Expand All @@ -77,18 +87,24 @@ func (spotifyFetcher) getAlbumArt(artist, album string, mdata map[string]dbus.Va

resp, err = http.DefaultClient.Do(req)
if err != nil {
panic(err)
return handleSpotErr(err)
}

body, err = io.ReadAll(resp.Body)
logger.Debug(string(body))

spotifyData := &spotifySeach{}
if err := json.Unmarshal(body, &spotifyData); err != nil {
panic(err)
return handleSpotErr(err)
}

if len(spotifyData.Tracks.Items) == 0 {
// nothing found
fmt.Fprintln(os.Stderr, "Nothing found on spotify for %s", album)
return "music"
}

images := spotifyData.Albums.Items[0].Images
images := spotifyData.Tracks.Items[0].Album.Images
// may or may not be needed (will see)
/*
sort.Slice(images, func(i, j int) bool {
Expand Down

0 comments on commit cf4017a

Please sign in to comment.