From b441a098319a428ebcb455f95b8914992381a01d Mon Sep 17 00:00:00 2001 From: redxef Date: Sun, 22 Dec 2024 18:16:45 +0100 Subject: [PATCH] fix: initialize the executor the same for the filter programs and the sort program. --- .../ScheduledTasks/GeneratePlaylist.cs | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Jellyfin.Plugin.SmartPlaylist/ScheduledTasks/GeneratePlaylist.cs b/Jellyfin.Plugin.SmartPlaylist/ScheduledTasks/GeneratePlaylist.cs index f56c9f8..b472268 100644 --- a/Jellyfin.Plugin.SmartPlaylist/ScheduledTasks/GeneratePlaylist.cs +++ b/Jellyfin.Plugin.SmartPlaylist/ScheduledTasks/GeneratePlaylist.cs @@ -94,11 +94,9 @@ namespace Jellyfin.Plugin.SmartPlaylist.ScheduledTasks { return playlistGuid; } - private IEnumerable FilterPlaylistItems(IEnumerable items, User user, SmartPlaylistDto smartPlaylist) { - List results = new List(); - Expression expression = new Parser(StringTokenStream.generate(smartPlaylist.Program)).parse(); // parse here, so that we don't repeat the work for each item - Executor executor = new Executor(new DefaultEnvironment()); - + private Executor SetupExecutor() { + var env = new DefaultEnvironment(); + var executor = new Executor(env); executor.builtins["logd"] = (x) => { _logger.LogDebug(((Lisp.String)x.First()).Value(), x.Skip(1).ToArray()); return Lisp_Boolean.TRUE; @@ -115,13 +113,20 @@ namespace Jellyfin.Plugin.SmartPlaylist.ScheduledTasks { _logger.LogError(((Lisp.String)x.First()).Value(), x.Skip(1).ToArray()); return Lisp_Boolean.TRUE; }; - - executor.environment.Set("user", Lisp_Object.FromBase(user)); if (Plugin.Instance is not null) { executor.eval(Plugin.Instance.Configuration.InitialProgram); } else { throw new ApplicationException("Plugin Instance is not yet initialized"); } + return executor; + } + + private IEnumerable FilterPlaylistItems(IEnumerable items, User user, SmartPlaylistDto smartPlaylist) { + List results = new List(); + Expression expression = new Parser(StringTokenStream.generate(smartPlaylist.Program)).parse(); // parse here, so that we don't repeat the work for each item + Executor executor = SetupExecutor(); + + executor.environment.Set("user", Lisp_Object.FromBase(user)); foreach (var i in items) { executor.environment.Set("item", Lisp_Object.FromBase(i)); var r = executor.eval(expression); @@ -131,7 +136,7 @@ namespace Jellyfin.Plugin.SmartPlaylist.ScheduledTasks { results.Add(i); } } - executor = new Executor(new DefaultEnvironment()); + executor = SetupExecutor(); executor.environment.Set("user", Lisp_Object.FromBase(user)); executor.environment.Set("items", Lisp_Object.FromBase(results)); results = new List();