Skip to content

Commit

Permalink
feat: update Anitomy
Browse files Browse the repository at this point in the history
  • Loading branch information
chu-shen authored and kookxiang committed Oct 6, 2024
1 parent 045e2d5 commit 9f6703a
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 18 deletions.
258 changes: 244 additions & 14 deletions Jellyfin.Plugin.Bangumi/Anitomy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,264 @@

namespace Jellyfin.Plugin.Bangumi;

/// <summary>
/// The Anitomy class contains methods for extracting various elements from a string path using the AnitomySharp library.
/// </summary>
public class Anitomy
{
public static List<Element> ElementsOutput(string path)
// This variable stores the elements obtained from parsing a file path using the AnitomySharp library
private readonly IEnumerable<Element> _elements;

// The constructor takes a file path as input and calls the AnitomySharp.Parse method to parse the file and store the result in the _elements variable
public Anitomy(string path)
{
_elements = AnitomySharp.AnitomySharp.Parse(path);
}
// The constructor takes a file path and option as input and calls the AnitomySharp.Parse method to parse the file and store the result in the _elements variable
// default: Options(string AllowedDelimiters = " _.+,| ", bool ParseEpisodeNumber = true, bool ParseEpisodeTitle = true, bool ParseFileExtension = true, bool ParseReleaseGroup = true)
public Anitomy(string path, Options options)
{
_elements = AnitomySharp.AnitomySharp.Parse(path, options);
}

// This method returns a List of Element objects from the _elements variable
public List<Element> GetElements()
{
return new List<Element>(AnitomySharp.AnitomySharp.Parse(path));
return new List<Element>(_elements);
}

public static string? ExtractAnimeTitle(string path)
/// <summary>
/// Extracts the ElementAnimeSeason.
/// </summary>
/// <returns>The extracted ElementAnimeSeason, or null if not found.</returns>
public string? ExtractAnimeSeason()
{
var elements = AnitomySharp.AnitomySharp.Parse(path);
return elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementAnimeTitle)?.Value;
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementAnimeSeason)?.Value;
}

public static string? ExtractEpisodeTitle(string path)
/// <summary>
/// Extracts the ElementAnimeSeasonPrefix.
/// </summary>
/// <returns>The extracted ElementAnimeSeasonPrefix, or null if not found.</returns>
public string? ExtractAnimeSeasonPrefix()
{
var elements = AnitomySharp.AnitomySharp.Parse(path);
return elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementEpisodeTitle)?.Value;
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementAnimeSeasonPrefix)?.Value;
}

public static string? ExtractEpisodeNumber(string path)
/// <summary>
/// Extracts the ElementAnimeTitle.
/// </summary>
/// <returns>The extracted ElementAnimeTitle, or null if not found.</returns>
public string? ExtractAnimeTitle()
{
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementAnimeTitle)?.Value;
}
/// <summary>
/// Extracts the ElementAnimeType.
/// </summary>
/// <returns>The extracted ElementAnimeType, or null if not found.</returns>
public string[]? ExtractAnimeType()
{
var elements = AnitomySharp.AnitomySharp.Parse(path);
return elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementEpisodeNumber)?.Value;
return _elements.Where(p => p.Category == Element.ElementCategory.ElementAnimeType).Select(type => type.Value).ToArray();
}

public static string? ExtractSeasonNumber(string path)
/// <summary>
/// Extracts the ElementAnimeYear.
/// </summary>
/// <returns>The extracted ElementAnimeYear, or null if not found.</returns>
public string? ExtractAnimeYear()
{
var elements = AnitomySharp.AnitomySharp.Parse(path);
return elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementAnimeSeason)?.Value;
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementAnimeYear)?.Value;
}

/// <summary>
/// Extracts the ElementAudioTerm.
/// </summary>
/// <returns>The extracted ElementAudioTerm, or null if not found.</returns>
public string? ExtractAudioTerm()
{
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementAudioTerm)?.Value;
}

