mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-09-06 05:47:14 -04:00
68 lines
2.2 KiB
C#
68 lines
2.2 KiB
C#
|
using MediaBrowser.Controller.Entities;
|
|||
|
using MediaBrowser.Controller.Entities.Audio;
|
|||
|
using MediaBrowser.Controller.Library;
|
|||
|
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)
|
|||
|
{
|
|||
|
return GetInstantMixFromGenres(item.Genres, user);
|
|||
|
}
|
|||
|
|
|||
|
public IEnumerable<Audio> GetInstantMixFromArtist(string name, User user)
|
|||
|
{
|
|||
|
var artist = _libraryManager.GetArtist(name);
|
|||
|
|
|||
|
var genres = _libraryManager.RootFolder
|
|||
|
.RecursiveChildren
|
|||
|
.OfType<Audio>()
|
|||
|
.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
|
|||
|
.RecursiveChildren
|
|||
|
.OfType<Audio>()
|
|||
|
.SelectMany(i => i.Genres)
|
|||
|
.Concat(item.Genres)
|
|||
|
.Distinct(StringComparer.OrdinalIgnoreCase);
|
|||
|
|
|||
|
return GetInstantMixFromGenres(genres, user);
|
|||
|
}
|
|||
|
|
|||
|
public IEnumerable<Audio> GetInstantMixFromGenres(IEnumerable<string> genres, User user)
|
|||
|
{
|
|||
|
var inputItems = user.RootFolder.GetRecursiveChildren(user);
|
|||
|
|
|||
|
var genresDictionary = genres.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
|
|||
|
|
|||
|
return inputItems
|
|||
|
.OfType<Audio>()
|
|||
|
.Select(i => new Tuple<Audio, int>(i, i.Genres.Count(genresDictionary.ContainsKey)))
|
|||
|
.OrderByDescending(i => i.Item2)
|
|||
|
.ThenBy(i => Guid.NewGuid())
|
|||
|
.Select(i => i.Item1)
|
|||
|
.Take(100)
|
|||
|
.OrderBy(i => Guid.NewGuid());
|
|||
|
}
|
|||
|
}
|
|||
|
}
|