Skip to content

Commit

Permalink
add infobox support; add translation setting for person #143
Browse files Browse the repository at this point in the history
  • Loading branch information
kookxiang committed Jul 21, 2024
1 parent 1bad618 commit 94894d2
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 7 deletions.
7 changes: 5 additions & 2 deletions Jellyfin.Plugin.Bangumi.Test/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.Bangumi.Configuration;
using Jellyfin.Plugin.Bangumi.Providers;
using Jellyfin.Plugin.Bangumi.Test.Util;
using MediaBrowser.Controller.Providers;
Expand All @@ -28,12 +29,14 @@ public void ProviderInfo()
[TestMethod]
public async Task GetById()
{
Bangumi.Plugin.Instance!.Configuration.PersonTranslationPreference = TranslationPreferenceType.Chinese;
var result = await _provider.GetMetadata(new PersonLookupInfo
{
ProviderIds = new Dictionary<string, string> { { Constants.ProviderName, "5847" } }
ProviderIds = new Dictionary<string, string> { { Constants.ProviderName, "7307" } }
}, _token);
Assert.IsNotNull(result.Item, "person info should not be null");
Assert.AreEqual("茅野愛衣", result.Item.Name, "should return correct name");
Assert.AreEqual("上坂すみれ", result.Item.OriginalTitle, "should return correct name");
Assert.AreEqual("上坂堇", result.Item.Name, "should return translated name");
Assert.IsNotNull(result.Item.ProviderIds[Constants.ProviderName], "should have plugin provider id");
}

Expand Down
1 change: 1 addition & 0 deletions Jellyfin.Plugin.Bangumi.Test/Series.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ private static void AssertSeries(MetadataResult<MediaBrowser.Controller.Entities
Assert.AreEqual("2013-10-05", result.Item.AirTime, "should return correct air time info");
Assert.AreEqual(DayOfWeek.Saturday, result.Item.AirDays?[0], "should return correct air day info");
Assert.IsTrue(result.Item.CommunityRating is > 0 and <= 10, "should return rating info");
Assert.IsNotNull(result.Item.HomePageUrl, "should return official website link");
Assert.IsNotNull(result.People.Find(x => x.IsType(PersonKind.Actor)), "should have at least one actor");
Assert.IsNotNull(result.People.Find(x => x.IsType(PersonKind.Director)), "should have at least one director");
Assert.IsNotNull(result.People.Find(x => x.IsType(PersonKind.Writer)), "should have at least one writer");
Expand Down
7 changes: 7 additions & 0 deletions Jellyfin.Plugin.Bangumi/Configuration/ConfigPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ <h2 class="sectionTitle">元数据</h2>
<option value="Chinese">优先使用中文翻译</option>
</select>
</div>
<div class="selectContainer">
<label class="selectLabel" for="PersonTranslationPreference">人物名称翻译</label>
<select class="emby-select-withcolor emby-select" id="PersonTranslationPreference" is="emby-select">
<option value="Original">优先使用日文</option>
<option value="Chinese">优先使用中文</option>
</select>
</div>
<div class="checkboxContainer checkboxContainer-withDescription">
<label class="emby-checkbox-label">
<input id="UseBangumiSeasonTitle" is="emby-checkbox" type="checkbox"/>
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 @@ -12,6 +12,8 @@ public class PluginConfiguration : BasePluginConfiguration
{
public TranslationPreferenceType TranslationPreference { get; set; } = TranslationPreferenceType.Chinese;

public TranslationPreferenceType PersonTranslationPreference { get; set; } = TranslationPreferenceType.Original;

public int RequestTimeout { get; set; } = 5000;

public string BaseServerUrl { get; set; } = "https://api.bgm.tv";
Expand Down
23 changes: 23 additions & 0 deletions Jellyfin.Plugin.Bangumi/Model/InfoBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Jellyfin.Plugin.Bangumi.Model;

public class InfoBox : List<InfoBoxItem>
{
public string? GetString(string key)
{
return this.FirstOrDefault(x => x.Key == key)?.Value.GetString();
}
}

public class InfoBoxItem
{
[JsonPropertyName("key")]
public string Key { get; set; } = null!;

[JsonPropertyName("value")]
public JsonElement Value { get; set; } = default!;
}
28 changes: 28 additions & 0 deletions Jellyfin.Plugin.Bangumi/Model/PersonDetail.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.Text.Json.Serialization;
using Jellyfin.Plugin.Bangumi.Configuration;

Expand Down Expand Up @@ -26,6 +27,33 @@ public class PersonDetail : Person
[JsonPropertyName("birth_day")]
public int? Birthday { get; set; }

[JsonIgnore]
public DateTime? Birthdate =>
BirthYear != null && BirthMonth != null && Birthday != null ? new DateTime((int)BirthYear, (int)BirthMonth, (int)Birthday) : null;

[JsonIgnore]
public string? BirthPlace => InfoBox?.GetString("出生地") ?? InfoBox?.GetString("出身地");

[JsonIgnore]
public DateTime? DeathDate
{
get
{
var dateStr = InfoBox?.GetString("卒日");
if (dateStr != null && DateTime.TryParseExact(dateStr, "yyyy年MM月dd日", CultureInfo.GetCultureInfo("zh-CN"), DateTimeStyles.None, out var date))
return date;
return null;
}
}

[JsonPropertyName("infobox")]
public InfoBox? InfoBox { get; set; }

[JsonIgnore]
public string TranslatedName => Configuration.PersonTranslationPreference switch
{
TranslationPreferenceType.Original => Name,
TranslationPreferenceType.Chinese => InfoBox?.GetString("简体中文名") ?? Name,
_ => Name
};
}
23 changes: 21 additions & 2 deletions Jellyfin.Plugin.Bangumi/Model/Subject.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text.Json.Serialization;
using Jellyfin.Plugin.Bangumi.Configuration;
#if !EMBY
using System;
using Fastenshtein;
#endif

Expand Down Expand Up @@ -83,6 +84,24 @@ public string[] PopularTags
}
}

