Skip to content

Commit

Permalink
Add configuration option for fetching album art
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriano Caloiaro committed Dec 15, 2020
1 parent 2d6d78d commit cf2887f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ A simple terminal UI player for [di.fm Premium](http://di.fm)

![App Screenshot](https://user-images.githubusercontent.com/3331648/81481515-bb668400-91fe-11ea-8a7c-39e1bb76c55d.png)

# Dependencies
# Dependencies

## PulseAudio

Expand All @@ -19,7 +19,7 @@ ln -s $(brew info pulseaudio | grep "/usr/local/Cellar" | awk '{print $1}')//hom
brew services start pulseaudio
```

### Linux
### Linux

`apt install pulseaudio`

Expand All @@ -42,16 +42,22 @@ If your `$GOPATH` is not set, see https://github.com/golang/go/wiki/SettingGOPAT

# Authenticate

There are two authentication options
There are two authentication options

- Enter your username and password directly into `di-tui`
- If you're justifiably uncomfortable with entering your username/password into this application, copy your "Listen Key" from (https://www.di.fm/settings) and create the following file:

## ~/.config/di-tui/config.yml
## ~/.config/di-tui/config.yml
```yml
token: <YOUR LISTEN KEY>
album_art: <BOOLEAN>
```
| key | description |
| --- | ----------- |
| token | Your di.fm authentication "Listen Key" found at https://www.di.fm/settings |
| album_art | Turn album art on or off |
# Run
`di-tui`
35 changes: 23 additions & 12 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"reflect"

"github.com/acaloiaro/di-tui/components"
"github.com/acaloiaro/di-tui/config"
"github.com/acaloiaro/di-tui/context"
"github.com/acaloiaro/di-tui/difm"
"github.com/michiwend/gomusicbrainz"
Expand All @@ -21,12 +22,16 @@ import (
// Art fetches album art for the given track, converts it to ASCII, and return the ASCII stringified album art
func Art(artist, track string) (art string) {

if !config.AlbumArt() {
return
}

// create a new WS2Client.
client, err := gomusicbrainz.NewWS2Client(
"https://musicbrainz.org/ws/2",
"di-tui",
"0.0.1",
"http://github.com/acaloiaro/di-tui")
"https://github.com/acaloiaro/di-tui")

if err != nil {
return
Expand All @@ -36,7 +41,7 @@ func Art(artist, track string) (art string) {
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 foudn for the artist (%s) and track (%s)", artist, track)
err = fmt.Errorf("no releases found for the artist (%s) and track (%s)", artist, track)
return
}

Expand Down Expand Up @@ -132,15 +137,21 @@ func TogglePause(ctx *context.AppContext) {
ctx.IsPlaying = !ctx.IsPlaying
}

// UpdateNowPlaying updates the application's now playing view with the currently playing channel
// UpdateNowPlaying updates the application's now playing view with the currently playing channel and album art
// Artist and track information are fetched separately from album art, allowing for a more responsive UI
func UpdateNowPlaying(chn *components.ChannelItem, ctx *context.AppContext) {
ctx.CurrentChannel = chn
cp := difm.GetCurrentlyPlaying(ctx)
albumArt := Art(cp.Track.Artist, cp.Track.Title)

ctx.View.App.QueueUpdateDraw(func() {
ctx.View.NowPlaying.Channel = chn
ctx.View.NowPlaying.Track = cp.Track
ctx.View.NowPlaying.Art = albumArt
})
go func() {
ctx.CurrentChannel = chn
cp := difm.GetCurrentlyPlaying(ctx)

ctx.View.App.QueueUpdateDraw(func() {
ctx.View.NowPlaying.Channel = chn
ctx.View.NowPlaying.Track = cp.Track
})

albumArt := Art(cp.Track.Artist, cp.Track.Title)
ctx.View.App.QueueUpdateDraw(func() {
ctx.View.NowPlaying.Art = albumArt
})
}()
}
18 changes: 13 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,25 @@ func init() {
viper.AddConfigPath("$HOME/.config/di-tui")
viper.AddConfigPath("$HOME/.di-tui/")
viper.AddConfigPath(".")

viper.SetDefault("album_art", true)

err := viper.ReadInConfig()
if err != nil {
saveConfig()
}
}

// AlbumArt returns true if album art should be fetched when a new song begins playing
func AlbumArt() bool {
return viper.GetBool("album_art")
}

// GetToken returns the di.fm API token if one is available
func GetToken() (token string) {
return viper.GetString("token")
}

// SaveToken persists the di.fm API token to disk
func SaveToken(token string) {
viper.Set("username", "")
Expand All @@ -29,11 +42,6 @@ func SaveToken(token string) {
saveConfig()
}

// GetToken returns the di.fm API token if one is available
func GetToken() (token string) {
return viper.GetString("token")
}

func saveConfig() {
viper.SetConfigFile(configFilePath())
viper.SetConfigType("yaml")
Expand Down

0 comments on commit cf2887f

Please sign in to comment.