Merge pull request #6 from aditya-K2/lastfm

Connecting LastFm Api to get Image Urls which is then rendered
This commit is contained in:
Aditya Kurdunkar 2021-11-13 22:02:56 +05:30 committed by GitHub
commit b6936d7300
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 205 additions and 85 deletions

5
App.go
View File

@ -1,7 +1,6 @@
package main
import (
"github.com/fhs/gompd/mpd"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
@ -16,9 +15,9 @@ type Application struct {
pBar *progressBar
}
func newApplication(conn mpd.Client, r *Renderer) *Application {
func newApplication(r *Renderer) *Application {
var pBar *progressBar = newProgressBar(conn, r)
var pBar *progressBar = newProgressBar(r)
expandedView := tview.NewTable()
Navbar := tview.NewTable()
searchBar := tview.NewTable()

View File

@ -12,6 +12,7 @@ https://user-images.githubusercontent.com/51816057/140478368-5b724b2f-2499-4150-
- [x] Add a config parser
- [x] Image Previews
- [x] User Key Mappings
- [x] Querying LastFM API for getting Album Art
- [ ] Fuzzy Searching
- [ ] Visual Mode (like vim) for updating playlists
@ -83,6 +84,31 @@ Following functions are provided :
| updateDB |
| deleteSongFromPlaylist |
## Getting Album Art from [LastFm API](https://www.last.fm/api)
1. Generate API Key [here](https://www.last.fm/login?next=%2Fapi%2Faccount%2Fcreate%3F_pjax%3D%2523content)
![Screenshot from 2021-11-13 21-54-54](https://user-images.githubusercontent.com/51816057/141651276-f76a5c7f-65fe-4a1a-b130-18cdf67dd471.png)
2. Add the api key and api secret to config.yml
```yml
GET_COVER_ART_FROM_LAST_FM : "TRUE" # Turn On Getting Album art from lastfm api
LASTFM_API_KEY: "YOUR API KEY HERE"
LASTFM_API_SECRET: "YOUR API SECRET HERE"
```
3. Auto correct
![Screenshot from 2021-11-13 21-59-46](https://user-images.githubusercontent.com/51816057/141651414-1586577a-cab2-48e2-a24b-1053f8634fbe.png)
:
```yml
LASTFM_AUTO_CORRECT: 1 # 0 means it is turned off
```
### Tested on following terminals:
- Alacritty

41
cache/cache.go vendored Normal file
View File

@ -0,0 +1,41 @@
package cache
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
var (
CACHE_LIST map[[2]string]string = make(map[[2]string]string)
)
func LoadCache(path string) {
cacheFileContent, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println("Could Not Read From Cache File")
}
lineSlice := strings.Split(string(cacheFileContent), "\n")
for _, line := range lineSlice {
if len(line) != 0 {
param := strings.Split(line, "\t")
if len(param) == 3 {
CACHE_LIST[[2]string{param[0], param[1]}] = param[2]
}
}
}
}
func WriteToCache(path string) {
b, err := os.Create(path)
if err == nil {
for k, v := range CACHE_LIST {
b.Write([]byte(fmt.Sprintf("%s\t%s\t%s\n", k[0], k[1], v)))
}
}
}
func GenerateName(artist, album string) string {
return fmt.Sprintf("%s-%s.jpg", artist, album)
}

16
cache/cache_test.go vendored Normal file
View File

@ -0,0 +1,16 @@
package cache
import "testing"
func TestLoadCache(t *testing.T) {
expectedResult := [2]string{"hello/wer.jpg", "hello/iwer.jpg"}
loadCache("./testdata/cache.txt")
var i int = 0
for _, v := range CACHE_LIST {
if v != expectedResult[i] {
if v != expectedResult[i+1] {
t.Errorf("Didn't Get The Expected Value receieved %s", v)
}
}
}
}

2
cache/testdata/cache.txt vendored Normal file
View File

@ -0,0 +1,2 @@
Kanye West Life Of Pablo hello/wer.jpg
Niravana Heart Shaped Box hello/iwer.jpg

View File

@ -3,10 +3,7 @@ package main
import (
"strings"
"github.com/fhs/gompd/mpd"
// "github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
// "fmt"
)
func getFormattedString(s string, width int) string {
@ -19,18 +16,18 @@ func getFormattedString(s string, width int) string {
return s
}
func togglePlayBack(connection mpd.Client) error {
status, err := connection.Status()
func togglePlayBack() error {
status, err := CONN.Status()
if status["state"] == "play" && err == nil {
connection.Pause(true)
CONN.Pause(true)
} else if status["state"] == "pause" && err == nil {
connection.Play(-1)
CONN.Play(-1)
}
return err
}
func UpdatePlaylist(conn mpd.Client, inputTable *tview.Table) {
_playlistAttr, _ := conn.PlaylistInfo(-1, -1)
func UpdatePlaylist(inputTable *tview.Table) {
_playlistAttr, _ := CONN.PlaylistInfo(-1, -1)
inputTable.Clear()
for i, j := range _playlistAttr {
@ -55,11 +52,11 @@ func join(stringSlice []string) string {
return _s
}
func Update(conn mpd.Client, f []FileNode, inputTable *tview.Table) {
func Update(f []FileNode, inputTable *tview.Table) {
inputTable.Clear()
for i, j := range f {
if len(j.children) == 0 {
_songAttributes, err := conn.ListAllInfo(j.absolutePath)
_songAttributes, err := CONN.ListAllInfo(j.absolutePath)
if err == nil && _songAttributes[0]["Title"] != "" {
_, _, w, _ := inputTable.GetInnerRect()
inputTable.SetCell(i, 0,

28
go.mod
View File

@ -13,30 +13,4 @@ require (
gitlab.com/diamondburned/ueberzug-go v0.0.0-20190521043425-7c15a5f63b06
)
require (
github.com/BurntSushi/freetype-go v0.0.0-20160129220410-b763ddbfe298 // indirect
github.com/BurntSushi/graphics-go v0.0.0-20160129215708-b43f31a4a966 // indirect
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802 // indirect
github.com/BurntSushi/xgbutil v0.0.0-20160919175755-f7c97cef3b4e // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/icza/bitio v1.0.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mewkiz/pkg v0.0.0-20190919212034-518ade7978e2 // indirect
github.com/mitchellh/mapstructure v1.4.2 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
golang.org/x/text v0.3.6 // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
require github.com/shkh/lastfm-go v0.0.0-20191215035245-89a801c244e0

2
go.sum
View File

@ -264,6 +264,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sagikazarmark/crypt v0.1.0/go.mod h1:B/mN0msZuINBtQ1zZLEQcegFJJf9vnYIR88KRMEuODE=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/shkh/lastfm-go v0.0.0-20191215035245-89a801c244e0 h1:cgqwZtnR+IQfUYDLJ3Kiy4aE+O/wExTzEIg8xwC4Qfs=
github.com/shkh/lastfm-go v0.0.0-20191215035245-89a801c244e0/go.mod h1:n3nudMl178cEvD44PaopxH9jhJaQzthSxUzLO5iKMy4=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY=
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=

53
lastfm.go Normal file
View 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")
}

67
main.go
View File

@ -1,7 +1,6 @@
package main
import (
"log"
"strconv"
"time"
@ -11,6 +10,7 @@ import (
"github.com/spf13/viper"
)
var CONN *mpd.Client
var Volume int64
var Random bool
var Repeat bool
@ -19,37 +19,38 @@ var InsidePlaylist bool = true
func main() {
config.ReadConfig()
// Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:"+viper.GetString("MPD_PORT"))
if err != nil {
log.Fatalln(err)
var mpdConnectionError error
CONN, mpdConnectionError = mpd.Dial("tcp", "localhost:"+viper.GetString("MPD_PORT"))
if mpdConnectionError != nil {
panic(mpdConnectionError)
}
defer conn.Close()
defer CONN.Close()
r := newRenderer()
c, _ := conn.CurrentSong()
c, _ := CONN.CurrentSong()
if len(c) != 0 {
r.Start(viper.GetString("MUSIC_DIRECTORY") + c["file"])
r.Start(c["file"])
} else {
r.Start("stop")
}
UI := newApplication(*conn, r)
UI := newApplication(r)
fileMap, err := conn.GetFiles()
fileMap, err := CONN.GetFiles()
dirTree := generateDirectoryTree(fileMap)
UpdatePlaylist(*conn, UI.expandedView)
UpdatePlaylist(UI.expandedView)
_v, _ := conn.Status()
_v, _ := CONN.Status()
Volume, _ = strconv.ParseInt(_v["volume"], 10, 64)
Random, _ = strconv.ParseBool(_v["random"])
Repeat, _ = strconv.ParseBool(_v["repeat"])
UI.expandedView.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) {
if InsidePlaylist {
UpdatePlaylist(*conn, UI.expandedView)
UpdatePlaylist(UI.expandedView)
} else {
Update(*conn, dirTree.children, UI.expandedView)
Update(dirTree.children, UI.expandedView)
}
return UI.expandedView.GetInnerRect()
})
@ -59,53 +60,53 @@ func main() {
r, _ := UI.expandedView.GetSelection()
if !InsidePlaylist {
if len(dirTree.children[r].children) == 0 {
id, _ := conn.AddId(dirTree.children[r].absolutePath, -1)
conn.PlayId(id)
id, _ := CONN.AddId(dirTree.children[r].absolutePath, -1)
CONN.PlayId(id)
} else {
Update(*conn, dirTree.children[r].children, UI.expandedView)
Update(dirTree.children[r].children, UI.expandedView)
dirTree = &dirTree.children[r]
}
} else {
conn.Play(r)
CONN.Play(r)
}
},
"togglePlayBack": func() {
togglePlayBack(*conn)
togglePlayBack()
},
"showParentContent": func() {
if !InsidePlaylist {
if dirTree.parent != nil {
Update(*conn, dirTree.parent.children, UI.expandedView)
Update(dirTree.parent.children, UI.expandedView)
dirTree = dirTree.parent
}
}
},
"nextSong": func() {
conn.Next()
CONN.Next()
},
"clearPlaylist": func() {
conn.Clear()
CONN.Clear()
if InsidePlaylist {
UpdatePlaylist(*conn, UI.expandedView)
UpdatePlaylist(UI.expandedView)
}
},
"previousSong": func() {
conn.Previous()
CONN.Previous()
},
"addToPlaylist": func() {
if !InsidePlaylist {
r, _ := UI.expandedView.GetSelection()
conn.Add(dirTree.children[r].absolutePath)
CONN.Add(dirTree.children[r].absolutePath)
}
},
"toggleRandom": func() {
err := conn.Random(!Random)
err := CONN.Random(!Random)
if err == nil {
Random = !Random
}
},
"toggleRepeat": func() {
err := conn.Repeat(!Repeat)
err := CONN.Repeat(!Repeat)
if err == nil {
Repeat = !Repeat
}
@ -116,7 +117,7 @@ func main() {
} else {
Volume -= 10
}
conn.SetVolume(int(Volume))
CONN.SetVolume(int(Volume))
},
"increaseVolume": func() {
if Volume >= 100 {
@ -124,17 +125,17 @@ func main() {
} else {
Volume += 10
}
conn.SetVolume(int(Volume))
CONN.SetVolume(int(Volume))
},
"navigateToFiles": func() {
InsidePlaylist = false
UI.Navbar.Select(1, 0)
Update(*conn, dirTree.children, UI.expandedView)
Update(dirTree.children, UI.expandedView)
},
"navigateToPlaylist": func() {
InsidePlaylist = true
UI.Navbar.Select(0, 0)
UpdatePlaylist(*conn, UI.expandedView)
UpdatePlaylist(UI.expandedView)
},
"navigateToMostPlayed": func() {
InsidePlaylist = false
@ -144,10 +145,10 @@ func main() {
UI.App.Stop()
},
"stop": func() {
conn.Stop()
CONN.Stop()
},
"updateDB": func() {
_, err = conn.Update("")
_, err = CONN.Update("")
if err != nil {
panic(err)
}
@ -155,7 +156,7 @@ func main() {
"deleteSongFromPlaylist": func() {
if InsidePlaylist {
r, _ := UI.expandedView.GetSelection()
conn.Delete(r, -1)
CONN.Delete(r, -1)
}
},
}

View File

@ -4,10 +4,8 @@ import (
"fmt"
"strconv"
"github.com/fhs/gompd/mpd"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/spf13/viper"
)
var CurrentSong string
@ -26,7 +24,7 @@ type progressBar struct {
// This Function returns a progressBar with a table of two rows
// the First row will contain information about the current Song
// and the Second one will contain the progressBar
func newProgressBar(conn mpd.Client, r *Renderer) *progressBar {
func newProgressBar(r *Renderer) *progressBar {
p := progressBar{}
a := tview.NewTable().
@ -37,8 +35,8 @@ func newProgressBar(conn mpd.Client, r *Renderer) *progressBar {
a.SetBorder(true)
a.SetDrawFunc(func(s tcell.Screen, x, y, width, height int) (int, int, int, int) {
p.updateTitle(conn, r)
p.updateProgress(conn)
p.updateTitle(r)
p.updateProgress()
return p.t.GetInnerRect()
})
@ -48,8 +46,8 @@ func newProgressBar(conn mpd.Client, r *Renderer) *progressBar {
return &p
}
func (s *progressBar) updateTitle(conn mpd.Client, r *Renderer) {
_currentAttributes, err := conn.CurrentSong()
func (s *progressBar) updateTitle(r *Renderer) {
_currentAttributes, err := CONN.CurrentSong()
if err == nil {
song := "[green::bi]" + _currentAttributes["Title"] + "[-:-:-] - " + "[blue::b]" + _currentAttributes["Artist"] + "\n"
s.t.GetCell(0, 0).Text = song
@ -57,15 +55,15 @@ func (s *progressBar) updateTitle(conn mpd.Client, r *Renderer) {
CurrentSong = ""
r.Send("stop")
} else if song != CurrentSong && len(_currentAttributes) != 0 {
r.Send(viper.GetString("music_directory") + _currentAttributes["file"])
r.Send(_currentAttributes["file"])
CurrentSong = song
}
// fmt.Println(len(_currentAttributes))
}
}
func (s *progressBar) updateProgress(conn mpd.Client) {
_status, err := conn.Status()
func (s *progressBar) updateProgress() {
_status, err := CONN.Status()
_, _, _width, _ := s.t.GetInnerRect()
el, err1 := strconv.ParseFloat(_status["elapsed"], 8)
du, err := strconv.ParseFloat(_status["duration"], 8)

View File

@ -43,7 +43,18 @@ func openImage(path string, c chan string) {
fw, fh := getFontWidth()
var im *ueberzug.Image
if path != "stop" {
img2, _ := getImg(extractImageFromFile(path))
absPath := viper.GetString("MUSIC_DIRECTORY") + path
extractedImage := extractImageFromFile(absPath)
if extractedImage == viper.GetString("DEFAULT_IMAGE_PATH") && viper.GetString("GET_COVER_ART_FROM_LAST_FM") == "TRUE" {
a, err := CONN.ListInfo(path)
if err == nil && len(a) != 0 {
downloadedImage, err := getImageFromLastFM(a[0]["artist"], a[0]["album"])
if err == nil {
extractedImage = downloadedImage
}
}
}
img2, _ := getImg(extractedImage)
im, _ = ueberzug.NewImage(img2, int(float32(IMG_X)*fw)+viper.GetInt("ADDITIONAL_PADDING_X"), int(float32(IMG_Y)*fh)+viper.GetInt("ADDITIONAL_PADDING_Y"))
}
d := <-c