Connecting LastFm Api to get Image Urls which is then rendered
This commit is contained in:
parent
cd2acb8a7c
commit
c19a053126
53
lastfm.go
Normal file
53
lastfm.go
Normal file
@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
_ "image/jpeg"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/shkh/lastfm-go/lastfm"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func getImageFromLastFM(artist, album string) (string, error) {
|
||||
api := lastfm.New(viper.GetString("LASTFM_API_KEY"), viper.GetString("LASTFM_API_SECRET"))
|
||||
v, err := api.Album.GetInfo(map[string]interface{}{
|
||||
"artist": artist,
|
||||
"album": album,
|
||||
"autocorrect": viper.GetInt("LASTFM_AUTO_CORRECT"),
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
} else {
|
||||
return downloadImage(v.Images[len(v.Images)-1].Url)
|
||||
}
|
||||
}
|
||||
|
||||
func downloadImage(url string) (string, error) {
|
||||
var reader io.Reader
|
||||
if strings.HasPrefix(url, "http") {
|
||||
r, err := http.Get(url)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer r.Body.Close()
|
||||
reader = r.Body
|
||||
v, err := io.ReadAll(reader)
|
||||
if err == nil {
|
||||
b, err := os.Create(viper.GetString("COVER_IMAGE_PATH"))
|
||||
if err == nil {
|
||||
b.Write(v)
|
||||
return viper.GetString("COVER_IMAGE_PATH"), nil
|
||||
} else {
|
||||
b.Close()
|
||||
return "", err
|
||||
}
|
||||
} else {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return "", errors.New("Image Not Received")
|
||||
}
|
Loading…
Reference in New Issue
Block a user