/
spotify_web_api_cache.go
51 lines (47 loc) · 1.32 KB
/
spotify_web_api_cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package spotify
import (
"bytes"
"compress/gzip"
"encoding/json"
"github.com/fabiofalci/sconsify/infrastructure"
webspotify "github.com/zmb3/spotify"
"io"
"io/ioutil"
)
type WebApiCache struct {
Albums []webspotify.SavedAlbum
Songs []webspotify.SavedTrack
NewReleases []webspotify.FullPlaylist
}
func (spotify *Spotify) loadWebApiCache() *WebApiCache {
if spotify.cacheWebApiContent {
if fileLocation := infrastructure.GetWebApiCacheFileLocation(); fileLocation != "" {
if b, err := ioutil.ReadFile(fileLocation); err == nil {
compressed := bytes.NewBuffer(b)
if r, err := gzip.NewReader(compressed); err == nil {
var uncompressed bytes.Buffer
io.Copy(&uncompressed, r)
r.Close()
var webApiCache WebApiCache
if err := json.Unmarshal(uncompressed.Bytes(), &webApiCache); err == nil {
return &webApiCache
}
}
}
}
}
return &WebApiCache{}
}
func (spotify *Spotify) persistWebApiCache(webApiCache *WebApiCache) {
if spotify.cacheWebApiContent {
if b, err := json.Marshal(webApiCache); err == nil {
var compressed bytes.Buffer
w := gzip.NewWriter(&compressed)
w.Write([]byte(b))
w.Close()
if fileLocation := infrastructure.GetWebApiCacheFileLocation(); fileLocation != "" {
infrastructure.SaveFile(fileLocation, compressed.Bytes())
}
}
}
}