add random album sort
This commit is contained in:
+119
-15
@@ -29,34 +29,38 @@ func NewLibraryManager(s *ServerManager) *LibraryManager {
|
|||||||
type AlbumSortOrder string
|
type AlbumSortOrder string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
RecentlyAdded AlbumSortOrder = "Recently Added"
|
AlbumSortRecentlyAdded AlbumSortOrder = "Recently Added"
|
||||||
RecentlyPlayed AlbumSortOrder = "Recently Played"
|
AlbumSortRecentlyPlayed AlbumSortOrder = "Recently Played"
|
||||||
FrequentlyPlayed AlbumSortOrder = "Frequently Played"
|
AlbumSortFrequentlyPlayed AlbumSortOrder = "Frequently Played"
|
||||||
TitleAZ AlbumSortOrder = "Title (A-Z)"
|
AlbumSortRandom AlbumSortOrder = "Random"
|
||||||
ArtistAZ AlbumSortOrder = "Artist (A-Z)"
|
AlbumSortTitleAZ AlbumSortOrder = "Title (A-Z)"
|
||||||
|
AlbumSortArtistAZ AlbumSortOrder = "Artist (A-Z)"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
AlbumSortOrders []string = []string{
|
AlbumSortOrders []string = []string{
|
||||||
string(RecentlyAdded),
|
string(AlbumSortRecentlyAdded),
|
||||||
string(RecentlyPlayed),
|
string(AlbumSortRecentlyPlayed),
|
||||||
string(FrequentlyPlayed),
|
string(AlbumSortFrequentlyPlayed),
|
||||||
string(TitleAZ),
|
string(AlbumSortRandom),
|
||||||
string(ArtistAZ),
|
string(AlbumSortTitleAZ),
|
||||||
|
string(AlbumSortArtistAZ),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func (l *LibraryManager) AlbumsIter(sort AlbumSortOrder) AlbumIterator {
|
func (l *LibraryManager) AlbumsIter(sort AlbumSortOrder) AlbumIterator {
|
||||||
switch sort {
|
switch sort {
|
||||||
case RecentlyAdded:
|
case AlbumSortRecentlyAdded:
|
||||||
return l.newBaseIter("newest")
|
return l.newBaseIter("newest")
|
||||||
case RecentlyPlayed:
|
case AlbumSortRecentlyPlayed:
|
||||||
return l.newBaseIter("recent")
|
return l.newBaseIter("recent")
|
||||||
case FrequentlyPlayed:
|
case AlbumSortFrequentlyPlayed:
|
||||||
return l.newBaseIter("frequent")
|
return l.newBaseIter("frequent")
|
||||||
case TitleAZ:
|
case AlbumSortRandom:
|
||||||
|
return l.newRandomIter()
|
||||||
|
case AlbumSortTitleAZ:
|
||||||
return l.newBaseIter("alphabeticalByName")
|
return l.newBaseIter("alphabeticalByName")
|
||||||
case ArtistAZ:
|
case AlbumSortArtistAZ:
|
||||||
return l.newBaseIter("alphabeticalByArtist")
|
return l.newBaseIter("alphabeticalByArtist")
|
||||||
default:
|
default:
|
||||||
log.Printf("Undefined album sort order: %s", sort)
|
log.Printf("Undefined album sort order: %s", sort)
|
||||||
@@ -263,3 +267,103 @@ func (s *searchIter) addNewAlbums(al []*subsonic.AlbumID3) {
|
|||||||
s.albumIDset[album.ID] = true
|
s.albumIDset[album.ID] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type randomIter struct {
|
||||||
|
albumIDSet map[string]bool
|
||||||
|
l *LibraryManager
|
||||||
|
s *subsonic.Client
|
||||||
|
prefetched []*subsonic.AlbumID3
|
||||||
|
prefetchedPos int
|
||||||
|
// Random iter works in two phases - phase 1 by requesting random
|
||||||
|
// albums from the server. Since the Subsonic API provides no way
|
||||||
|
// of paginating a single random sort, we may get albums back twice.
|
||||||
|
// We use albumIDSet to keep track of which albums have already been returned.
|
||||||
|
// Once we start getting back too many already-returned albums,
|
||||||
|
// switch to requesting more albums from a deterministic sort order.
|
||||||
|
phaseTwo bool
|
||||||
|
offset int
|
||||||
|
done bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *LibraryManager) newRandomIter() *randomIter {
|
||||||
|
return &randomIter{
|
||||||
|
l: l,
|
||||||
|
s: l.s.Server,
|
||||||
|
albumIDSet: make(map[string]bool),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *randomIter) Next() *subsonic.AlbumID3 {
|
||||||
|
if r.done {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.prefetched == nil {
|
||||||
|
if r.phaseTwo {
|
||||||
|
for len(r.prefetched) == 0 {
|
||||||
|
albums, err := r.s.GetAlbumList2("newest", map[string]string{"size": "20", "offset": strconv.Itoa(r.offset)})
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
albums = nil
|
||||||
|
}
|
||||||
|
if len(albums) == 0 {
|
||||||
|
r.done = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
r.offset += len(albums)
|
||||||
|
for _, album := range albums {
|
||||||
|
if _, ok := r.albumIDSet[album.ID]; !ok {
|
||||||
|
r.prefetched = append(r.prefetched, album)
|
||||||
|
r.albumIDSet[album.ID] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
r.prefetchedPos = 0
|
||||||
|
} else {
|
||||||
|
albums, err := r.s.GetAlbumList2("random", map[string]string{"size": "25"})
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
r.done = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var hitCount int
|
||||||
|
for _, album := range albums {
|
||||||
|
if _, ok := r.albumIDSet[album.ID]; !ok {
|
||||||
|
hitCount++
|
||||||
|
r.prefetched = append(r.prefetched, album)
|
||||||
|
r.albumIDSet[album.ID] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if successRatio := float64(hitCount) / float64(25); successRatio < 0.4 {
|
||||||
|
r.phaseTwo = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// return from prefetched results
|
||||||
|
if len(r.prefetched) > 0 {
|
||||||
|
a := r.prefetched[r.prefetchedPos]
|
||||||
|
r.prefetchedPos++
|
||||||
|
if r.prefetchedPos == len(r.prefetched) {
|
||||||
|
r.prefetched = nil
|
||||||
|
r.prefetchedPos = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
r.l.CacheAlbum(a)
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *randomIter) NextN(n int, cb func(*subsonic.AlbumID3)) {
|
||||||
|
go func() {
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
a := r.Next()
|
||||||
|
cb(a)
|
||||||
|
if a == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user