/// <summary>
/// Extracts the ElementDeviceCompatibility.
/// </summary>
/// <returns>The extracted ElementDeviceCompatibility, or null if not found.</returns>
public string? ExtractDeviceCompatibility()
{
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementDeviceCompatibility)?.Value;
}

/// <summary>
/// Extracts the ElementEpisodeNumber.
/// </summary>
/// <returns>The extracted ElementEpisodeNumber, or null if not found.</returns>
public string? ExtractEpisodeNumber()
{
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementEpisodeNumber)?.Value;
}

/// <summary>
/// Extracts the ElementEpisodeNumberAlt.
/// </summary>
/// <returns>The extracted ElementEpisodeNumberAlt, or null if not found.</returns>
public string? ExtractEpisodeNumberAlt()
{
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementEpisodeNumberAlt)?.Value;
}

/// <summary>
/// Extracts the ElementEpisodePrefix.
/// </summary>
/// <returns>The extracted ElementEpisodePrefix, or null if not found.</returns>
public string? ExtractEpisodePrefix()
{
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementEpisodePrefix)?.Value;
}

/// <summary>
/// Extracts the ElementEpisodeTitle.
/// </summary>
/// <returns>The extracted ElementEpisodeTitle, or null if not found.</returns>
public string? ExtractEpisodeTitle()
{
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementEpisodeTitle)?.Value;
}

/// <summary>
/// Extracts the ElementFileChecksum.
/// </summary>
/// <returns>The extracted ElementFileChecksum, or null if not found.</returns>
public string? ExtractFileChecksum()
{
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementFileChecksum)?.Value;
}

/// <summary>
/// Extracts the ElementFileExtension.
/// </summary>
/// <returns>The extracted ElementFileExtension, or null if not found.</returns>
public string? ExtractFileExtension()
{
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementFileExtension)?.Value;
}

/// <summary>
/// Extracts the ElementFileName.
/// </summary>
/// <returns>The extracted ElementFileName, or null if not found.</returns>
public string? ExtractFileName()
{
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementFileName)?.Value;
}

/// <summary>
/// Extracts the ElementLanguage.
/// </summary>
/// <returns>The extracted ElementLanguage, or null if not found.</returns>
public string? ExtractLanguage()
{
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementLanguage)?.Value;
}

/// <summary>
/// Extracts the ElementOther.
/// </summary>
/// <returns>The extracted ElementOther, or null if not found.</returns>
public string? ExtractOther()
{
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementOther)?.Value;
}

/// <summary>
/// Extracts the ElementReleaseGroup.
/// </summary>
/// <returns>The extracted ElementReleaseGroup, or null if not found.</returns>
public string? ExtractReleaseGroup()
{
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementReleaseGroup)?.Value;
}

/// <summary>
/// Extracts the ElementReleaseInformation.
/// </summary>
/// <returns>The extracted ElementReleaseInformation, or null if not found.</returns>
public string? ExtractReleaseInformation()
{
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementReleaseInformation)?.Value;
}

/// <summary>
/// Extracts the ElementReleaseVersion.
/// </summary>
/// <returns>The extracted ElementReleaseVersion, or null if not found.</returns>
public string? ExtractReleaseVersion()
{
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementReleaseVersion)?.Value;
}

/// <summary>
/// Extracts the ElementSource.
/// </summary>
/// <returns>The extracted ElementSource, or null if not found.</returns>
public string? ExtractSource()
{
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementSource)?.Value;
}

/// <summary>
/// Extracts the ElementSubtitles.
/// </summary>
/// <returns>The extracted ElementSubtitles, or null if not found.</returns>
public string? ExtractSubtitles()
{
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementSubtitles)?.Value;
}

/// <summary>
/// Extracts the ElementUnknown.
/// </summary>
/// <returns>The extracted ElementUnknown, or null if not found.</returns>
public string? ExtractUnknown()
{
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementUnknown)?.Value;
}

