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

Optionally use the AnitomySharp library to extract media titles #9

Merged
merged 8 commits into from
Nov 9, 2023
4 changes: 4 additions & 0 deletions Jellyfin.Plugin.AniList/Configuration/PluginConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public PluginConfiguration()
AniDbRateLimit = 2000;
AniDbReplaceGraves = true;
AniListShowSpoilerTags = true;
UseAnitomyLibrary = false;
}

public TitlePreferenceType TitlePreference { get; set; }
Expand All @@ -53,5 +54,8 @@ public PluginConfiguration()
public bool AniListShowSpoilerTags { get; set; }

public bool FilterPeopleByTitlePreference { get; set; }

public bool UseAnitomyLibrary { get; set; }

}
}
8 changes: 8 additions & 0 deletions Jellyfin.Plugin.AniList/Configuration/configPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
<span>Display Tags that are marked as spoilers</span>
</label>
</div>
<div class="checkboxContainer checkboxContainer-withDescription">
<label class="emby-checkbox-label">
<input id="chkUseAnitomyLibrary" name="chkUseAnitomyLibrary" type="checkbox" is="emby-checkbox" />
<span>Use the Anitomy library to resolve titles</span>
</label>
</div>
<div>
<button is="emby-button" type="submit" class="raised button-submit block emby-button">
<span>Save</span>
Expand All @@ -87,6 +93,7 @@
document.getElementById('chkAniDbRateLimit').value = config.AniDbRateLimit;
document.getElementById('chkAniDbReplaceGraves').checked = config.AniDbReplaceGraves;
document.getElementById('chkAniListShowSpoilerTags').checked = config.AniListShowSpoilerTags;
document.getElementById('chkUseAnitomyLibrary').checked = config.UseAnitomyLibrary;

Dashboard.hideLoadingMsg();
});
Expand All @@ -104,6 +111,7 @@
config.AniDbRateLimit = document.getElementById('chkAniDbRateLimit').value;
config.AniDbReplaceGraves = document.getElementById('chkAniDbReplaceGraves').checked;
config.AniListShowSpoilerTags = document.getElementById('chkAniListShowSpoilerTags').checked;
config.UseAnitomyLibrary = document.getElementById('chkUseAnitomyLibrary').checked;

