2021-12-22 07:26:57 -07:00
|
|
|
package client
|
2021-10-09 05:51:47 -06:00
|
|
|
|
|
|
|
import (
|
2021-11-14 02:06:24 -07:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-12-24 03:07:21 -07:00
|
|
|
|
2022-09-01 13:55:33 -06:00
|
|
|
"github.com/aditya-K2/fuzzy"
|
|
|
|
"github.com/aditya-K2/gomp/notify"
|
2022-09-08 13:16:38 -06:00
|
|
|
"github.com/fhs/gompd/v2/mpd"
|
2021-12-22 07:26:57 -07:00
|
|
|
|
2021-10-18 00:42:10 -06:00
|
|
|
"strings"
|
2021-10-09 05:51:47 -06:00
|
|
|
)
|
|
|
|
|
2021-11-16 05:33:36 -07:00
|
|
|
var (
|
2022-09-01 13:55:33 -06:00
|
|
|
Conn *mpd.Client
|
2021-12-22 07:26:57 -07:00
|
|
|
ArtistTree map[string]map[string]map[string]string
|
2022-09-01 13:55:33 -06:00
|
|
|
WHITE_AND_BOLD string = "[white::b]"
|
|
|
|
DirTree *FileNode
|
|
|
|
Matches fuzzy.Matches
|
|
|
|
SearchContentSlice []interface{}
|
2021-11-16 05:33:36 -07:00
|
|
|
)
|
|
|
|
|
2021-12-22 07:26:57 -07:00
|
|
|
func TogglePlayBack() error {
|
2022-09-01 13:55:33 -06:00
|
|
|
status, err := Conn.Status()
|
2021-10-17 10:12:02 -06:00
|
|
|
if status["state"] == "play" && err == nil {
|
2022-09-01 13:55:33 -06:00
|
|
|
Conn.Pause(true)
|
2021-10-17 10:12:02 -06:00
|
|
|
} else if status["state"] == "pause" && err == nil {
|
2022-09-01 13:55:33 -06:00
|
|
|
Conn.Play(-1)
|
2021-10-09 05:51:47 -06:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2021-10-14 10:32:29 -06:00
|
|
|
|
2021-12-24 03:07:21 -07:00
|
|
|
// The GenerateContentSlice returns a slice of the content to be displayed on the Search View. The Slice is generated
|
|
|
|
// because the random nature of maps as they return values randomly hence the draw function keeps changing the order
|
|
|
|
// in which the results appear.
|
2021-11-16 02:47:50 -07:00
|
|
|
func GenerateContentSlice(selectedSuggestion string) ([]interface{}, error) {
|
|
|
|
var ContentSlice []interface{}
|
|
|
|
if strings.TrimRight(selectedSuggestion, " ") == "" {
|
2022-09-01 13:55:33 -06:00
|
|
|
notify.Notify.Send("Empty Search!")
|
2021-11-16 02:47:50 -07:00
|
|
|
return nil, errors.New("empty Search String Provided")
|
|
|
|
}
|
2021-12-22 07:26:57 -07:00
|
|
|
if _, ok := ArtistTree[selectedSuggestion]; ok {
|
2021-11-16 05:33:36 -07:00
|
|
|
ContentSlice = append(ContentSlice, WHITE_AND_BOLD+"Artists :")
|
2021-11-16 02:47:50 -07:00
|
|
|
ContentSlice = append(ContentSlice, selectedSuggestion)
|
2021-11-16 05:33:36 -07:00
|
|
|
ContentSlice = append(ContentSlice, WHITE_AND_BOLD+"Artist Albums :")
|
2021-12-22 07:26:57 -07:00
|
|
|
for albumName := range ArtistTree[selectedSuggestion] {
|
2021-11-16 02:47:50 -07:00
|
|
|
ContentSlice = append(ContentSlice, [2]string{albumName, selectedSuggestion})
|
|
|
|
}
|
2021-11-16 05:33:36 -07:00
|
|
|
ContentSlice = append(ContentSlice, WHITE_AND_BOLD+"Artist Tracks :")
|
2021-12-22 07:26:57 -07:00
|
|
|
for albumName, trackList := range ArtistTree[selectedSuggestion] {
|
2021-11-16 02:47:50 -07:00
|
|
|
for track := range trackList {
|
|
|
|
ContentSlice = append(ContentSlice, [3]string{track, selectedSuggestion, albumName})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-22 07:26:57 -07:00
|
|
|
if aMap := QueryArtistTreeForAlbums(ArtistTree, selectedSuggestion); len(aMap) != 0 {
|
2021-11-16 05:33:36 -07:00
|
|
|
ContentSlice = append(ContentSlice, WHITE_AND_BOLD+"Albums :")
|
2021-11-16 02:47:50 -07:00
|
|
|
for mSlice := range aMap {
|
|
|
|
ContentSlice = append(ContentSlice, mSlice)
|
|
|
|
}
|
2021-11-16 05:33:36 -07:00
|
|
|
ContentSlice = append(ContentSlice, WHITE_AND_BOLD+"Album Tracks :")
|
2021-11-16 02:47:50 -07:00
|
|
|
for a, pathSlice := range aMap {
|
|
|
|
for _, path := range pathSlice {
|
2021-11-17 02:36:18 -07:00
|
|
|
ContentSlice = append(ContentSlice, [3]string{path[0], a[1], a[0]})
|
2021-11-16 02:47:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-22 07:26:57 -07:00
|
|
|
if tMap := QueryArtistTreeForTracks(ArtistTree, selectedSuggestion); len(tMap) != 0 {
|
2021-11-16 05:33:36 -07:00
|
|
|
ContentSlice = append(ContentSlice, WHITE_AND_BOLD+"Tracks :")
|
2021-11-16 02:47:50 -07:00
|
|
|
for mSlice := range tMap {
|
|
|
|
ContentSlice = append(ContentSlice, mSlice)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ContentSlice, nil
|
|
|
|
}
|
|
|
|
|
2021-12-24 03:07:21 -07:00
|
|
|
// GenerateArtistTree Artist Tree is a map of Artist to their Album Map
|
|
|
|
// Album Tree is a map of the tracks in that particular album.
|
2021-11-14 02:06:24 -07:00
|
|
|
func GenerateArtistTree() (map[string]map[string]map[string]string, error) {
|
2021-12-22 07:26:57 -07:00
|
|
|
ArtistTree = make(map[string]map[string]map[string]string)
|
2022-09-01 13:55:33 -06:00
|
|
|
AllInfo, err := Conn.ListAllInfo("/")
|
2021-11-14 02:06:24 -07:00
|
|
|
if err == nil {
|
|
|
|
for _, i := range AllInfo {
|
|
|
|
if _, ArtistExists := ArtistTree[i["Artist"]]; !ArtistExists {
|
|
|
|
ArtistTree[i["Artist"]] = make(map[string]map[string]string)
|
|
|
|
}
|
|
|
|
if _, AlbumExists := ArtistTree[i["Artist"]][i["Album"]]; !AlbumExists {
|
|
|
|
ArtistTree[i["Artist"]][i["Album"]] = make(map[string]string)
|
|
|
|
}
|
|
|
|
if _, TitleExists := ArtistTree[i["Artist"]][i["Album"]][i["Title"]]; !TitleExists {
|
|
|
|
ArtistTree[i["Artist"]][i["Album"]][i["Title"]] = i["file"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ArtistTree, nil
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("Could Not Generate Artist Tree")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrintArtistTree(a map[string]map[string]map[string]string) {
|
|
|
|
for k, v := range a {
|
|
|
|
fmt.Println(k, " : ")
|
|
|
|
for k1, v1 := range v {
|
|
|
|
fmt.Println("\t|---", k1, " : ")
|
|
|
|
for k2 := range v1 {
|
|
|
|
fmt.Println("\t\t|---", k2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-15 11:46:15 -07:00
|
|
|
|
2021-12-24 03:07:21 -07:00
|
|
|
// Adds All tracks from a specified album to a playlist
|
2021-11-16 05:09:44 -07:00
|
|
|
func AddAlbum(a map[string]map[string]map[string]string, alb string, artist string) {
|
|
|
|
for _, v := range a[artist][alb] {
|
2022-09-01 13:55:33 -06:00
|
|
|
err := Conn.Add(v)
|
2021-11-16 05:09:44 -07:00
|
|
|
if err != nil {
|
2022-09-01 13:55:33 -06:00
|
|
|
notify.Notify.Send("Could Not Add Song : " + v)
|
2021-11-15 11:46:15 -07:00
|
|
|
}
|
|
|
|
}
|
2022-09-01 13:55:33 -06:00
|
|
|
notify.Notify.Send("Album Added : " + alb)
|
2021-11-15 11:46:15 -07:00
|
|
|
}
|
|
|
|
|
2021-12-24 03:07:21 -07:00
|
|
|
// Adds All tracks from a specified artist to a playlist
|
2021-11-15 11:46:15 -07:00
|
|
|
func AddArtist(a map[string]map[string]map[string]string, artist string) {
|
|
|
|
if val, ok := a[artist]; ok {
|
|
|
|
for _, v := range val {
|
|
|
|
for _, path := range v {
|
2022-09-01 13:55:33 -06:00
|
|
|
err := Conn.Add(path)
|
2021-11-16 02:47:50 -07:00
|
|
|
if err != nil {
|
2022-09-01 13:55:33 -06:00
|
|
|
notify.Notify.Send("Could Not Add Song : " + path)
|
2021-11-16 02:47:50 -07:00
|
|
|
}
|
2021-11-15 11:46:15 -07:00
|
|
|
}
|
|
|
|
}
|
2022-09-01 13:55:33 -06:00
|
|
|
notify.Notify.Send("Artist Added : " + artist)
|
2021-11-15 11:46:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-24 03:07:21 -07:00
|
|
|
// Adds Specified Track to the Playlist
|
2021-11-16 07:59:46 -07:00
|
|
|
func AddTitle(a map[string]map[string]map[string]string, artist, alb, track string, addAndPlay bool) {
|
|
|
|
if addAndPlay {
|
2022-09-08 13:16:38 -06:00
|
|
|
id, err := Conn.AddID(a[artist][alb][track], -1)
|
|
|
|
Conn.PlayID(id)
|
2021-11-16 07:59:46 -07:00
|
|
|
if err != nil {
|
2022-09-01 13:55:33 -06:00
|
|
|
notify.Notify.Send("Could Not Add Track : " + track)
|
2021-11-16 07:59:46 -07:00
|
|
|
}
|
|
|
|
} else {
|
2022-09-01 13:55:33 -06:00
|
|
|
err := Conn.Add(a[artist][alb][track])
|
2021-11-16 07:59:46 -07:00
|
|
|
if err != nil {
|
2022-09-01 13:55:33 -06:00
|
|
|
notify.Notify.Send("Could Not Add Track : " + track)
|
2021-11-16 07:59:46 -07:00
|
|
|
}
|
2021-11-16 02:47:50 -07:00
|
|
|
}
|
2022-09-01 13:55:33 -06:00
|
|
|
notify.Notify.Send("Track Added : " + track)
|
2021-11-16 02:47:50 -07:00
|
|
|
}
|
|
|
|
|
2021-12-22 07:26:57 -07:00
|
|
|
/* Querys the Artist Tree for a track and returns a TrackMap (i.e [3]string{artist, album, track} -> Path) which will help us
|
2021-11-16 02:47:50 -07:00
|
|
|
to add tracks to the playlist */
|
|
|
|
func QueryArtistTreeForTracks(a map[string]map[string]map[string]string, track string) map[[3]string]string {
|
2021-11-15 11:46:15 -07:00
|
|
|
TrackMap := make(map[[3]string]string)
|
|
|
|
for artistName, albumMap := range a {
|
2021-11-16 07:30:04 -07:00
|
|
|
for albumName, trackList := range albumMap {
|
2021-11-15 11:46:15 -07:00
|
|
|
for trackName, path := range trackList {
|
|
|
|
if trackName == track {
|
2021-11-16 07:30:04 -07:00
|
|
|
TrackMap[[3]string{trackName, artistName, albumName}] = path
|
2021-11-15 11:46:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TrackMap
|
|
|
|
}
|
2021-11-16 02:47:50 -07:00
|
|
|
|
2021-12-22 07:26:57 -07:00
|
|
|
/* Querys the Artist Tree for an album and returns a AlbumMap (i.e [3]string{artist, album } ->[]Path of songs in the album)
|
2021-11-16 02:47:50 -07:00
|
|
|
which will help us to add all album tracks to the playlist */
|
|
|
|
func QueryArtistTreeForAlbums(a map[string]map[string]map[string]string, album string) map[[2]string][][2]string {
|
|
|
|
AlbumMap := make(map[[2]string][][2]string)
|
|
|
|
for artistName, albumMap := range a {
|
|
|
|
for albumName, trackList := range albumMap {
|
|
|
|
if albumName == album {
|
|
|
|
var pathSlice [][2]string
|
|
|
|
for trackName, path := range trackList {
|
|
|
|
pathSlice = append(pathSlice, [2]string{trackName, path})
|
|
|
|
}
|
2021-11-17 02:36:18 -07:00
|
|
|
AlbumMap[[2]string{albumName, artistName}] = pathSlice
|
2021-11-16 02:47:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return AlbumMap
|
|
|
|
}
|
2021-11-16 05:09:44 -07:00
|
|
|
|
2021-11-16 07:59:46 -07:00
|
|
|
func AddToPlaylist(a interface{}, addAndPlay bool) {
|
2021-11-16 05:09:44 -07:00
|
|
|
switch a.(type) {
|
|
|
|
case [3]string:
|
|
|
|
{
|
|
|
|
b := a.([3]string)
|
2021-12-22 07:26:57 -07:00
|
|
|
AddTitle(ArtistTree, b[1], b[2], b[0], addAndPlay)
|
2021-11-16 05:09:44 -07:00
|
|
|
}
|
|
|
|
case [2]string:
|
|
|
|
{
|
|
|
|
b := a.([2]string)
|
2021-12-22 07:26:57 -07:00
|
|
|
AddAlbum(ArtistTree, b[0], b[1])
|
2021-11-16 05:09:44 -07:00
|
|
|
}
|
|
|
|
case string:
|
|
|
|
{
|
|
|
|
b := a.(string)
|
2021-12-22 07:26:57 -07:00
|
|
|
AddArtist(ArtistTree, b)
|
2021-11-16 05:09:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|