Skip to content

Commit

Permalink
Surface metadata about tracks in index + add disc padding for filenames
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Crane <marcus@utf9k.net>
  • Loading branch information
marcus-crane committed Oct 7, 2023
1 parent f87394c commit 26436a1
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 23 deletions.
6 changes: 3 additions & 3 deletions cmd/khinsider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ func Execute(buildInfo BuildInfo) {
Name: "search",
Aliases: []string{"s"},
Usage: "search for an album to download",
// Before: func(c *cli.Context) error {
// return BeforeSearch()
// },
Before: func(c *cli.Context) error {
return BeforeSearch()
},
Action: func(c *cli.Context) error {
return SearchAction()
},
Expand Down
4 changes: 2 additions & 2 deletions cmd/khinsider/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ func SearchAction() error {
if err != nil {
panic(err)
}
albumSlug, err := search.FilterAlbumList(index)
hints, err := search.FilterAlbumList(index)
if err != nil {
panic(err)
}
err = DownloadAction(albumSlug)
err = DownloadAction(hints.Slug)
if err != nil {
pterm.Error.Println("Failed to download album")
}
Expand Down
24 changes: 16 additions & 8 deletions pkg/download/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"io/fs"
"os"
"strings"
"unicode/utf8"

"github.com/marcus-crane/khinsider/v3/pkg/types"
Expand All @@ -17,8 +16,7 @@ import (
func GetAlbum(album *types.Album) {
usrHome, _ := os.UserHomeDir()
downloadFolder := fmt.Sprintf("%s/Downloads", usrHome) // TODO: Offer a configuration option
normalisedSlug := strings.ReplaceAll(album.Slug, ".", "")
directoryPath := fmt.Sprintf("%s/%s", downloadFolder, normalisedSlug)
directoryPath := fmt.Sprintf("%s/%s", downloadFolder, normaliseFileName(album.Title))
// TODO: This should be checked before download since it takes ages to get here
_, err := os.Stat(directoryPath)
if !errors.Is(err, fs.ErrNotExist) && err != nil {
Expand All @@ -40,15 +38,25 @@ func GetAlbum(album *types.Album) {
panic(err)
}
pterm.Success.Printfln("Successfully created %s", directoryPath)
prevDisc := int32(0)
trackPadLen := len(fmt.Sprintf("%d", album.Total.Tracks))
discPadLen := len(fmt.Sprintf("%d", album.Tracks[len(album.Tracks)-1].DiscNumber))
for _, track := range album.Tracks {
currDisc := track.DiscNumber
if currDisc != 0 && currDisc > prevDisc {
highestTrackNumForDisc := int32(0)
for _, track := range album.Tracks {
if track.DiscNumber == currDisc {
highestTrackNumForDisc = track.TrackNumber
}
}
trackPadLen = len(fmt.Sprintf("%d", highestTrackNumForDisc))
}
trackFmt := track.Title
// Some of the numbering can be quite bad on khinsider so we shouldn't
// assume the track numbers are any good!
padLength := len(fmt.Sprintf("%d", album.Total.Tracks))
trackFmt = fmt.Sprintf("%0*d %s", padLength, track.TrackNumber, trackFmt)
trackFmt = fmt.Sprintf("%0*d %s", trackPadLen, track.TrackNumber, trackFmt)
if track.DiscNumber != 0 {
// TODO: Padding for 10+ discs
trackFmt = fmt.Sprintf("%dx%s", track.DiscNumber, trackFmt)
trackFmt = fmt.Sprintf("%0*dx%s", discPadLen, track.DiscNumber, trackFmt)
}
err := SaveAudioFile(track, trackFmt, directoryPath)
if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions pkg/indexer/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,12 @@ func DownloadIndex() error {
panic(err)
}
}(res.Body)
p, _ := pterm.DefaultProgressbar.WithTotal(int(res.ContentLength)).WithTitle("Downloading index").WithRemoveWhenDone(true).WithShowPercentage(true).Start()
reader := util.NewBarProxyReader(res.Body, p)
if res.StatusCode == http.StatusOK {
pterm.Debug.Printfln("Retrieved index with status code of %d", res.StatusCode)
indexPath := getCachePath(LocalIndex)
createPathIfNotExists(indexPath)
var index types.SearchResults
if err := util.LoadJSON(reader, &index); err != nil {
if err := util.LoadJSON(res.Body, &index); err != nil {
panic(err)
}
err = SaveIndex(index)
Expand Down
24 changes: 19 additions & 5 deletions pkg/search/filter.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
package search

import (
"fmt"
"sort"
"strings"

"github.com/AlecAivazis/survey/v2"
"github.com/marcus-crane/khinsider/v3/pkg/types"
)

func FilterAlbumList(list types.SearchResults) (string, error) {
func FilterAlbumList(list types.SearchResults) (types.AlbumHints, error) {
keys := make([]string, 0, len(list))
for k := range list {
keys = append(keys, k)
for k, v := range list {
kFmt := fmt.Sprintf("%s ~ %d", k, v.Year)
if v.DiscCount > 0 {
kFmt = fmt.Sprintf("%s | D%d", kFmt, v.DiscCount)
}
kFmt = fmt.Sprintf("%s | T%d | %s", kFmt, v.TrackCount, v.Genre)
if v.MP3Exists {
kFmt = fmt.Sprintf("%s | [MP3]", kFmt)
}
if v.FlacExists {
kFmt = fmt.Sprintf("%s [FLAC]", kFmt)
}
keys = append(keys, kFmt)
}
sort.Strings(keys)

Expand All @@ -23,7 +36,8 @@ func FilterAlbumList(list types.SearchResults) (string, error) {
err := survey.AskOne(prompt, &result, survey.WithPageSize(25))

if err != nil {
return "", err
return types.AlbumHints{}, err
}
return list[result], nil
title := strings.Split(result, " ~")[0]
return list[title], nil
}
12 changes: 11 additions & 1 deletion pkg/types/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
package types

type SearchResults map[string]string
type SearchResults map[string]AlbumHints

type AlbumHints struct {
Slug string `json:"s"`
MP3Exists bool `json:"m"`
FlacExists bool `json:"f"`
Year int32 `json:"y"`
Genre string `json:"g"`
TrackCount int32 `json:"c"`
DiscCount int32 `json:"d"`
}

type Album struct {
Title string `json:"title"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func LoadJSON(file io.Reader, i interface{}) error {
fileBytes, _ := io.ReadAll(file)
err := json.Unmarshal(fileBytes, &i)
if err != nil {
pterm.Error.Println("Failed to load JSON")
pterm.Error.Printf("Failed to load JSON: %+v\n", err)
return errors.New("failed to load JSON")
}
return nil
Expand Down

0 comments on commit 26436a1

Please sign in to comment.