ApiClient.updatePluginConfiguration(AniListConfigurationPage.pluginUniqueId, config).then(function (result) {
Dashboard.processPluginConfigurationUpdateResult(result);
Expand Down
1 change: 1 addition & 0 deletions Jellyfin.Plugin.AniList/Jellyfin.Plugin.AniList.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageReference Include="Jellyfin.Controller" Version="10.*-*" />
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="AnitomySharp.NET6" Version="0.4.0" />
</ItemGroup>

<ItemGroup>
Expand Down
29 changes: 25 additions & 4 deletions Jellyfin.Plugin.AniList/Providers/AniList/AniListMovieProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
using MediaBrowser.Model.Providers;
using MediaBrowser.Model.Serialization;
using Microsoft.Extensions.Logging;
using Jellyfin.Plugin.AniList.Configuration;


//API v2
namespace Jellyfin.Plugin.AniList.Providers.AniList
Expand All @@ -37,6 +39,7 @@ public async Task<MetadataResult<Movie>> GetMetadata(MovieInfo info, Cancellatio
{
var result = new MetadataResult<Movie>();
Media media = null;
PluginConfiguration config = Plugin.Instance.Configuration;

var aid = info.ProviderIds.GetOrDefault(ProviderNames.AniList);
if (!string.IsNullOrEmpty(aid))
Expand All @@ -45,11 +48,29 @@ public async Task<MetadataResult<Movie>> GetMetadata(MovieInfo info, Cancellatio
}
else
{
_log.LogInformation("Start AniList... Searching({Name})", info.Name);
MediaSearchResult msr = await _aniListApi.Search_GetSeries(info.Name, cancellationToken);
if (msr != null)
var searchName = info.Name;
MediaSearchResult msr;
if(config.UseAnitomyLibrary)
{//Use Anitomy to extract the title
searchName = Anitomy.AnitomyHelper.ExtractAnimeTitle(searchName);
searchName = AnilistSearchHelper.PreprocessTitle(searchName);
_log.LogInformation("Start AniList... Searching({Name})", searchName);
msr = await _aniListApi.Search_GetSeries(searchName, cancellationToken);
if (msr != null)
{
media = await _aniListApi.GetAnime(msr.id.ToString());
}
}
if(!config.UseAnitomyLibrary || media == null)
{
media = await _aniListApi.GetAnime(msr.id.ToString());
searchName = info.Name;
searchName = AnilistSearchHelper.PreprocessTitle(searchName);
_log.LogInformation("Start AniList... Searching({Name})", searchName);
msr = await _aniListApi.Search_GetSeries(searchName, cancellationToken);
if (msr != null)
{
media = await _aniListApi.GetAnime(msr.id.ToString());
}
}
}

Expand Down
35 changes: 35 additions & 0 deletions Jellyfin.Plugin.AniList/Providers/AniList/AniListSearchHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;


namespace Jellyfin.Plugin.AniList.Providers.AniList
{
public class AnilistSearchHelper
{
public static String PreprocessTitle(String path)
{ //Remove items that will always cause anilist to fail
String input = path;

//Season designation
input = Regex.Replace(
input,
"(\\s|\\.)S\\d{1,2}", String.Empty);
MisterMcDuck marked this conversation as resolved.
Show resolved Hide resolved
// ~ ALT NAME ~
input = Regex.Replace(
input,
"\\s*~(\\w|\\d|\\s)+~", String.Empty);
MisterMcDuck marked this conversation as resolved.
Show resolved Hide resolved

// Native Name (English Name)
// Only replaces if the name ends with a parenthesis to hopefully avoid mangling titles with parens (e.g. Evangelion 1.11 You Are (Not) Alone)
input = Regex.Replace(
input.Trim(),
"\\((\\w|\\d|\\s)+\\)$", String.Empty);
MisterMcDuck marked this conversation as resolved.
Show resolved Hide resolved


return input;
}
}
}
29 changes: 25 additions & 4 deletions Jellyfin.Plugin.AniList/Providers/AniList/AniListSeriesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using MediaBrowser.Model.Providers;
using MediaBrowser.Model.Serialization;
using Microsoft.Extensions.Logging;
using Jellyfin.Plugin.AniList.Configuration;

//API v2
namespace Jellyfin.Plugin.AniList.Providers.AniList
Expand All @@ -36,6 +37,7 @@ public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo info, Cancellat
{
var result = new MetadataResult<Series>();
Media media = null;
PluginConfiguration config = Plugin.Instance.Configuration;

var aid = info.ProviderIds.GetOrDefault(ProviderNames.AniList);
if (!string.IsNullOrEmpty(aid))
Expand All @@ -44,11 +46,30 @@ public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo info, Cancellat
}
else
{
_log.LogInformation("Start AniList... Searching({Name})", info.Name);
MediaSearchResult msr = await _aniListApi.Search_GetSeries(info.Name, cancellationToken);
if (msr != null)
var searchName = info.Name;
MediaSearchResult msr;
if(config.UseAnitomyLibrary)
{ //Use Anitomy to extract the title
searchName = Anitomy.AnitomyHelper.ExtractAnimeTitle(searchName);
//todo Add episode/season?
searchName = AnilistSearchHelper.PreprocessTitle(searchName);
_log.LogInformation("Start AniList... Searching({Name})", searchName);
msr = await _aniListApi.Search_GetSeries(searchName, cancellationToken);
if (msr != null)
{
media = await _aniListApi.GetAnime(msr.id.ToString());
}
}
if(!config.UseAnitomyLibrary || media == null)
{
media = await _aniListApi.GetAnime(msr.id.ToString());
searchName = info.Name;
searchName = AnilistSearchHelper.PreprocessTitle(searchName);
_log.LogInformation("Start AniList... Searching({Name})", searchName);
msr = await _aniListApi.Search_GetSeries(info.Name, cancellationToken);
if (msr != null)
{
media = await _aniListApi.GetAnime(msr.id.ToString());
}
}
}

Expand Down
35 changes: 35 additions & 0 deletions Jellyfin.Plugin.AniList/Providers/Anitomy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

using AnitomySharp;

namespace Jellyfin.Plugin.AniList.Anitomy
{
public class AnitomyHelper
{
public static String ExtractAnimeTitle(string path)
{
String input = path;
var elements = AnitomySharp.AnitomySharp.Parse(input);
return elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementAnimeTitle).Value;
}
public static String ExtractEpisodeTitle(string path)
{
var elements = AnitomySharp.AnitomySharp.Parse(path);
return elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementEpisodeTitle).Value;
}
public static String ExtractEpisodeNumber(string path)
{
var elements = AnitomySharp.AnitomySharp.Parse(path);
return elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementEpisodeNumber).Value;
}
public static String ExtractSeasonNumber(string path)
{
var elements = AnitomySharp.AnitomySharp.Parse(path);
return elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementAnimeSeason).Value;
}
}
}