-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
metadata.go
168 lines (139 loc) · 3.94 KB
/
metadata.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"encoding/json"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/caarlos0/log"
"github.com/cheshir/ttlcache"
)
type SongMetadata struct {
ID string
AlbumArtwork string
ShareURL string
}
const baseURL = "https://tools.applemediaservices.com/api/apple-media/music/US/search.json"
func get(url string, result interface{}) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
bts, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if err := json.Unmarshal(bts, result); err != nil {
return err
}
return nil
}
func getSongMetadata(key string) (SongMetadata, error) {
var result getSongMetadataResult
err := get(baseURL+"?types=songs&limit=1&term="+key, &result)
if err != nil {
return SongMetadata{}, err
}
if len(result.Songs.Data) == 0 {
return SongMetadata{}, nil
}
id := result.Songs.Data[0].ID
artwork := result.Songs.Data[0].Attributes.Artwork.URL
artwork = strings.Replace(artwork, "{w}", "512", 1)
artwork = strings.Replace(artwork, "{h}", "512", 1)
return SongMetadata{
ID: id,
AlbumArtwork: artwork,
ShareURL: result.Songs.Data[0].Attributes.URL,
}, nil
}
func getArtistArtwork(key string) (string, error) {
var result getArtistMetadataResult
err := get(baseURL+"?types=artists&limit=1&term="+key, &result)
if err != nil {
return "", err
}
if len(result.Artists.Data) == 0 {
return "", nil
}
artwork := result.Artists.Data[0].Attributes.Artwork.URL
artwork = strings.Replace(artwork, "{w}", "512", 1)
artwork = strings.Replace(artwork, "{h}", "512", 1)
return artwork, nil
}
func getMetadata(artist, album, song string) (Metadata, error) {
key := url.QueryEscape(strings.Join([]string{artist, album, song}, " "))
albumArtworkCached, albumArtworkOk := cache.albumArtwork.Get(ttlcache.StringKey(key))
shareURLCached, shareURLOk := cache.shareURL.Get(ttlcache.StringKey(key))
artistArtworkCached, artistArtworkOk := cache.artistArtwork.Get(ttlcache.StringKey(artist))
if albumArtworkOk && artistArtworkOk && shareURLOk {
log.WithField("key", key).Debug("got song info from cache")
return Metadata{
AlbumArtwork: albumArtworkCached.(string),
ArtistArtwork: artistArtworkCached.(string),
ShareURL: shareURLCached.(string),
}, nil
}
var err error
var songMetadata SongMetadata
var artistArtwork string
if albumArtworkOk && shareURLOk {
songMetadata = SongMetadata{
AlbumArtwork: albumArtworkCached.(string),
ShareURL: shareURLCached.(string),
}
} else {
log.WithField("song", song).Debug("getting song metadata from api")
songMetadata, err = getSongMetadata(key)
if err != nil {
return Metadata{}, err
}
}
if artistArtworkOk {
artistArtwork = artistArtworkCached.(string)
} else {
log.WithField("artist", artist).Debug("getting artist artwork from api")
artistArtwork, _ = getArtistArtwork(url.QueryEscape(artist))
}
cache.albumArtwork.Set(ttlcache.StringKey(key), songMetadata.AlbumArtwork, time.Hour)
cache.shareURL.Set(ttlcache.StringKey(key), songMetadata.ShareURL, time.Hour)
cache.artistArtwork.Set(ttlcache.StringKey(artist), artistArtwork, time.Hour)
return Metadata{
ID: songMetadata.ID,
AlbumArtwork: songMetadata.AlbumArtwork,
ShareURL: songMetadata.ShareURL,
ArtistArtwork: artistArtwork,
}, nil
}
type getSongMetadataResult struct {
Songs struct {
Data []struct {
ID string `json:"id"`
Attributes struct {
URL string `json:"url"`
Artwork struct {
URL string `json:"url"`
} `json:"artwork"`
} `json:"attributes"`
} `json:"data"`
} `json:"songs"`
}
type getArtistMetadataResult struct {
Artists struct {
Data []struct {
Attributes struct {
Artwork struct {
URL string `json:"url"`
} `json:"artwork"`
} `json:"attributes"`
} `json:"data"`
} `json:"artists"`
}
type Metadata struct {
ID string
AlbumArtwork string
ArtistArtwork string
ShareURL string
}