2014-03-30 12:49:40 -04:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-12-12 22:56:30 -05:00
|
|
|
|
using MediaBrowser.Controller.Playlists;
|
2014-03-30 12:49:40 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.Library
|
|
|
|
|
{
|
|
|
|
|
public class MusicManager : IMusicManager
|
|
|
|
|
{
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
|
|
|
|
|
public MusicManager(ILibraryManager libraryManager)
|
|
|
|
|
{
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<Audio> GetInstantMixFromSong(Audio item, User user)
|
|
|
|
|
{
|
2014-07-04 15:17:22 -04:00
|
|
|
|
var list = new List<Audio>
|
|
|
|
|
{
|
|
|
|
|
item
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return list.Concat(GetInstantMixFromGenres(item.Genres, user));
|
2014-03-30 12:49:40 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<Audio> GetInstantMixFromArtist(string name, User user)
|
|
|
|
|
{
|
|
|
|
|
var artist = _libraryManager.GetArtist(name);
|
|
|
|
|
|
2014-08-18 21:42:53 -04:00
|
|
|
|
var genres = user.RootFolder
|
2015-01-25 01:34:50 -05:00
|
|
|
|
.GetRecursiveChildren(user, i => i is Audio)
|
|
|
|
|
.Cast<Audio>()
|
2014-03-30 12:49:40 -04:00
|
|
|
|
.Where(i => i.HasArtist(name))
|
|
|
|
|
.SelectMany(i => i.Genres)
|
|
|
|
|
.Concat(artist.Genres)
|
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
return GetInstantMixFromGenres(genres, user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<Audio> GetInstantMixFromAlbum(MusicAlbum item, User user)
|
|
|
|
|
{
|
|
|
|
|
var genres = item
|
2015-01-25 01:34:50 -05:00
|
|
|
|
.GetRecursiveChildren(user, i => i is Audio)
|
|
|
|
|
.Cast<Audio>()
|
2014-12-12 22:56:30 -05:00
|
|
|
|
.SelectMany(i => i.Genres)
|
|
|
|
|
.Concat(item.Genres)
|
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
return GetInstantMixFromGenres(genres, user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<Audio> GetInstantMixFromPlaylist(Playlist item, User user)
|
|
|
|
|
{
|
|
|
|
|
var genres = item
|
2015-01-25 01:34:50 -05:00
|
|
|
|
.GetRecursiveChildren(user, i => i is Audio)
|
|
|
|
|
.Cast<Audio>()
|
2014-03-30 12:49:40 -04:00
|
|
|
|
.SelectMany(i => i.Genres)
|
|
|
|
|
.Concat(item.Genres)
|
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
return GetInstantMixFromGenres(genres, user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<Audio> GetInstantMixFromGenres(IEnumerable<string> genres, User user)
|
|
|
|
|
{
|
2015-01-25 01:34:50 -05:00
|
|
|
|
var inputItems = user.RootFolder
|
|
|
|
|
.GetRecursiveChildren(user, i => i is Audio);
|
2014-03-30 12:49:40 -04:00
|
|
|
|
|
|
|
|
|
var genresDictionary = genres.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
return inputItems
|
2015-01-25 01:34:50 -05:00
|
|
|
|
.Cast<Audio>()
|
2014-03-30 12:49:40 -04:00
|
|
|
|
.Select(i => new Tuple<Audio, int>(i, i.Genres.Count(genresDictionary.ContainsKey)))
|
2014-07-04 15:17:22 -04:00
|
|
|
|
.Where(i => i.Item2 > 0)
|
2014-03-30 12:49:40 -04:00
|
|
|
|
.OrderByDescending(i => i.Item2)
|
|
|
|
|
.ThenBy(i => Guid.NewGuid())
|
|
|
|
|
.Select(i => i.Item1)
|
|
|
|
|
.Take(100)
|
|
|
|
|
.OrderBy(i => Guid.NewGuid());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|