Simpler album art fetching

This commit is contained in:
Cyber Oliveira 2024-01-08 21:35:52 -03:00
parent 7df5f3391d
commit 1eb812bb61
No known key found for this signature in database
GPG key ID: 185B193B89DF323E
4 changed files with 22 additions and 23 deletions

View file

@ -2,6 +2,7 @@ package app
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"image"
@ -17,41 +18,38 @@ import (
"github.com/acaloiaro/di-tui/context"
"github.com/acaloiaro/di-tui/difm"
"github.com/acaloiaro/di-tui/player"
"github.com/michiwend/gomusicbrainz"
"github.com/nfnt/resize"
)
// Art fetches album art for the given track, converts it to ASCII, and return the ASCII stringified album art
func Art(ctx *context.AppContext, artist, track string) (art string, err error) {
func Art(ctx *context.AppContext, cp components.Track) (art string, err error) {
if !config.AlbumArt() {
return
}
// create a new WS2Client.
client, err := gomusicbrainz.NewWS2Client(
"https://musicbrainz.org/ws/2",
"di-tui",
"0.0.1",
"https://github.com/acaloiaro/di-tui")
// download track details
var resp *http.Response
url := fmt.Sprintf("https://api.audioaddict.com/v1/di/tracks/%d", cp.ID)
resp, err = http.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
// search musicbrainz for a matching artist/track
searchString := fmt.Sprintf(`artist:"%s" release:"%s"`, artist, track)
mbResp, _ := client.SearchRelease(searchString, 1, -1)
if len(mbResp.Releases) == 0 {
err = fmt.Errorf("no releases found for the artist (%s) and track (%s)", artist, track)
body, err := io.ReadAll(resp.Body)
if err != nil || resp.StatusCode != 200 {
return
}
var trackDetails components.TrackDetails
json.Unmarshal(body, &trackDetails)
// fetch the album art and convert it to ascii
release := mbResp.Releases[0]
url := fmt.Sprintf("http://coverartarchive.org/release/%s/front", release.ID)
resp, err := http.Get(url)
url = fmt.Sprintf("https:%s", trackDetails.AlbumArtURL)
resp, err = http.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
img, format, err := image.Decode(resp.Body)
if format != "jpeg" && format != "png" || err != nil {
@ -155,7 +153,7 @@ func UpdateNowPlaying(ctx *context.AppContext, chn *components.ChannelItem) {
ctx.View.NowPlaying.Track = cp.Track
})
albumArt, err := Art(ctx, cp.Track.Artist, cp.Track.Title)
albumArt, err := Art(ctx, cp.Track)
if err != nil {
return
}

View file

@ -27,6 +27,7 @@ type CurrentlyPlaying struct {
// Track is metadata about a currently playing di.fm track
type Track struct {
ID int `json:"id"`
Artist string `json:"display_artist"`
Title string `json:"display_title"`
Duration float64 `json:"duration"`
@ -38,3 +39,9 @@ type StatusMessage struct {
Message string
Duration time.Duration
}
// TrackDetails is metadata about a currently playing di.fm track
type TrackDetails struct {
ID int `json:"id"`
AlbumArtURL string `json:"asset_url"`
}

2
go.mod
View file

@ -7,7 +7,6 @@ require (
github.com/gdamore/tcell/v2 v2.6.0
github.com/hajimehoshi/go-mp3 v0.2.1
github.com/jfreymuth/pulse v0.1.0
github.com/michiwend/gomusicbrainz v0.0.0-20181012083520-6c07e13dd396
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/rivo/tview v0.0.0-20230621164836-6cc0565babaf
github.com/spf13/pflag v1.0.5
@ -24,7 +23,6 @@ require (
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magiconair/properties v1.8.1 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/michiwend/golang-pretty v0.0.0-20141116172505-8ac61812ea3f // indirect
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/pelletier/go-toml v1.6.0 // indirect
github.com/rivo/uniseg v0.4.3 // indirect

4
go.sum
View file

@ -76,10 +76,6 @@ github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czP
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/michiwend/golang-pretty v0.0.0-20141116172505-8ac61812ea3f h1:oF0u/1VBm5gYitYvcIh443XZuYYkR50CP0Vsj3gAa1o=
github.com/michiwend/golang-pretty v0.0.0-20141116172505-8ac61812ea3f/go.mod h1:k0nOQg5bmAmEwWAuodvib9B74Oyny6aRMpkBcVWEtKs=
github.com/michiwend/gomusicbrainz v0.0.0-20181012083520-6c07e13dd396 h1:REpvt1iVqbiiBk2TihujngsSpHHMHiB/JBkQH/qkq8s=
github.com/michiwend/gomusicbrainz v0.0.0-20181012083520-6c07e13dd396/go.mod h1:HKpGCk/zijJ9GXdTWgtpnd5BbKDXtN0OQ3E4/zpxOwM=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=