Skip to content

Commit

Permalink
Update image providers
Browse files Browse the repository at this point in the history
  • Loading branch information
Xzonn committed May 19, 2024
1 parent 8e05c54 commit b34cad3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
19 changes: 17 additions & 2 deletions Jellyfin.Plugin.Douban/DoubanApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ public async Task<List<RemoteImageInfo>> FetchMovieImages(string sid, string typ
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(responseText);

var results = Helper.ParseMovieImages(responseText, imageType, Configuration.DistinguishUsingAspectRatio, Configuration.CdnServer);
var results = Helper.ParseImages(responseText, imageType, Configuration.DistinguishUsingAspectRatio, Configuration.CdnServer);
_log.LogDebug("{count} image(s) found for movie {sid}", results.Count, sid);
return results;
}

Expand Down Expand Up @@ -319,11 +320,25 @@ public async Task<ApiPersonSubject> FetchPersonByPersonageId(string pid, Cancell
string? responseText = await FetchUrl(url, token);
if (string.IsNullOrEmpty(responseText)) { return new ApiPersonSubject(); }

var result = Helper.ParsePerson(responseText, pid);
var result = Helper.ParsePerson(responseText, pid, Configuration.CdnServer);
_log.LogDebug("PersonageId {pid} is: {name}", pid, result.Name);
return result;
}

public async Task<List<RemoteImageInfo>> FetchPersonImages(string pid, CancellationToken token = default)
{
_log.LogDebug("Fetching images for person: {pid}", pid);
string url = $"https://www.douban.com/personage/{pid}/photos/";
string? responseText = await FetchUrl(url, token);
if (string.IsNullOrEmpty(responseText)) { return []; }
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(responseText);

var results = Helper.ParseImages(responseText, ImageType.Primary, false, Configuration.CdnServer);
_log.LogDebug("{count} image(s) found for person {pid}", results.Count, pid);
return results;
}

private async Task<string?> FetchUrl(string url, CancellationToken token = default)
{
if (Caches.Count > 0 && CacheLastClean < DateTime.Now - TimeSpan.FromDays(1))
Expand Down
16 changes: 11 additions & 5 deletions Jellyfin.Plugin.Douban/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Jellyfin.Plugin.Douban.Model;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -33,9 +34,9 @@ public static class Helper
private static Regex REGEX_DATE => new(@"\d{4}-\d\d-\d\d");
private static Regex REGEX_CELEBRITY => new(@"/celebrity/(\d+)/");
private static Regex REGEX_DOUBANIO_HOST => new(@"https?://img\d+\.doubanio.com");

public static Regex REGEX_SEASON => new(@" *第(?<season>[一二三四五六七八九十百千万\d]+)[季期部]| *\b(?:Season) (?<season>\d+)", RegexOptions.IgnoreCase);
public static Regex REGEX_SEASON_2 => new(@"(?<![a-z\d\.']|女神异闻录|Part )(?<season>\d{1,2})$");
private static Regex REGEX_SEASON => new(@" *第(?<season>[一二三四五六七八九十百千万\d]+)[季期部]| *\b(?:Season) (?<season>\d+)", RegexOptions.IgnoreCase);
private static Regex REGEX_SEASON_2 => new(@"(?<![a-z\d\.']|女神异闻录|Part )(?<season>\d{1,2})$");
private static Regex REGEX_IMAGE_VOTE => new(@"(\d+)回应");

public static string? AnitomySharpParse(string name, ElementCategory category)
{
Expand Down Expand Up @@ -354,17 +355,20 @@ public static List<PersonInfo> ParseMovieCelebrities(string responseText, string
}
}

public static List<RemoteImageInfo> ParseMovieImages(string responseText, ImageType imageType = ImageType.Primary, bool distinguishUsingAspectRatio = DEFAULT_DISTINGUISH_USING_ASPECT_RATIO, string cdnServer = DEFAULT_CDN_SERVER)
public static List<RemoteImageInfo> ParseImages(string responseText, string cdnServer) => ParseImages(responseText, ImageType.Primary, DEFAULT_DISTINGUISH_USING_ASPECT_RATIO, cdnServer);

public static List<RemoteImageInfo> ParseImages(string responseText, ImageType imageType = ImageType.Primary, bool distinguishUsingAspectRatio = DEFAULT_DISTINGUISH_USING_ASPECT_RATIO, string cdnServer = DEFAULT_CDN_SERVER)
{
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(responseText);

var results = htmlDoc.QuerySelectorAll(".article ul li").Select(_ =>
{
var posterId = REGEX_IMAGE.Match(_.QuerySelector("img").Attributes["src"].Value).Groups[1].Value;
var size = _.QuerySelector(".prop")?.InnerText.Trim().Split("x") ?? ["0", "0"];
var size = (_.QuerySelector(".prop") ?? _.QuerySelector(".size"))?.InnerText.Trim().Split("x") ?? ["0", "0"];
var width = Convert.ToInt32(size[0]);
var height = Convert.ToInt32(size[1]);
int.TryParse(REGEX_IMAGE_VOTE.Match(_.QuerySelector(".name a")?.InnerText ?? "").Groups[1].Value, out var vote);
return new RemoteImageInfo()
{
ProviderName = Constants.PluginName,
Expand All @@ -374,6 +378,8 @@ public static List<RemoteImageInfo> ParseMovieImages(string responseText, ImageT
Url = $"{cdnServer}/view/photo/l/public/{posterId}.jpg",
Width = width,
Height = height,
CommunityRating = vote == 0 ? null : vote,
RatingType = RatingType.Likes,
};
}).ToList();
return results;
Expand Down
1 change: 1 addition & 0 deletions Jellyfin.Plugin.Douban/Provider/MovieImageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using Microsoft.Extensions.Logging;
Expand Down
2 changes: 2 additions & 0 deletions Jellyfin.Plugin.Douban/Provider/PersonImageProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -43,6 +44,7 @@ public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, Cancell
};
images.Add(image);
}
(await api.FetchPersonImages(pid.ToString(), token)).ForEach(images.Add);

return images;
}
Expand Down

0 comments on commit b34cad3

Please sign in to comment.