Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
kookxiang committed Jul 17, 2022
1 parent 5a30fd7 commit c9a8d9f
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
using System.Collections.Generic;
using System.Linq;
using AnitomySharp;

namespace Jellyfin.Plugin.Bangumi.Utils;

public class AnitomyHelper
{
public static List<Element> ElementsOutput(string path)
{
return new List<Element>(AnitomySharp.AnitomySharp.Parse(path));
}

public static string ExtractAnimeTitle(string path)
{
var elements = AnitomySharp.AnitomySharp.Parse(path);
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;
}
using System.Collections.Generic;
using System.Linq;
using AnitomySharp;

namespace Jellyfin.Plugin.Bangumi;

public class Anitomy
{
public static List<Element> ElementsOutput(string path)
{
return new List<Element>(AnitomySharp.AnitomySharp.Parse(path));
}

public static string? ExtractAnimeTitle(string path)
{
var elements = AnitomySharp.AnitomySharp.Parse(path);
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;
}
}
44 changes: 26 additions & 18 deletions Jellyfin.Plugin.Bangumi/Configuration/ConfigPage.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
Expand Down Expand Up @@ -124,26 +124,34 @@ <h2 class="sectionTitle">元数据</h2>
<option id="optLanguageChinese" value="Chinese">优先使用中文翻译</option>
</select>
</div>
<div class="checkboxContainer checkboxContainer-withDescription">
<label class="emby-checkbox-label">
<input id="AlwaysGetTitleByAnitomySharp" is="emby-checkbox" type="checkbox"/>
<span>始终使用AnitomySharp猜测动画名</span>
</label>
</div>
<div class="selectContainer">
<label class="selectLabel" for="TranslationPreference">集数纠正</label>
<div class="checkboxContainer checkboxContainer-withDescription">
<label class="emby-checkbox-label">
<input id="AlwaysReplaceEpisodeNumber" 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>
</div>
</div>
<div class="checkboxContainer checkboxContainer-withDescription">
<label class="emby-checkbox-label">
<input id="AlwaysReplaceEpisodeNumber" is="emby-checkbox" type="checkbox"/>
<span>始终根据文件名猜测集数</span>
</label>
<div class="selectContainer">
<div class="checkboxContainer checkboxContainer-withDescription">
<label class="emby-checkbox-label">
<input id="AlwaysGetEpisodeByAnitomySharp" is="emby-checkbox" type="checkbox"/>
<span>使用 AnitomySharp 猜测集数</span>
</label>
</div>
</div>
<div class="checkboxContainer checkboxContainer-withDescription">
<label class="emby-checkbox-label">
<input id="AlwaysGetEpisodeByAnitomySharp" is="emby-checkbox" type="checkbox"/>
<span>始终使用AnitomySharp猜测集数</span>
</label>
<div class="selectContainer">
<div class="checkboxContainer checkboxContainer-withDescription">
<label class="emby-checkbox-label">
<input id="AlwaysGetTitleByAnitomySharp" is="emby-checkbox" type="checkbox"/>
<span>使用 AnitomySharp 猜测动画名</span>
</label>
</div>
</div>
<div>
<button class="raised button-submit block emby-button" is="emby-button" type="submit">
Expand Down
11 changes: 7 additions & 4 deletions Jellyfin.Plugin.Bangumi/Providers/EpisodeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.Bangumi.Model;
using Jellyfin.Plugin.Bangumi.Utils;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
Expand Down Expand Up @@ -198,8 +197,12 @@ private double GuessEpisodeNumber(double? current, string fileName, double max =
var episodeIndex = current ?? 0;
var episodeIndexFromFilename = episodeIndex;

// 临时测试,待改造 #TODO
if (_plugin.Configuration.AlwaysGetEpisodeByAnitomySharp) return double.Parse(AnitomyHelper.ExtractEpisodeNumber(fileName));
if (_plugin.Configuration.AlwaysGetEpisodeByAnitomySharp)
{
var anitomyIndex = Anitomy.ExtractEpisodeNumber(fileName);
if (!string.IsNullOrEmpty(anitomyIndex))
return double.Parse(anitomyIndex);
}

foreach (var regex in NonEpisodeFileNameRegex)
{
Expand All @@ -216,7 +219,7 @@ private double GuessEpisodeNumber(double? current, string fileName, double max =
break;
}

if (_plugin.Configuration.AlwaysReplaceEpisodeNumber && episodeIndexFromFilename != episodeIndex)
if (_plugin.Configuration.AlwaysReplaceEpisodeNumber && !episodeIndexFromFilename.Equals(episodeIndex))
{
_log.LogWarning("use episode index {NewIndex} instead of {Index} for {FileName}",
episodeIndexFromFilename, episodeIndex, fileName);
Expand Down
8 changes: 5 additions & 3 deletions Jellyfin.Plugin.Bangumi/Providers/MovieProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.Bangumi.Configuration;
using Jellyfin.Plugin.Bangumi.Model;
using Jellyfin.Plugin.Bangumi.Utils;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
Expand All @@ -28,6 +28,8 @@ public MovieProvider(Plugin plugin, BangumiApi api, ILogger<MovieProvider> logge

private HttpClient HttpClient => _plugin.GetHttpClient();

private PluginConfiguration Configuration => _plugin.Configuration;

public int Order => -5;
public string Name => Constants.ProviderName;

Expand All @@ -39,7 +41,7 @@ public async Task<MetadataResult<Movie>> GetMetadata(MovieInfo info, Cancellatio
var subjectId = info.ProviderIds.GetOrDefault(Constants.ProviderName);
if (string.IsNullOrEmpty(subjectId))
{
var searchName = BangumiHelper.NameHelper(info.Name, _plugin);
var searchName = Configuration.AlwaysGetTitleByAnitomySharp ? Anitomy.ExtractAnimeTitle(info.Path) ?? info.Name : info.Name;
_log.LogInformation("Searching {Name} in bgm.tv", searchName);
var searchResult = await _api.SearchSubject(searchName, token);
searchResult = Subject.SortBySimilarity(searchResult, searchName);
Expand All @@ -52,7 +54,7 @@ public async Task<MetadataResult<Movie>> GetMetadata(MovieInfo info, Cancellatio
// try search OriginalTitle
if (string.IsNullOrEmpty(subjectId) && info.OriginalTitle != null && !string.Equals(info.OriginalTitle, info.Name, StringComparison.Ordinal))
{
var searchName = BangumiHelper.NameHelper(info.OriginalTitle, _plugin);
var searchName = Configuration.AlwaysGetTitleByAnitomySharp ? Anitomy.ExtractAnimeTitle(info.Path) ?? info.OriginalTitle : info.OriginalTitle;
_log.LogInformation("Searching {Name} in bgm.tv", searchName);
var searchResult = await _api.SearchSubject(searchName, token);
searchResult = Subject.SortBySimilarity(searchResult, searchName);
Expand Down
9 changes: 6 additions & 3 deletions Jellyfin.Plugin.Bangumi/Providers/SeriesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.Bangumi.Configuration;
using Jellyfin.Plugin.Bangumi.Model;
using Jellyfin.Plugin.Bangumi.Utils;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
Expand All @@ -26,7 +26,10 @@ public SeriesProvider(Plugin plugin, BangumiApi api, ILogger<SeriesProvider> log
_log = log;
}

private PluginConfiguration Configuration => _plugin.Configuration;

public int Order => -5;

public string Name => Constants.ProviderName;

public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo info, CancellationToken token)
Expand All @@ -37,7 +40,7 @@ public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo info, Cancellat
var subjectId = info.ProviderIds.GetOrDefault(Constants.ProviderName);
if (string.IsNullOrEmpty(subjectId))
{
var searchName = BangumiHelper.NameHelper(info.Name, _plugin);
var searchName = Configuration.AlwaysGetTitleByAnitomySharp ? Anitomy.ExtractAnimeTitle(info.Path) ?? info.Name : info.Name;
_log.LogInformation("Searching {Name} in bgm.tv", searchName);
var searchResult = await _api.SearchSubject(searchName, token);
searchResult = Subject.SortBySimilarity(searchResult, searchName);
Expand All @@ -50,7 +53,7 @@ public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo info, Cancellat
// try search OriginalTitle
if (string.IsNullOrEmpty(subjectId) && info.OriginalTitle != null && !string.Equals(info.OriginalTitle, info.Name, StringComparison.Ordinal))
{
var searchName = BangumiHelper.NameHelper(info.OriginalTitle, _plugin);
var searchName = Configuration.AlwaysGetTitleByAnitomySharp ? Anitomy.ExtractAnimeTitle(info.Path) ?? info.OriginalTitle : info.OriginalTitle;
_log.LogInformation("Searching {Name} in bgm.tv", searchName);
var searchResult = await _api.SearchSubject(searchName, token);
searchResult = Subject.SortBySimilarity(searchResult, searchName);
Expand Down
11 changes: 0 additions & 11 deletions Jellyfin.Plugin.Bangumi/Utils/BangumiHelper.cs

This file was deleted.

0 comments on commit c9a8d9f

Please sign in to comment.