-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalbum.go
292 lines (220 loc) · 6.08 KB
/
album.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package store
import (
"sort"
"strings"
"xarantolus/sensibleHub/store/music"
)
// Album represents a music album. Songs is guaranteed to have one or more items
type Album struct {
Title string
Artist string
Songs []music.Entry
}
func (a *Album) AlbumTitle() string {
if a.Title == "" {
return "Unknown"
}
return a.Title
}
// setupAlbum moves the song with the same title as the album name to the first place
func (a *Album) setupAlbum() (ret *Album) {
a.Title = a.Songs[0].MusicData.Album
a.Artist = a.Songs[0].MusicData.Artist
if len(a.Songs) < 2 || a.Title == "" {
return a
}
var firstSong int = -1
for i, s := range a.Songs {
title := s.MusicData.Title
// Title (feat. something)
if bi := strings.IndexRune(title, '('); bi != -1 {
title = strings.TrimSpace(title[:bi])
}
// If song title without feature artist == album title
if strings.EqualFold(CleanName(title), CleanName(a.Title)) {
firstSong = i
break
}
}
// If it is -1, we couldn't find any song that should be at the front so we ignore it
// If it is 0, we already have the correct order
if firstSong < 1 {
return a
}
newSongs := make([]music.Entry, len(a.Songs))
newSongs[0] = a.Songs[firstSong]
// Copy before first song
n := copy(newSongs[1:], a.Songs[:firstSong])
copy(newSongs[n+1:], a.Songs[firstSong+1:])
return &Album{
newSongs[0].MusicData.Album,
newSongs[0].MusicData.Artist,
newSongs,
}
}
// GetAlbum gets the specified album for the given artist.
// Songs are sorted alphabetically as we don't store the title number
func (m *Manager) GetAlbum(artist, albumName string) (a Album, ok bool) {
artist, albumName = CleanName(artist), CleanName(albumName)
for _, e := range m.AllEntries() {
// Wrong artist?
if !strings.EqualFold(CleanName(e.Artist()), artist) {
continue
}
// Wrong album?
if !strings.EqualFold(CleanName(e.AlbumName()), albumName) {
continue
}
a.Songs = append(a.Songs, e)
}
if len(a.Songs) == 0 {
return a, false
}
return *a.setupAlbum(), true
}
// ArtistInfo contains a summary of an artist and all albums
type ArtistInfo struct {
Name string
PlayTime float64
YearStart int
YearEnd int
Albums []Album
Featured []music.Entry
}
// Artist returns the albums for the specified artist
func (m *Manager) Artist(artist string) (ai ArtistInfo, ok bool) {
var res []Album
cleanedArtist := CleanName(artist)
artist = strings.ToUpper(artist)
am := make(map[string]Album)
unknownAlbum := Album{}
for _, e := range m.AllEntries() {
// Wrong artist?
if !strings.EqualFold(CleanName(e.Artist()), cleanedArtist) {
ti := strings.ToUpper(e.MusicData.Title)
// We assume that the song title is something like
// Title (feat. FirstArtist & SecondArtist)
firstBracket, lastBracket := strings.IndexByte(ti, '('), strings.LastIndexByte(ti, ')')
// cannot find the "feat." part in brackets:
if firstBracket == -1 || lastBracket == -1 {
continue
}
// Both are uppercase, check if we can find the artist in there
if strings.Contains(ti[firstBracket:lastBracket], artist) {
ai.Featured = append(ai.Featured, e)
}
continue
}
aname := CleanName(e.MusicData.Album)
combined := strings.ToUpper(aname)
var album Album
if aname == "" {
album = unknownAlbum
} else {
album = am[combined]
}
// Album might be zero value, but that doesn't matter
album.Songs = append(album.Songs, e)
if aname == "" {
unknownAlbum = album
} else {
am[combined] = album
}
ai.PlayTime += e.MusicData.Duration
if e.MusicData.Year != nil {
y := *e.MusicData.Year
if y < ai.YearStart || ai.YearStart == 0 {
ai.YearStart = y
}
if y > ai.YearEnd || ai.YearEnd == 0 {
ai.YearEnd = y
}
}
}
if len(am) == 0 && len(unknownAlbum.Songs) == 0 {
return ai, false
}
for _, a := range am {
a = *a.setupAlbum()
res = append(res, a)
}
ai.Albums = res
sort.Slice(res, func(i, j int) bool {
return strings.ToUpper(res[i].Title) < strings.ToUpper(res[j].Title)
})
sort.Slice(ai.Featured, func(i, j int) bool {
return strings.ToUpper(ai.Featured[i].MusicData.Title) < strings.ToUpper(ai.Featured[j].MusicData.Title)
})
// Unknown albums should always be the last album
if len(unknownAlbum.Songs) > 0 {
unknownAlbum.Artist = unknownAlbum.Songs[0].MusicData.Artist
ai.Albums = append(ai.Albums, unknownAlbum)
}
ai.Name = ai.Albums[0].Artist
return ai, true
}
// GroupByAlbum groups songs by their artist and albums
func (m *Manager) GroupByAlbum() (res []Album) {
normalizedArtists := make(map[string]string)
// this code is very similar to `ftp/musicfactory.go`
// in fact, I even copied most of it
am := make(map[string]Album)
for _, e := range m.AllEntries() {
art := CleanName(e.Artist())
artistName, ok := normalizedArtists[strings.ToUpper(art)]
if !ok {
normalizedArtists[strings.ToUpper(e.Artist())] = e.Artist()
artistName = art
}
aname := CleanName(e.AlbumName())
combined := strings.ToUpper(art + "/" + aname)
album, ok := am[combined]
if !ok {
album = Album{
Title: e.AlbumName(),
Artist: artistName,
}
}
album.Songs = append(album.Songs, e)
am[combined] = album
}
for _, a := range am {
a = *a.setupAlbum()
res = append(res, a)
}
sort.Slice(res, func(i, j int) bool {
if strings.EqualFold(res[i].Artist, res[j].Artist) {
return strings.ToUpper(res[i].Title) < strings.ToUpper(res[j].Title)
}
return res[i].Artist < res[j].Artist
})
return
}
// CleanName converts `n` to an url-safe string
func CleanName(n string) string {
return strings.Map(func(r rune) rune {
if r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' {
return r
}
if r >= '0' && r <= '9' {
return r
}
if r == '-' || r == '.' || r == ' ' || r == '(' || r == ')' {
return r
}
return -1
}, n)
}
func equalBrackets(a, b string) bool {
return strings.EqualFold(cleanBrackets(a), cleanBrackets(b))
}
func cleanBrackets(a string) string {
for {
var sb, eb = strings.IndexRune(a, '('), strings.IndexRune(a, ')')
if sb == -1 || eb == -1 || sb > eb {
break
}
a = a[:sb] + a[eb+1:]
}
return strings.Join(strings.Fields(a), " ")
}