Skip to content

Commit

Permalink
fix: actor url change. close #86
Browse files Browse the repository at this point in the history
  • Loading branch information
cxfksword committed Jul 2, 2024
1 parent f337406 commit cb5d12f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Jellyfin.Plugin.MetaShark.Test/PersonProviderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void TestGetMetadata()

Task.Run(async () =>
{
var info = new PersonLookupInfo() { ProviderIds = new Dictionary<string, string>() { { BaseProvider.DoubanProviderId, "1016771" } } };
var info = new PersonLookupInfo() { ProviderIds = new Dictionary<string, string>() { { BaseProvider.DoubanProviderId, "27257290" } } };
var provider = new PersonProvider(httpClientFactory, loggerFactory, libraryManagerStub.Object, httpContextAccessorStub.Object, doubanApi, tmdbApi, omdbApi, imdbApi);
var result = await provider.GetMetadata(info, CancellationToken.None);
Assert.IsNotNull(result);
Expand Down
53 changes: 48 additions & 5 deletions Jellyfin.Plugin.MetaShark/Api/DoubanApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -492,15 +492,22 @@ public async Task<List<DoubanCelebrity>> GetCelebritiesBySidAsync(string sid, Ca
return null;
}

var cacheKey = $"celebrity_{id}";
var cacheKey = $"personage_{id}";
var expiredOption = new MemoryCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30) };
DoubanCelebrity? celebrity;
if (_memoryCache.TryGetValue<DoubanCelebrity?>(cacheKey, out celebrity))
{
return celebrity;
}

var url = $"https://movie.douban.com/celebrity/{id}/";
// 兼容旧版 ID 处理
var personageID = await this.CheckPersonageIDAsync(id, cancellationToken).ConfigureAwait(false);
if (!string.IsNullOrEmpty(personageID))
{
id = personageID;
}

var url = $"https://www.douban.com/personage/{id}/";
var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false);
response.EnsureSuccessStatusCode();

Expand All @@ -511,6 +518,7 @@ public async Task<List<DoubanCelebrity>> GetCelebritiesBySidAsync(string sid, Ca
var contentNode = doc.QuerySelector("#content");
if (contentNode != null)
{
celebrity.Id = id;
celebrity.Img = contentNode.GetAttr("img.avatar", "src") ?? string.Empty;
var nameStr = contentNode.GetText("h1.subject-name") ?? string.Empty;
celebrity.Name = this.ParseCelebrityName(nameStr);
Expand Down Expand Up @@ -573,11 +581,39 @@ public async Task<List<DoubanCelebrity>> GetCelebritiesBySidAsync(string sid, Ca
return celebrity;
}


_memoryCache.Set<DoubanCelebrity?>(cacheKey, null, expiredOption);
return null;
}

public async Task<string?> CheckPersonageIDAsync(string id, CancellationToken cancellationToken)
{
if (id.Length != 7)
{
return null;
}

var url = $"https://movie.douban.com/celebrity/{id}/";
var handler = new HttpClientHandler()
{
AllowAutoRedirect = false
};
using (var noRedirectClient = new HttpClient(handler))
{
var resp = await noRedirectClient.GetAsync(url, cancellationToken).ConfigureAwait(false);
if (resp.Headers.TryGetValues("Location", out var values))
{
var location = values.First();
var newId = location.GetMatchGroup(this.regId);
if (!string.IsNullOrEmpty(newId))
{
return newId;
}
}
}

return null;
}


public async Task<List<DoubanPhoto>> GetCelebrityPhotosAsync(string cid, CancellationToken cancellationToken)
{
Expand All @@ -587,7 +623,7 @@ public async Task<List<DoubanPhoto>> GetCelebrityPhotosAsync(string cid, Cancell
return list;
}

var cacheKey = $"celebrity_photo_{cid}";
var cacheKey = $"personage_photo_{cid}";
var expiredOption = new MemoryCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30) };
if (_memoryCache.TryGetValue<List<DoubanPhoto>>(cacheKey, out var photos))
{
Expand All @@ -598,7 +634,14 @@ public async Task<List<DoubanPhoto>> GetCelebrityPhotosAsync(string cid, Cancell

try
{
var url = $"https://movie.douban.com/celebrity/{cid}/photos/";
// 兼容旧版 ID 处理
var personageID = await this.CheckPersonageIDAsync(cid, cancellationToken).ConfigureAwait(false);
if (!string.IsNullOrEmpty(personageID))
{
cid = personageID;
}

var url = $"https://www.douban.com/personage/{cid}/photos/";
var response = await httpClient.GetAsync(url, cancellationToken).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class DoubanPersonExternalId : IExternalId
public ExternalIdMediaType? Type => ExternalIdMediaType.Person;

/// <inheritdoc />
public string UrlFormatString => "https://movie.douban.com/celebrity/{0}/";
public string UrlFormatString => "https://www.douban.com/personage/{0}/";

/// <inheritdoc />
public bool Supports(IHasProviderIds item) => item is Person;
Expand Down
2 changes: 1 addition & 1 deletion Jellyfin.Plugin.MetaShark/Providers/PersonProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public async Task<MetadataResult<Person>> GetMetadata(PersonLookupInfo info, Can
item.ProductionLocations = new[] { c.Birthplace };
}

item.SetProviderId(DoubanProviderId, cid);
item.SetProviderId(DoubanProviderId, c.Id);
if (!string.IsNullOrEmpty(c.Imdb))
{
var newImdbId = await this._imdbApi.CheckPersonNewIDAsync(c.Imdb, cancellationToken).ConfigureAwait(false);
Expand Down

0 comments on commit cb5d12f

Please sign in to comment.