Merge pull request #9 from aditya-K2/cache

Caching
This commit is contained in:
Aditya Kurdunkar 2021-11-28 23:39:43 +05:30 committed by GitHub
commit 064e6b9673
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 82 additions and 26 deletions

41
cache/cache.go vendored
View File

@ -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)
}

View File

@ -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 {

View File

@ -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

View File

@ -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"))
}

View File

@ -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
}