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

143 lines
4.4 KiB
C#
Raw Normal View History

2019-11-01 17:38:54 +00:00
#pragma warning disable CS1591
2019-12-10 23:13:57 +00:00
#pragma warning disable SA1600
2019-11-01 17:38:54 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
2017-05-21 07:25:49 +00:00
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Playlists;
2017-09-04 19:28:22 +00:00
using MediaBrowser.Model.Entities;
2017-04-27 18:12:44 +00:00
using MediaBrowser.Model.Querying;
namespace Emby.Server.Implementations.Library
{
public class MusicManager : IMusicManager
{
private readonly ILibraryManager _libraryManager;
public MusicManager(ILibraryManager libraryManager)
{
_libraryManager = libraryManager;
}
public List<BaseItem> GetInstantMixFromSong(Audio item, User user, DtoOptions dtoOptions)
{
var list = new List<Audio>
{
item
};
return list.Concat(GetInstantMixFromGenres(item.Genres, user, dtoOptions)).ToList();
}
public List<BaseItem> GetInstantMixFromArtist(MusicArtist item, User user, DtoOptions dtoOptions)
{
2017-05-21 07:25:49 +00:00
return GetInstantMixFromGenres(item.Genres, user, dtoOptions);
}
public List<BaseItem> GetInstantMixFromAlbum(MusicAlbum item, User user, DtoOptions dtoOptions)
{
2017-05-21 07:25:49 +00:00
return GetInstantMixFromGenres(item.Genres, user, dtoOptions);
2014-12-13 03:56:30 +00:00
}
public List<BaseItem> GetInstantMixFromFolder(Folder item, User user, DtoOptions dtoOptions)
2015-08-28 04:19:08 +00:00
{
var genres = item
2016-05-18 05:34:10 +00:00
.GetRecursiveChildren(user, new InternalItemsQuery(user)
{
2017-05-21 07:25:49 +00:00
IncludeItemTypes = new[] { typeof(Audio).Name },
DtoOptions = dtoOptions
2016-05-18 05:34:10 +00:00
})
2015-08-28 04:19:08 +00:00
.Cast<Audio>()
.SelectMany(i => i.Genres)
.Concat(item.Genres)
.DistinctNames();
2017-05-21 07:25:49 +00:00
return GetInstantMixFromGenres(genres, user, dtoOptions);
2015-08-28 04:19:08 +00:00
}
public List<BaseItem> GetInstantMixFromPlaylist(Playlist item, User user, DtoOptions dtoOptions)
2014-12-13 03:56:30 +00:00
{
2017-05-21 07:25:49 +00:00
return GetInstantMixFromGenres(item.Genres, user, dtoOptions);
}
public List<BaseItem> GetInstantMixFromGenres(IEnumerable<string> genres, User user, DtoOptions dtoOptions)
{
2017-04-27 18:12:44 +00:00
var genreIds = genres.DistinctNames().Select(i =>
{
try
{
2018-09-12 17:26:21 +00:00
return _libraryManager.GetMusicGenre(i).Id;
2017-04-27 18:12:44 +00:00
}
catch
{
2018-09-12 17:26:21 +00:00
return Guid.Empty;
2017-04-27 18:12:44 +00:00
}
2018-09-12 17:26:21 +00:00
}).Where(i => !i.Equals(Guid.Empty)).ToArray();
2017-05-21 07:25:49 +00:00
return GetInstantMixFromGenreIds(genreIds, user, dtoOptions);
2017-04-27 18:12:44 +00:00
}
2018-09-12 17:26:21 +00:00
public List<BaseItem> GetInstantMixFromGenreIds(Guid[] genreIds, User user, DtoOptions dtoOptions)
2017-04-27 18:12:44 +00:00
{
return _libraryManager.GetItemList(new InternalItemsQuery(user)
2015-10-19 15:33:49 +00:00
{
IncludeItemTypes = new[] { typeof(Audio).Name },
2017-04-27 18:12:44 +00:00
GenreIds = genreIds.ToArray(),
2015-10-19 15:33:49 +00:00
2017-04-27 18:12:44 +00:00
Limit = 200,
2015-10-19 15:33:49 +00:00
2019-10-20 14:08:40 +00:00
OrderBy = new[] { (ItemSortBy.Random, SortOrder.Ascending) },
2017-05-21 07:25:49 +00:00
DtoOptions = dtoOptions
});
}
public List<BaseItem> GetInstantMixFromItem(BaseItem item, User user, DtoOptions dtoOptions)
{
var genre = item as MusicGenre;
if (genre != null)
{
2018-09-12 17:26:21 +00:00
return GetInstantMixFromGenreIds(new[] { item.Id }, user, dtoOptions);
}
var playlist = item as Playlist;
if (playlist != null)
{
2017-05-21 07:25:49 +00:00
return GetInstantMixFromPlaylist(playlist, user, dtoOptions);
}
var album = item as MusicAlbum;
if (album != null)
{
2017-05-21 07:25:49 +00:00
return GetInstantMixFromAlbum(album, user, dtoOptions);
}
var artist = item as MusicArtist;
if (artist != null)
{
2017-05-21 07:25:49 +00:00
return GetInstantMixFromArtist(artist, user, dtoOptions);
}
var song = item as Audio;
if (song != null)
{
2017-05-21 07:25:49 +00:00
return GetInstantMixFromSong(song, user, dtoOptions);
}
2015-08-28 04:19:08 +00:00
var folder = item as Folder;
if (folder != null)
{
2017-05-21 07:25:49 +00:00
return GetInstantMixFromFolder(folder, user, dtoOptions);
2015-08-28 04:19:08 +00:00
}
2015-10-19 15:33:49 +00:00
return new List<BaseItem>();
}
}
}