[JsonPropertyName("infobox")]
public InfoBox? InfoBox { get; set; }

[JsonIgnore]
public string? OfficialWebSite => InfoBox?.GetString("官方网站");

[JsonIgnore]
public DateTime? EndDate
{
get
{
var dateStr = InfoBox?.GetString("播放结束");
if (dateStr != null && DateTime.TryParseExact(dateStr, "yyyy年MM月dd日", CultureInfo.GetCultureInfo("zh-CN"), DateTimeStyles.None, out var date))
return date;
return null;
}
}

public static List<Subject> SortBySimilarity(IEnumerable<Subject> list, string keyword)
{
#if EMBY
Expand Down
2 changes: 2 additions & 0 deletions Jellyfin.Plugin.Bangumi/Providers/MovieProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public async Task<MetadataResult<Movie>> GetMetadata(MovieInfo info, Cancellatio
result.Item.OriginalTitle = subject.OriginalName;
result.Item.Overview = string.IsNullOrEmpty(subject.Summary) ? null : subject.Summary;
result.Item.Tags = subject.PopularTags;
result.Item.HomePageUrl = subject.OfficialWebSite;
result.Item.EndDate = subject.EndDate;

if (DateTime.TryParse(subject.AirDate, out var airDate))
result.Item.PremiereDate = airDate;
Expand Down
7 changes: 5 additions & 2 deletions Jellyfin.Plugin.Bangumi/Providers/PersonProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ public async Task<MetadataResult<Person>> GetMetadata(PersonLookupInfo info, Can
result.HasMetadata = true;
result.Item = new Person
{
Name = person.Name,
Name = person.TranslatedName,
OriginalTitle = person.Name,
Overview = person.Summary,
PremiereDate = person.Birthdate,
ProductionYear = person.BirthYear
ProductionYear = person.BirthYear,
ProductionLocations = [person.BirthPlace],
EndDate = person.DeathDate
};
result.Item.ProviderIds.Add(Constants.ProviderName, $"{person.Id}");
return result;
Expand Down
3 changes: 3 additions & 0 deletions Jellyfin.Plugin.Bangumi/Providers/SeasonProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ public async Task<MetadataResult<Season>> GetMetadata(SeasonInfo info, Cancellat
if (subject.ProductionYear?.Length == 4)
result.Item.ProductionYear = int.Parse(subject.ProductionYear);

result.Item.HomePageUrl = subject.OfficialWebSite;
result.Item.EndDate = subject.EndDate;

if (subject.IsNSFW)
result.Item.OfficialRating = "X";

Expand Down
4 changes: 3 additions & 1 deletion Jellyfin.Plugin.Bangumi/Providers/SeriesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo info, Cancellat
result.Item.OriginalTitle = subject.OriginalName;
result.Item.Overview = string.IsNullOrEmpty(subject.Summary) ? null : subject.Summary;
result.Item.Tags = subject.PopularTags;
result.Item.HomePageUrl = subject.OfficialWebSite;
result.Item.EndDate = subject.EndDate;

if (DateTime.TryParse(subject.AirDate, out var airDate))
{
result.Item.AirTime = subject.AirDate;
result.Item.AirDays = new[] { airDate.DayOfWeek };
result.Item.AirDays = [airDate.DayOfWeek];
result.Item.PremiereDate = airDate;
result.Item.ProductionYear = airDate.Year;
}
Expand Down

0 comments on commit 94894d2

Please sign in to comment.