feat: HentaiPawのカバー画像Last-Modifiedから個別の公開・更新時間を取得・設定するように修正
This commit is contained in:
1 parent
9cb9892cb3
commit
bd5728b504
1 file changed
+98
-4
@@ -74,6 +74,14 @@ internal static class hentaipaw
|
|||||||
.Where(a => !string.Equals(a.GetAttribute("href"), "/articles/rank", StringComparison.OrdinalIgnoreCase))
|
.Where(a => !string.Equals(a.GetAttribute("href"), "/articles/rank", StringComparison.OrdinalIgnoreCase))
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
|
// カードに掲載されているカバー画像 URL を収集し、Last-Modified(公開・更新日時)を一括並列取得
|
||||||
|
var coverImageUrls = articleCards
|
||||||
|
.Select(card => card.QuerySelector("img")?.GetAttribute("src")?.Trim()
|
||||||
|
?? card.QuerySelector("img")?.GetAttribute("data-src")?.Trim()
|
||||||
|
?? string.Empty)
|
||||||
|
.Where(src => !string.IsNullOrEmpty(src));
|
||||||
|
var imageLastModifiedMap = await FetchImagesLastModifiedAsync(client, coverImageUrls);
|
||||||
|
|
||||||
var linkList = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
var linkList = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||||
int itemIndex = 0;
|
int itemIndex = 0;
|
||||||
|
|
||||||
@@ -129,19 +137,28 @@ internal static class hentaipaw
|
|||||||
var categories = new List<string>();
|
var categories = new List<string>();
|
||||||
var authors = new List<string>();
|
var authors = new List<string>();
|
||||||
|
|
||||||
|
// カバー画像の Last-Modified から正確な個別公開日時を取得
|
||||||
|
DateTimeOffset? detectedDate = null;
|
||||||
|
if (!string.IsNullOrEmpty(imgSrc) && imageLastModifiedMap.TryGetValue(imgSrc, out var imgDate))
|
||||||
|
{
|
||||||
|
detectedDate = imgDate;
|
||||||
|
}
|
||||||
|
|
||||||
if (prevEntry != null && !string.IsNullOrEmpty(prevEntry.SummaryHtml))
|
if (prevEntry != null && !string.IsNullOrEmpty(prevEntry.SummaryHtml))
|
||||||
{
|
{
|
||||||
// 既に取得済みの記事はキャッシュを活用(FlareSolverrへのアクセスを削減)
|
// 既に取得済みの記事はキャッシュを活用(FlareSolverrへのアクセスを削減)
|
||||||
itemPubDate = prevEntry.PublishDate;
|
|
||||||
summaryHtml = prevEntry.SummaryHtml;
|
summaryHtml = prevEntry.SummaryHtml;
|
||||||
categories.AddRange(prevEntry.Categories);
|
categories.AddRange(prevEntry.Categories);
|
||||||
authors.AddRange(prevEntry.Authors);
|
authors.AddRange(prevEntry.Authors);
|
||||||
|
|
||||||
|
// キャッシュされた日時は、カバー画像の日時が取得できればそちらに更新(過去の生成日時キャッシュを修復)
|
||||||
|
itemPubDate = detectedDate ?? (prevEntry.PublishDate != DateTimeOffset.MinValue
|
||||||
|
? prevEntry.PublishDate
|
||||||
|
: new DateTimeOffset(DateTime.Now.AddSeconds(-itemIndex)));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// 新規記事の場合、個別ページから詳細メタ情報を取得
|
// 新規記事の場合、個別ページから詳細メタ情報を取得
|
||||||
itemPubDate = new DateTimeOffset(DateTime.Now.AddSeconds(-itemIndex));
|
|
||||||
|
|
||||||
string? author = null;
|
string? author = null;
|
||||||
string? circle = null;
|
string? circle = null;
|
||||||
string? parody = null;
|
string? parody = null;
|
||||||
@@ -230,6 +247,25 @@ internal static class hentaipaw
|
|||||||
LogManager.AddLog($"[警告] hentaipaw 詳細ページ取得失敗 ({fullUrl}): {ex.Message}");
|
LogManager.AddLog($"[警告] hentaipaw 詳細ページ取得失敗 ({fullUrl}): {ex.Message}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// カバー画像の日時を最終決定(詳細ページで imgSrc が変更された場合のフォールバック含む)
|
||||||
|
if (!string.IsNullOrEmpty(imgSrc))
|
||||||
|
{
|
||||||
|
if (imageLastModifiedMap.TryGetValue(imgSrc, out var updatedDate))
|
||||||
|
{
|
||||||
|
detectedDate = updatedDate;
|
||||||
|
}
|
||||||
|
else if (!detectedDate.HasValue)
|
||||||
|
{
|
||||||
|
detectedDate = await GetImageLastModifiedAsync(client, imgSrc);
|
||||||
|
if (detectedDate.HasValue)
|
||||||
|
{
|
||||||
|
imageLastModifiedMap[imgSrc] = detectedDate.Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
itemPubDate = detectedDate ?? new DateTimeOffset(DateTime.Now.AddSeconds(-itemIndex));
|
||||||
|
|
||||||
// 本文 HTML (Summary) の生成
|
// 本文 HTML (Summary) の生成
|
||||||
var body = new StringBuilder();
|
var body = new StringBuilder();
|
||||||
string viewerUrl = $"https://ja.hentaipaw.com/viewer?articleId={Uri.EscapeDataString(articleId)}&page=1";
|
string viewerUrl = $"https://ja.hentaipaw.com/viewer?articleId={Uri.EscapeDataString(articleId)}&page=1";
|
||||||
@@ -319,7 +355,7 @@ internal static class hentaipaw
|
|||||||
}
|
}
|
||||||
|
|
||||||
feed.Items = items;
|
feed.Items = items;
|
||||||
await CmnUtil.ExportXMLFileAsync("hentaipaw.xml", feed, client);
|
await CmnUtil.ExportXMLFileAsync("hentaipaw.xml", feed, client, filter24Hours: false);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -390,4 +426,62 @@ internal static class hentaipaw
|
|||||||
|
|
||||||
return existingEntries;
|
return existingEntries;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// カバー画像 URL 群の Last-Modified ヘッダーを一括並列取得し、URL と公開・更新日時(JST)のマッピングを返します。
|
||||||
|
/// </summary>
|
||||||
|
private static async Task<Dictionary<string, DateTimeOffset>> FetchImagesLastModifiedAsync(HttpClient client, IEnumerable<string> imageUrls)
|
||||||
|
{
|
||||||
|
var dict = new Dictionary<string, DateTimeOffset>(StringComparer.OrdinalIgnoreCase);
|
||||||
|
var distinctUrls = imageUrls.Where(u => !string.IsNullOrEmpty(u)).Distinct(StringComparer.OrdinalIgnoreCase).ToList();
|
||||||
|
if (distinctUrls.Count == 0)
|
||||||
|
{
|
||||||
|
return dict;
|
||||||
|
}
|
||||||
|
|
||||||
|
var tasks = distinctUrls.Select(async url =>
|
||||||
|
{
|
||||||
|
var date = await GetImageLastModifiedAsync(client, url);
|
||||||
|
return (Url: url, Date: date);
|
||||||
|
});
|
||||||
|
|
||||||
|
var results = await Task.WhenAll(tasks);
|
||||||
|
foreach (var r in results)
|
||||||
|
{
|
||||||
|
if (r.Date.HasValue)
|
||||||
|
{
|
||||||
|
dict[r.Url] = r.Date.Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dict;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 単一のカバー画像 URL に対して HTTP HEAD リクエストを送信し、Last-Modified ヘッダーから公開・更新日時(JST)を取得します。
|
||||||
|
/// </summary>
|
||||||
|
private static async Task<DateTimeOffset?> GetImageLastModifiedAsync(HttpClient client, string imageUrl)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(imageUrl))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var req = new HttpRequestMessage(HttpMethod.Head, imageUrl);
|
||||||
|
using var resp = await client.SendAsync(req, HttpCompletionOption.ResponseHeadersRead);
|
||||||
|
if (resp.IsSuccessStatusCode && resp.Content.Headers.LastModified.HasValue)
|
||||||
|
{
|
||||||
|
// JST (+09:00) に変換
|
||||||
|
return resp.Content.Headers.LastModified.Value.ToOffset(TimeSpan.FromHours(9));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// 取得失敗時は null を返す
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user