diff --git a/cache/cache.go b/cache/cache.go index 8f454be..80eba89 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -1,6 +1,7 @@ package cache import ( + "errors" "fmt" "io/ioutil" "os" @@ -8,13 +9,24 @@ import ( ) var ( - CACHE_LIST map[[2]string]string = make(map[[2]string]string) + USER_CACHE_DIR, err = os.UserCacheDir() + CACHE_LIST map[[2]string]string = make(map[[2]string]string) + CACHE_DIR string = USER_CACHE_DIR + DEFAULT_IMG string ) -func LoadCache(path string) { +func SetCacheDir(path string) { + CACHE_DIR = path +} + +func SetDefaultPath(path string) { + DEFAULT_IMG = path +} + +func LoadCache(path string) error { cacheFileContent, err := ioutil.ReadFile(path) if err != nil { - fmt.Println("Could Not Read From Cache File") + return errors.New("Could Not Read From Cache File") } lineSlice := strings.Split(string(cacheFileContent), "\n") for _, line := range lineSlice { @@ -25,9 +37,28 @@ func LoadCache(path string) { } } } + return nil } -func WriteToCache(path string) { +func GetFromCache(artist, album string) (string, error) { + if val, ok := CACHE_LIST[[2]string{artist, album}]; ok { + return val, nil + } else { + return "", errors.New("Element Not In Cache") + } +} + +func PointToDefault(artist, album string) { + CACHE_LIST[[2]string{artist, album}] = DEFAULT_IMG +} + +func AddToCache(artist, album string) string { + fileName := CACHE_DIR + GenerateName(artist, album) + CACHE_LIST[[2]string{artist, album}] = fileName + return fileName +} + +func WriteCache(path string) { b, err := os.Create(path) if err == nil { for k, v := range CACHE_LIST { @@ -37,5 +68,5 @@ func WriteToCache(path string) { } func GenerateName(artist, album string) string { - return fmt.Sprintf("%s-%s.jpg", artist, album) + return strings.Replace(strings.Replace(fmt.Sprintf("%s-%s.jpg", artist, album), " ", "_", -1), "/", "_", -1) } diff --git a/imageUtils.go b/imageUtils.go index 7172c5e..86f0fd6 100644 --- a/imageUtils.go +++ b/imageUtils.go @@ -64,16 +64,16 @@ func GetFlacImage(songPath, imagePath string) string { return "" } -func extractImageFromFile(uri string) string { +func extractImageFromFile(uri string, imagePath string) string { if strings.HasSuffix(uri, ".mp3") { - imagePath := GetMp3Image(uri, viper.GetString("COVER_IMAGE_PATH")) + imagePath := GetMp3Image(uri, imagePath) if imagePath == "" { return viper.GetString("DEFAULT_IMAGE_PATH") } else { return imagePath } } else if strings.HasSuffix(uri, ".flac") { - imagePath := GetFlacImage(uri, viper.GetString("COVER_IMAGE_PATH")) + imagePath := GetFlacImage(uri, imagePath) if imagePath == "" { return viper.GetString("DEFAULT_IMAGE_PATH") } else { diff --git a/lastfm.go b/lastfm.go index a673101..c1e9b86 100644 --- a/lastfm.go +++ b/lastfm.go @@ -12,7 +12,7 @@ import ( "github.com/spf13/viper" ) -func getImageFromLastFM(artist, album string) (string, error) { +func getImageFromLastFM(artist, album, imagePath 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, @@ -22,11 +22,11 @@ func getImageFromLastFM(artist, album string) (string, error) { if err != nil { return "", err } else { - return downloadImage(v.Images[len(v.Images)-1].Url) + return downloadImage(v.Images[len(v.Images)-1].Url, imagePath) } } -func downloadImage(url string) (string, error) { +func downloadImage(url string, imagePath string) (string, error) { var reader io.Reader if strings.HasPrefix(url, "http") { r, err := http.Get(url) @@ -37,10 +37,10 @@ func downloadImage(url string) (string, error) { reader = r.Body v, err := io.ReadAll(reader) if err == nil { - b, err := os.Create(viper.GetString("COVER_IMAGE_PATH")) + b, err := os.Create(imagePath) if err == nil { b.Write(v) - return viper.GetString("COVER_IMAGE_PATH"), nil + return imagePath, nil } else { b.Close() return "", err diff --git a/main.go b/main.go index 1f0045d..f512023 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "strconv" "time" + "github.com/aditya-K2/goMP/cache" "github.com/aditya-K2/goMP/config" "github.com/aditya-K2/goMP/search" "github.com/fhs/gompd/mpd" @@ -33,7 +34,9 @@ func main() { panic(mpdConnectionError) } defer CONN.Close() - + cache.SetCacheDir(viper.GetString("CACHE_DIR")) + cache.SetDefaultPath(viper.GetString("DEFAULT_IMAGE_PATH")) + cache.LoadCache(viper.GetString("CACHE_FILE")) r := newRenderer() c, _ := CONN.CurrentSong() if len(c) != 0 { @@ -265,4 +268,5 @@ func main() { if err := UI.App.Run(); err != nil { panic(err) } + cache.WriteCache(viper.GetString("CACHE_FILE")) } diff --git a/render.go b/render.go index 807c28a..b7797ee 100644 --- a/render.go +++ b/render.go @@ -1,6 +1,7 @@ package main import ( + "github.com/aditya-K2/goMP/cache" "github.com/spf13/viper" "gitlab.com/diamondburned/ueberzug-go" ) @@ -43,18 +44,7 @@ func openImage(path string, c chan string) { fw, fh := getFontWidth() var im *ueberzug.Image if path != "stop" { - 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 { - NOTIFICATION_SERVER.Send("Image From LastFM") - extractedImage = downloadedImage - } - } - } + extractedImage := getImagePath(path) 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")) } @@ -76,3 +66,34 @@ func openImage(path string, c chan string) { func (self *Renderer) Start(path string) { go openImage(path, self.c) } + +/* + This Function returns the path to the image that is to be rendered it checks first for the image in the cache + else it adds the image to the cache and then extracts it and renders it. +*/ +func getImagePath(path string) string { + a, err := CONN.ListInfo(path) + var extractedImage string + if err == nil && len(a) != 0 { + if val, err := cache.GetFromCache(a[0]["artist"], a[0]["album"]); err == nil { + extractedImage = val + } else { + imagePath := cache.AddToCache(a[0]["artist"], a[0]["album"]) + absPath := viper.GetString("MUSIC_DIRECTORY") + path + extractedImage = extractImageFromFile(absPath, imagePath) + if extractedImage == viper.GetString("DEFAULT_IMAGE_PATH") && viper.GetString("GET_COVER_ART_FROM_LAST_FM") == "TRUE" { + downloadedImage, err := getImageFromLastFM(a[0]["artist"], a[0]["album"], imagePath) + if err == nil { + NOTIFICATION_SERVER.Send("Image From LastFM") + extractedImage = downloadedImage + } else { + NOTIFICATION_SERVER.Send("Falling Back to Default Image.") + cache.PointToDefault(a[0]["artist"], a[0]["album"]) + } + } else { + NOTIFICATION_SERVER.Send("Extracted Image Successfully") + } + } + } + return extractedImage +}