/// <summary>
/// Extracts the ElementVideoResolution.
/// </summary>
/// <returns>The extracted ElementVideoResolution, or null if not found.</returns>
public string? ExtractVideoResolution()
{
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementVideoResolution)?.Value;
}

/// <summary>
/// Extracts the ElementVideoTerm.
/// </summary>
/// <returns>The extracted ElementVideoTerm, or null if not found.</returns>
public string? ExtractVideoTerm()
{
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementVideoTerm)?.Value;
}

/// <summary>
/// Extracts the ElementVolumeNumber.
/// </summary>
/// <returns>The extracted ElementVolumeNumber, or null if not found.</returns>
public string? ExtractVolumeNumber()
{
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementVolumeNumber)?.Value;
}

/// <summary>
/// Extracts the ElementVolumePrefix.
/// </summary>
/// <returns>The extracted ElementVolumePrefix, or null if not found.</returns>
public string? ExtractVolumePrefix()
{
return _elements.FirstOrDefault(p => p.Category == Element.ElementCategory.ElementVolumePrefix)?.Value;
}


}
3 changes: 2 additions & 1 deletion Jellyfin.Plugin.Bangumi/Providers/AlbumProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public async Task<MetadataResult<MusicAlbum>> GetMetadata(AlbumInfo info, Cancel

if (subjectId == 0 && Configuration.AlwaysGetTitleByAnitomySharp)
{
var searchName = Anitomy.ExtractAnimeTitle(baseName) ?? info.Name;
var anitomy = new Anitomy(baseName);
var searchName = anitomy.ExtractAnimeTitle() ?? info.Name;
log.LogInformation("Searching {Name} in bgm.tv", searchName);
// 不保证使用非原名或中文进行查询时返回正确结果
var searchResult = await api.SearchSubject(searchName, SubjectType.Music, token);
Expand Down
3 changes: 2 additions & 1 deletion Jellyfin.Plugin.Bangumi/Providers/EpisodeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ private double GuessEpisodeNumber(double? current, string fileName, double max =

if (Configuration.AlwaysGetEpisodeByAnitomySharp)
{
var anitomyIndex = Anitomy.ExtractEpisodeNumber(fileName);
var anitomy = new Anitomy(fileName);
var anitomyIndex = anitomy.ExtractEpisodeNumber();
if (!string.IsNullOrEmpty(anitomyIndex))
{
log.LogInformation("used episode number {index} from anitomy", anitomyIndex);
Expand Down
3 changes: 2 additions & 1 deletion Jellyfin.Plugin.Bangumi/Providers/MovieProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public async Task<MetadataResult<Movie>> GetMetadata(MovieInfo info, Cancellatio

if (subjectId == 0 && Configuration.AlwaysGetTitleByAnitomySharp)
{
var searchName = Anitomy.ExtractAnimeTitle(baseName) ?? info.Name;
var anitomy = new Anitomy(baseName);
var searchName = anitomy.ExtractAnimeTitle() ?? info.Name;
logger.LogInformation("Searching {Name} in bgm.tv", searchName);
// 不保证使用非原名或中文进行查询时返回正确结果
var searchResult = await api.SearchSubject(searchName, token);
Expand Down
3 changes: 2 additions & 1 deletion Jellyfin.Plugin.Bangumi/Providers/SeriesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo info, Cancellat

if (subjectId == 0 && Configuration.AlwaysGetTitleByAnitomySharp)
{
var searchName = Anitomy.ExtractAnimeTitle(baseName) ?? info.Name;
var anitomy = new Anitomy(baseName);
var searchName = anitomy.ExtractAnimeTitle() ?? info.Name;
log.LogInformation("Searching {Name} in bgm.tv", searchName);
// 不保证使用非原名或中文进行查询时返回正确结果
var searchResult = await api.SearchSubject(searchName, token);
Expand Down

0 comments on commit 9f6703a

Please sign in to comment.