Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 忽略小于五分钟的文件 #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Jellyfin.Plugin.Bangumi/Configuration/ConfigPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ <h2 class="sectionTitle">元数据</h2>
</label>
</div>
</div>
<div class="selectContainer">
<div class="checkboxContainer checkboxContainer-withDescription">
<label class="emby-checkbox-label">
<input id="IgnoreLessThanFiveMinutes" is="emby-checkbox" type="checkbox"/>
<span>屏蔽时长小于五分钟的文件</span>
</label>
</div>
</div>
<div class="verticalSection verticalSection">
<div class="sectionTitleContainer flex align-items-center">
<h2 class="sectionTitle">AnitomySharp</h2>
Expand Down
2 changes: 2 additions & 0 deletions Jellyfin.Plugin.Bangumi/Configuration/PluginConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ public class PluginConfiguration : BasePluginConfiguration
public bool AlwaysGetEpisodeByAnitomySharp { get; set; }

public bool UseTestingSearchApi { get; set; }

public bool IgnoreLessThanFiveMinutes { get; set; }
}
15 changes: 8 additions & 7 deletions Jellyfin.Plugin.Bangumi/Jellyfin.Plugin.Bangumi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AnitomySharp" Version="0.2.0"/>
<PackageReference Include="Fastenshtein" Version="1.0.0.8"/>
<PackageReference Include="Jellyfin.Controller" IncludeAssets="compile" Version="10.8.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authorization" IncludeAssets="compile" Version="6.0.12"/>
<PackageReference Include="AnitomySharp" Version="0.2.0" />
<PackageReference Include="Fastenshtein" Version="1.0.0.8" />
<PackageReference Include="Jellyfin.Controller" IncludeAssets="compile" Version="10.8.0" />
<PackageReference Include="Lib.Harmony" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" IncludeAssets="compile" Version="6.0.12" />
</ItemGroup>

<ItemGroup>
<None Remove="Configuration\configPage.html"/>
<EmbeddedResource Include="Configuration\ConfigPage.html"/>
<None Remove="Configuration\configPage.html" />
<EmbeddedResource Include="Configuration\ConfigPage.html" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="Jellyfin.Plugin.Bangumi.Test"/>
<InternalsVisibleTo Include="Jellyfin.Plugin.Bangumi.Test" />
</ItemGroup>
</Project>
21 changes: 21 additions & 0 deletions Jellyfin.Plugin.Bangumi/Patch/MediaInfoPatch.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
24 changes: 24 additions & 0 deletions Jellyfin.Plugin.Bangumi/Plugin.cs
Original file line number Diff line number Diff line change
@@ -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<PluginConfiguration>, IHasWebPages
{
public static Plugin? Instance;

public Dictionary<string, long> MediaTicks = new();

public Plugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer) : base(applicationPaths, xmlSerializer)
{
Instance = this;
LoadCache();
var harmony = new Harmony("jellyfin.plugin.bangumi");
harmony.PatchAll();
}

/// <inheritdoc />
Expand All @@ -38,4 +46,20 @@ public IEnumerable<PluginPageInfo> GetPages()
}
};
}

public void LoadCache()
{
var ticksPath = Path.Combine(ApplicationPaths.CachePath, "bangumi", "ticks.json");
if (File.Exists(ticksPath))
MediaTicks = JsonConvert.DeserializeObject<Dictionary<string, long>>(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);
}
}
36 changes: 36 additions & 0 deletions Jellyfin.Plugin.Bangumi/Rule/IgnoreRule.cs
Original file line number Diff line number Diff line change
@@ -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<IgnoreRule> _logger;

public IgnoreRule(ILogger<IgnoreRule> 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;
}
}