From ef710bf00b28c5ce8a57f031a71f85be93fb968c Mon Sep 17 00:00:00 2001 From: Mrs4s Date: Sun, 30 Jul 2023 03:27:29 +0800 Subject: [PATCH] feat: ignore less than 5min file --- .../Configuration/ConfigPage.html | 8 +++++ .../Configuration/PluginConfiguration.cs | 2 ++ .../Jellyfin.Plugin.Bangumi.csproj | 15 ++++---- .../Patch/MediaInfoPatch.cs | 21 +++++++++++ Jellyfin.Plugin.Bangumi/Plugin.cs | 24 +++++++++++++ Jellyfin.Plugin.Bangumi/Rule/IgnoreRule.cs | 36 +++++++++++++++++++ 6 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 Jellyfin.Plugin.Bangumi/Patch/MediaInfoPatch.cs create mode 100644 Jellyfin.Plugin.Bangumi/Rule/IgnoreRule.cs diff --git a/Jellyfin.Plugin.Bangumi/Configuration/ConfigPage.html b/Jellyfin.Plugin.Bangumi/Configuration/ConfigPage.html index 9f17883..81c58bd 100644 --- a/Jellyfin.Plugin.Bangumi/Configuration/ConfigPage.html +++ b/Jellyfin.Plugin.Bangumi/Configuration/ConfigPage.html @@ -194,6 +194,14 @@

元数据

+
+
+ +
+

AnitomySharp

diff --git a/Jellyfin.Plugin.Bangumi/Configuration/PluginConfiguration.cs b/Jellyfin.Plugin.Bangumi/Configuration/PluginConfiguration.cs index 5b5b493..ef35efd 100644 --- a/Jellyfin.Plugin.Bangumi/Configuration/PluginConfiguration.cs +++ b/Jellyfin.Plugin.Bangumi/Configuration/PluginConfiguration.cs @@ -31,4 +31,6 @@ public class PluginConfiguration : BasePluginConfiguration public bool AlwaysGetEpisodeByAnitomySharp { get; set; } public bool UseTestingSearchApi { get; set; } + + public bool IgnoreLessThanFiveMinutes { get; set; } } \ No newline at end of file diff --git a/Jellyfin.Plugin.Bangumi/Jellyfin.Plugin.Bangumi.csproj b/Jellyfin.Plugin.Bangumi/Jellyfin.Plugin.Bangumi.csproj index 5997ee4..169e819 100644 --- a/Jellyfin.Plugin.Bangumi/Jellyfin.Plugin.Bangumi.csproj +++ b/Jellyfin.Plugin.Bangumi/Jellyfin.Plugin.Bangumi.csproj @@ -10,18 +10,19 @@ - - - - + + + + + - - + + - + \ No newline at end of file diff --git a/Jellyfin.Plugin.Bangumi/Patch/MediaInfoPatch.cs b/Jellyfin.Plugin.Bangumi/Patch/MediaInfoPatch.cs new file mode 100644 index 0000000..38e1dfe --- /dev/null +++ b/Jellyfin.Plugin.Bangumi/Patch/MediaInfoPatch.cs @@ -0,0 +1,21 @@ +using HarmonyLib; +using MediaBrowser.Model.MediaInfo; + +namespace Jellyfin.Plugin.Bangumi.Patch; + +[HarmonyPatch("MediaBrowser.MediaEncoding.Probing.ProbeResultNormalizer", "GetMediaInfo")] +public class MediaInfoPatch +{ + internal static void Postfix(bool isAudio, string path, MediaInfo __result) + { + if (isAudio) return; + if (__result.RunTimeTicks is not > 0) return; + if (Plugin.Instance!.MediaTicks.TryGetValue(path, out var ticks) && ticks == __result.RunTimeTicks.Value) + return; + if (ticks == 0) + Plugin.Instance.MediaTicks.Add(path, __result.RunTimeTicks.Value); + else + Plugin.Instance.MediaTicks[path] = __result.RunTimeTicks.Value; + Plugin.Instance.SaveCache(); + } +} \ No newline at end of file diff --git a/Jellyfin.Plugin.Bangumi/Plugin.cs b/Jellyfin.Plugin.Bangumi/Plugin.cs index 6ce09c4..2132ce7 100644 --- a/Jellyfin.Plugin.Bangumi/Plugin.cs +++ b/Jellyfin.Plugin.Bangumi/Plugin.cs @@ -1,20 +1,28 @@ using System; using System.Collections.Generic; +using System.IO; +using HarmonyLib; using Jellyfin.Plugin.Bangumi.Configuration; using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Plugins; using MediaBrowser.Model.Plugins; using MediaBrowser.Model.Serialization; +using Newtonsoft.Json; namespace Jellyfin.Plugin.Bangumi; public class Plugin : BasePlugin, IHasWebPages { public static Plugin? Instance; + + public Dictionary MediaTicks = new(); public Plugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer) : base(applicationPaths, xmlSerializer) { Instance = this; + LoadCache(); + var harmony = new Harmony("jellyfin.plugin.bangumi"); + harmony.PatchAll(); } /// @@ -38,4 +46,20 @@ public IEnumerable GetPages() } }; } + + public void LoadCache() + { + var ticksPath = Path.Combine(ApplicationPaths.CachePath, "bangumi", "ticks.json"); + if (File.Exists(ticksPath)) + MediaTicks = JsonConvert.DeserializeObject>(File.ReadAllText(ticksPath)); + } + + public void SaveCache() + { + var json = JsonConvert.SerializeObject(MediaTicks); + var path = Path.Combine(ApplicationPaths.CachePath, "bangumi"); + if (!Directory.Exists(path)) + Directory.CreateDirectory(path); + File.WriteAllText(Path.Combine(path, "ticks.json"), json); + } } \ No newline at end of file diff --git a/Jellyfin.Plugin.Bangumi/Rule/IgnoreRule.cs b/Jellyfin.Plugin.Bangumi/Rule/IgnoreRule.cs new file mode 100644 index 0000000..f7bc4e2 --- /dev/null +++ b/Jellyfin.Plugin.Bangumi/Rule/IgnoreRule.cs @@ -0,0 +1,36 @@ +using System; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Resolvers; +using MediaBrowser.Model.IO; +using Microsoft.Extensions.Logging; + +namespace Jellyfin.Plugin.Bangumi.Rule; + +public class IgnoreRule : IResolverIgnoreRule +{ + private readonly ILogger _logger; + + public IgnoreRule(ILogger logger) + { + _logger = logger; + } + + public bool ShouldIgnore(FileSystemMetadata fileInfo, BaseItem parent) + { + if (!Plugin.Instance!.Configuration.IgnoreLessThanFiveMinutes) return false; + if (!Plugin.Instance.MediaTicks.TryGetValue(fileInfo.FullName, out var ticks)) + { + _logger.LogDebug($"processing file {fileInfo.FullName} error: unknown ticks"); + return false; + } + + var span = TimeSpan.FromTicks(ticks); + if (span.TotalMinutes < 5) + { + _logger.LogInformation($"Ignore file {fileInfo.FullName}. because duration {span.TotalMinutes} min < 5 min"); + return true; + } + + return false; + } +} \ No newline at end of file