Compare commits
2 Commits
04e8378f3f
...
964512de06
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
964512de06 | ||
|
|
87e276de3d |
@@ -76,21 +76,29 @@ func (s musicObjectStore) currentMusicObjects() ([]data.MusicObject, error) {
|
||||
|
||||
var indexSelectionPattern = regexp.MustCompile(`^[\d ,]+$`)
|
||||
|
||||
func interpretCommand(command string, store musicObjectStore) (musicObjectStore, error) {
|
||||
func interpretCommand(command string, store musicObjectStore) (musicObjectStore, []error) {
|
||||
// going back in history
|
||||
if command == ".." {
|
||||
if len(store) <= 1 {
|
||||
return store, errors.New("can't go back")
|
||||
return store, []error{errors.New("can't go back")}
|
||||
}
|
||||
|
||||
return store[:len(store)-1], nil
|
||||
return store[:len(store)-1], []error{}
|
||||
}
|
||||
|
||||
forceDownload := false
|
||||
if strings.HasPrefix(command, "d:") {
|
||||
command, _ = strings.CutPrefix(command, "d:")
|
||||
command = strings.TrimSpace(command)
|
||||
|
||||
forceDownload = true
|
||||
}
|
||||
|
||||
// fetch special music object
|
||||
if indexSelectionPattern.MatchString(command) {
|
||||
currentMusicObjects, err := store.currentMusicObjects()
|
||||
if err != nil {
|
||||
return store, err
|
||||
return store, []error{err}
|
||||
}
|
||||
|
||||
var fetched data.MusicObject
|
||||
@@ -99,14 +107,14 @@ func interpretCommand(command string, store musicObjectStore) (musicObjectStore,
|
||||
index, _ := strconv.Atoi(strings.TrimSpace(stringIndex))
|
||||
|
||||
if index >= len(currentMusicObjects) || index < 0 {
|
||||
return store, errors.New(command + " is out of bounds [0 <= " + strconv.Itoa(index) + " <= " + strconv.Itoa(len(currentMusicObjects)-1) + "]")
|
||||
return store, []error{errors.New(command + " is out of bounds [0 <= " + strconv.Itoa(index) + " <= " + strconv.Itoa(len(currentMusicObjects)-1) + "]")}
|
||||
}
|
||||
|
||||
current := currentMusicObjects[index]
|
||||
|
||||
newFetched, err := plugin.Fetch(current)
|
||||
if err != nil {
|
||||
return store, err
|
||||
return store, []error{err}
|
||||
}
|
||||
|
||||
if fetched == nil {
|
||||
@@ -116,7 +124,13 @@ func interpretCommand(command string, store musicObjectStore) (musicObjectStore,
|
||||
}
|
||||
}
|
||||
|
||||
return append(store, fetched.Related()), nil
|
||||
if forceDownload {
|
||||
return store, plugin.Download(fetched)
|
||||
} else {
|
||||
return append(store, fetched.Related()), []error{}
|
||||
}
|
||||
} else if forceDownload {
|
||||
return store, []error{errors.New("can only download indices not " + command)}
|
||||
}
|
||||
|
||||
// search in every other case
|
||||
@@ -126,7 +140,7 @@ func interpretCommand(command string, store musicObjectStore) (musicObjectStore,
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
return append(store, currentMusicObjects), nil
|
||||
return append(store, currentMusicObjects), []error{}
|
||||
}
|
||||
|
||||
func Shell(commandsList ...[]string) {
|
||||
@@ -161,10 +175,13 @@ func Shell(commandsList ...[]string) {
|
||||
fmt.Println("> " + command)
|
||||
}
|
||||
|
||||
store, err = interpretCommand(strings.TrimSpace(command), store)
|
||||
if err != nil {
|
||||
var errList []error
|
||||
store, errList = interpretCommand(strings.TrimSpace(command), store)
|
||||
if len(errList) > 0 {
|
||||
fmt.Println()
|
||||
fmt.Println(color.Yellow + err.Error() + color.Reset)
|
||||
for _, err := range errList {
|
||||
fmt.Println(color.Yellow + err.Error() + color.Reset)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
|
||||
@@ -84,7 +84,7 @@ func CleanSongTitle(title string, artistName string) string {
|
||||
title = strings.TrimSpace(title)
|
||||
|
||||
for _, d := range commonTitleSuffix {
|
||||
if strings.HasSuffix(strings.ToLower(d), d) {
|
||||
if strings.HasSuffix(strings.ToLower(title), d) {
|
||||
title = strings.TrimSpace(title[:len(d)-1])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package plugin
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"gitea.elara.ws/Hazel/music-kraken/internal/common"
|
||||
@@ -163,7 +164,7 @@ func Fetch(musicObject data.MusicObject) (data.MusicObject, error) {
|
||||
musicObject = musicObject.Merge(newMusicObject)
|
||||
}
|
||||
|
||||
return musicObject, nil
|
||||
return musicObject.Compile(), nil
|
||||
}
|
||||
|
||||
type SearchConfig struct {
|
||||
@@ -189,3 +190,93 @@ func Search(search string, config SearchConfig) ([]data.MusicObject, error) {
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
type downloadState struct {
|
||||
artist *data.Artist
|
||||
album *data.Album
|
||||
song *data.Song
|
||||
}
|
||||
|
||||
var variousArtist = data.Artist{
|
||||
Name: "VariousArtist",
|
||||
}.Compile().(data.Artist)
|
||||
|
||||
var variousAlbum = data.Album{
|
||||
Name: "VariousAlbum",
|
||||
}.Compile().(data.Album)
|
||||
|
||||
func downloadSong(song data.Song, state downloadState) error {
|
||||
fmt.Println("downloading: " + song.Name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func Download(musicObject data.MusicObject, statesInput ...downloadState) []error {
|
||||
state := downloadState{}
|
||||
if len(statesInput) > 0 {
|
||||
state = statesInput[0]
|
||||
}
|
||||
|
||||
musicObject, err := Fetch(musicObject)
|
||||
if err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
|
||||
if song, ok := musicObject.(data.Song); ok {
|
||||
state.song = &song
|
||||
|
||||
if state.artist == nil {
|
||||
if len(song.Artists) > 0 {
|
||||
state.artist = &song.Artists[0]
|
||||
} else {
|
||||
state.artist = &variousArtist
|
||||
}
|
||||
}
|
||||
|
||||
if state.album == nil {
|
||||
if song.Album.Name != "" {
|
||||
state.album = &song.Album
|
||||
} else {
|
||||
state.album = &variousAlbum
|
||||
}
|
||||
}
|
||||
|
||||
err := downloadSong(song, state)
|
||||
if err == nil {
|
||||
return []error{}
|
||||
} else {
|
||||
return []error{err}
|
||||
}
|
||||
}
|
||||
|
||||
if album, ok := musicObject.(data.Album); ok {
|
||||
state.album = &album
|
||||
|
||||
if state.artist == nil {
|
||||
if len(album.Artists) > 0 {
|
||||
state.artist = &album.Artists[0]
|
||||
} else {
|
||||
state.artist = &variousArtist
|
||||
}
|
||||
}
|
||||
|
||||
errList := []error{}
|
||||
for _, song := range album.Songs {
|
||||
errList = append(errList, Download(song, state)...)
|
||||
}
|
||||
return errList
|
||||
}
|
||||
|
||||
if artist, ok := musicObject.(data.Artist); ok {
|
||||
state.artist = &artist
|
||||
|
||||
errList := []error{}
|
||||
for _, album := range artist.Albums {
|
||||
errList = append(errList, Download(album, state)...)
|
||||
}
|
||||
return errList
|
||||
}
|
||||
|
||||
return []error{
|
||||
errors.New("music object not recognized"),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user