2024-06-27 01:47:44 +02:00
|
|
|
using MediaBrowser.Model.Tasks;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using MediaBrowser.Controller;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.Playlists;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.IO;
|
2024-06-29 18:29:40 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Jellyfin.Data.Entities;
|
|
|
|
using Jellyfin.Data.Enums;
|
|
|
|
using MediaBrowser.Controller;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.Playlists;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
using MediaBrowser.Model.Playlists;
|
|
|
|
using MediaBrowser.Model.Tasks;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2024-06-27 01:47:44 +02:00
|
|
|
|
2024-10-24 23:53:21 +02:00
|
|
|
using Jellyfin.Plugin.SmartPlaylist.Lisp;
|
|
|
|
using Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler;
|
|
|
|
using Lisp_Object = Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler.Object;
|
|
|
|
using Lisp_Boolean = Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler.Boolean;
|
|
|
|
|
|
|
|
|
2024-06-27 01:47:44 +02:00
|
|
|
namespace Jellyfin.Plugin.SmartPlaylist.ScheduledTasks {
|
|
|
|
public class GeneratePlaylist : IScheduledTask {
|
2024-10-24 23:53:21 +02:00
|
|
|
|
|
|
|
public static readonly BaseItemKind[] AvailableFilterItems = {
|
|
|
|
BaseItemKind.Audio
|
|
|
|
};
|
|
|
|
|
2024-06-27 01:47:44 +02:00
|
|
|
private readonly ILogger _logger;
|
2024-06-29 18:29:40 +02:00
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
private readonly IUserManager _userManager;
|
2024-10-24 23:53:21 +02:00
|
|
|
private readonly IPlaylistManager _playlistManager;
|
|
|
|
|
|
|
|
private readonly IStore _store;
|
2024-06-29 18:29:40 +02:00
|
|
|
|
2024-06-27 01:47:44 +02:00
|
|
|
public GeneratePlaylist(
|
2024-06-29 18:29:40 +02:00
|
|
|
ILogger<Plugin> logger,
|
|
|
|
ILibraryManager libraryManager,
|
2024-10-24 23:53:21 +02:00
|
|
|
IUserManager userManager,
|
|
|
|
IPlaylistManager playlistManager,
|
|
|
|
IServerApplicationPaths serverApplicationPaths
|
2024-06-27 01:47:44 +02:00
|
|
|
) {
|
|
|
|
_logger = logger;
|
2024-06-29 18:29:40 +02:00
|
|
|
_libraryManager = libraryManager;
|
|
|
|
_userManager = userManager;
|
2024-10-24 23:53:21 +02:00
|
|
|
_playlistManager = playlistManager;
|
|
|
|
|
|
|
|
_store = new Store(new FileSystem(serverApplicationPaths));
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
2024-06-29 18:29:40 +02:00
|
|
|
|
2024-06-27 01:47:44 +02:00
|
|
|
public string Category => "Library";
|
|
|
|
public string Name => "(re)generate Smart Playlists";
|
|
|
|
public string Description => "Generate or regenerate all Smart Playlists";
|
|
|
|
public string Key => nameof(GeneratePlaylist);
|
2024-06-29 18:29:40 +02:00
|
|
|
|
2024-06-27 01:47:44 +02:00
|
|
|
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers() {
|
|
|
|
return new[] {
|
|
|
|
new TaskTriggerInfo {
|
2024-10-24 23:53:21 +02:00
|
|
|
IntervalTicks = TimeSpan.FromHours(24).Ticks,
|
2024-06-27 01:47:44 +02:00
|
|
|
Type = TaskTriggerInfo.TriggerInterval,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2024-06-29 18:29:40 +02:00
|
|
|
|
|
|
|
private void GetUsers() {
|
|
|
|
foreach (var user in _userManager.Users) {
|
|
|
|
_logger.LogInformation("User {0}", user);
|
|
|
|
var query = new InternalItemsQuery(user) {
|
2024-10-24 23:53:21 +02:00
|
|
|
IncludeItemTypes = AvailableFilterItems,
|
2024-06-29 18:29:40 +02:00
|
|
|
Recursive = true,
|
|
|
|
};
|
|
|
|
foreach (BaseItem item in _libraryManager.GetItemsResult(query).Items) {
|
|
|
|
_logger.LogInformation("Item {0}", item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-24 23:53:21 +02:00
|
|
|
private SmartPlaylistId CreateNewPlaylist(SmartPlaylistDto dto, User user) {
|
|
|
|
var req = new PlaylistCreationRequest {
|
|
|
|
Name = dto.Name,
|
|
|
|
UserId = user.Id
|
|
|
|
};
|
|
|
|
return Guid.Parse(_playlistManager.CreatePlaylist(req).Result.Id);
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerable<Guid> FilterPlaylistItems(IEnumerable<BaseItem> items, User user, SmartPlaylistDto smartPlaylist) {
|
|
|
|
List<Guid> results = new List<Guid>();
|
|
|
|
Expression expression = new Parser(StringTokenStream.generate(smartPlaylist.Program)).parse();
|
|
|
|
Executor executor = new Executor();
|
|
|
|
foreach (var i in items) {
|
|
|
|
executor.environment["item"] = new Lisp_Object(i);
|
|
|
|
var r = executor.eval(expression);
|
|
|
|
_logger.LogInformation("Item {0} evaluated to {1}", i, r.ToString());
|
|
|
|
if (r is Lisp_Boolean r_bool) {
|
|
|
|
if (r_bool.value) { results.Add(i.Id); }
|
|
|
|
} else {
|
|
|
|
_logger.LogInformation("Program did not return a boolean, returned {0}", r.ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerable<BaseItem> GetAllUserMedia(User user) {
|
|
|
|
var req = new InternalItemsQuery(user) {
|
|
|
|
IncludeItemTypes = new[] {BaseItemKind.Audio},
|
|
|
|
Recursive = true,
|
|
|
|
};
|
|
|
|
return _libraryManager.GetItemsResult(req).Items;
|
|
|
|
}
|
|
|
|
|
2024-06-27 01:47:44 +02:00
|
|
|
public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken) {
|
2024-10-24 23:53:21 +02:00
|
|
|
_logger.LogInformation("Started regenerate Smart Playlists");
|
|
|
|
foreach (SmartPlaylistDto dto in await _store.GetAllSmartPlaylistsAsync()) {
|
|
|
|
var user = _userManager.GetUserById(dto.User);
|
|
|
|
List<Playlist> playlists = _playlistManager.GetPlaylists(user.Id).Where(x => x.Id == dto.Id).ToList();
|
|
|
|
if ((dto.Id == null) || !playlists.Any()) {
|
|
|
|
_logger.LogInformation("Generating new smart playlist (dto.Id = {0}, playlists.Any() = {1})", dto.Id, playlists.Any());
|
|
|
|
_store.DeleteSmartPlaylist(dto.Id);
|
|
|
|
dto.Id = CreateNewPlaylist(dto, user);
|
|
|
|
await _store.SaveSmartPlaylistAsync(dto);
|
|
|
|
playlists = _playlistManager.GetPlaylists(user.Id).Where(x => x.Id == dto.Id).ToList();
|
|
|
|
}
|
|
|
|
var insertItems = FilterPlaylistItems(GetAllUserMedia(user), user, dto);
|
|
|
|
Playlist playlist = playlists.First();
|
|
|
|
await ClearPlaylist(dto, playlist, user);
|
|
|
|
await _playlistManager.AddItemToPlaylistAsync(playlist.Id, insertItems.ToArray(), user.Id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task ClearPlaylist(SmartPlaylistDto smartPlaylist, Playlist playlist, User user) {
|
|
|
|
var req = new InternalItemsQuery(user)
|
|
|
|
{
|
|
|
|
IncludeItemTypes = AvailableFilterItems,
|
|
|
|
Recursive = true
|
|
|
|
};
|
|
|
|
var existingItems = playlist.GetChildren(user, false, req).Select(x => x.Id.ToString()).ToList();
|
|
|
|
await _playlistManager.RemoveItemFromPlaylistAsync(playlist.Id.ToString(), existingItems);
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|