jellyfin-server/MediaBrowser.Server.Implementations/Library/MusicManager.cs

146 lines
4.5 KiB
C#
Raw Normal View History

using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Library;
2014-12-13 03:56:30 +00:00
using MediaBrowser.Controller.Playlists;
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)
{
var list = new List<Audio>
{
item
};
return list.Concat(GetInstantMixFromGenres(item.Genres, user));
}
2015-08-19 23:57:27 +00:00
public IEnumerable<Audio> GetInstantMixFromArtist(MusicArtist artist, User user)
{
2014-08-19 01:42:53 +00:00
var genres = user.RootFolder
2015-01-25 06:34:50 +00:00
.GetRecursiveChildren(user, i => i is Audio)
.Cast<Audio>()
2015-04-26 03:25:07 +00:00
.Where(i => i.HasAnyArtist(artist.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 06:34:50 +00:00
.GetRecursiveChildren(user, i => i is Audio)
.Cast<Audio>()
2014-12-13 03:56:30 +00:00
.SelectMany(i => i.Genres)
.Concat(item.Genres)
2015-04-26 03:25:07 +00:00
.DistinctNames();
2014-12-13 03:56:30 +00:00
return GetInstantMixFromGenres(genres, user);
}
2015-08-28 04:19:08 +00:00
public IEnumerable<Audio> GetInstantMixFromFolder(Folder item, User user)
{
var genres = item
.GetRecursiveChildren(user, i => i is Audio)
.Cast<Audio>()
.SelectMany(i => i.Genres)
.Concat(item.Genres)
.DistinctNames();
return GetInstantMixFromGenres(genres, user);
}
2014-12-13 03:56:30 +00:00
public IEnumerable<Audio> GetInstantMixFromPlaylist(Playlist item, User user)
{
var genres = item
2015-01-25 06:34:50 +00:00
.GetRecursiveChildren(user, i => i is Audio)
.Cast<Audio>()
.SelectMany(i => i.Genres)
.Concat(item.Genres)
2015-04-26 03:25:07 +00:00
.DistinctNames();
return GetInstantMixFromGenres(genres, user);
}
public IEnumerable<Audio> GetInstantMixFromGenres(IEnumerable<string> genres, User user)
{
2015-10-19 15:33:49 +00:00
var genreList = genres.ToList();
2015-10-29 13:28:05 +00:00
var inputItems = _libraryManager.GetItems(new InternalItemsQuery(user)
2015-10-19 15:33:49 +00:00
{
IncludeItemTypes = new[] { typeof(Audio).Name },
2015-10-29 13:28:05 +00:00
Genres = genreList.ToArray()
2015-10-19 15:33:49 +00:00
2015-11-11 14:56:31 +00:00
}, new string[] { });
2015-10-19 15:33:49 +00:00
var genresDictionary = genreList.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
return inputItems
2015-01-25 06:34:50 +00:00
.Cast<Audio>()
.Select(i => new Tuple<Audio, int>(i, i.Genres.Count(genresDictionary.ContainsKey)))
.Where(i => i.Item2 > 0)
.OrderByDescending(i => i.Item2)
.ThenBy(i => Guid.NewGuid())
.Select(i => i.Item1)
.Take(100)
.OrderBy(i => Guid.NewGuid());
}
public IEnumerable<Audio> GetInstantMixFromItem(BaseItem item, User user)
{
var genre = item as MusicGenre;
if (genre != null)
{
return GetInstantMixFromGenres(new[] { item.Name }, user);
}
var playlist = item as Playlist;
if (playlist != null)
{
return GetInstantMixFromPlaylist(playlist, user);
}
var album = item as MusicAlbum;
if (album != null)
{
return GetInstantMixFromAlbum(album, user);
}
var artist = item as MusicArtist;
if (artist != null)
{
2015-08-19 23:57:27 +00:00
return GetInstantMixFromArtist(artist, user);
}
var song = item as Audio;
if (song != null)
{
return GetInstantMixFromSong(song, user);
}
2015-08-28 04:19:08 +00:00
var folder = item as Folder;
if (folder != null)
{
return GetInstantMixFromFolder(folder, user);
}
2015-10-19 15:33:49 +00:00
return new Audio[] { };
}
}
}