base64形式でサムネイルを表示するように
This commit is contained in:
1 parent
233ef518c3
commit
f5230cb0ab
6 files changed
+161
-14
No files matched your search
@@ -33,7 +33,27 @@ internal static class pixiv_common
|
||||
public static string UserAgent { get; set; } = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36";
|
||||
|
||||
/// <summary>
|
||||
/// RSSリーダー等で画像直リンク時の403エラーを回避するため、画像プロキシ(i.pixiv.re)を利用するかどうか
|
||||
/// サムネイル画像を直接取得して Base64 形式(Data URI: data:image/jpeg;base64,...)で RSS に埋め込むかどうか。
|
||||
/// 有効にすると RSS リーダー側での画像読み込み遅延(約9秒)および 403 エラーが完全に解消されます。
|
||||
/// 環境変数 PIXIV_EMBED_BASE64 (true/false) での上書きも可能です。
|
||||
/// </summary>
|
||||
public static bool EmbedThumbnailBase64
|
||||
{
|
||||
get
|
||||
{
|
||||
var envVal = Environment.GetEnvironmentVariable("PIXIV_EMBED_BASE64");
|
||||
if (!string.IsNullOrWhiteSpace(envVal) && bool.TryParse(envVal, out var parsed))
|
||||
{
|
||||
return parsed;
|
||||
}
|
||||
return _embedThumbnailBase64;
|
||||
}
|
||||
set => _embedThumbnailBase64 = value;
|
||||
}
|
||||
private static bool _embedThumbnailBase64 = true;
|
||||
|
||||
/// <summary>
|
||||
/// RSSリーダー等で画像直リンク時の403エラーを回避するため、画像プロキシ(i.pixiv.re)を利用するかどうか(Base64埋め込みが無効または失敗時のフォールバック先)
|
||||
/// </summary>
|
||||
public static bool UseImageProxy { get; set; } = true;
|
||||
|
||||
@@ -298,11 +318,122 @@ internal static class pixiv_common
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pixiv の作品アイテムリストを SyndicationItem のリストに変換します。
|
||||
/// Pixiv の画像 URL (i.pximg.net) から直接画像データをダウンロードし、Base64 (Data URI) 形式の文字列として取得します。
|
||||
/// Referer (https://www.pixiv.net/) や User-Agent、Cookie などのヘッダーを付加して取得します。
|
||||
/// </summary>
|
||||
/// <param name="client">HttpClient インスタンス</param>
|
||||
/// <param name="imageUrl">Pixiv 画像 URL (例: https://i.pximg.net/c/250x250_80_a2/...)</param>
|
||||
/// <returns>Base64 Data URI 文字列(例: "data:image/jpeg;base64,...")、失敗時は null</returns>
|
||||
public static async Task<string?> FetchImageAsBase64Async(HttpClient client, string imageUrl)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(imageUrl)) return null;
|
||||
|
||||
try
|
||||
{
|
||||
// プロキシドメインに置換されていた場合は i.pximg.net に戻して Pixiv 公式 CDN から直接高速取得
|
||||
var targetUrl = imageUrl;
|
||||
if (targetUrl.Contains(ImageProxyHost) && ImageProxyHost != "i.pximg.net")
|
||||
{
|
||||
targetUrl = targetUrl.Replace(ImageProxyHost, "i.pximg.net");
|
||||
}
|
||||
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, targetUrl);
|
||||
request.Headers.Referrer = new Uri(REFERER_URL);
|
||||
request.Headers.Add("User-Agent", UserAgent);
|
||||
|
||||
var effectiveCookie = GetEffectiveCookie();
|
||||
if (!string.IsNullOrWhiteSpace(effectiveCookie))
|
||||
{
|
||||
request.Headers.Add("Cookie", effectiveCookie);
|
||||
}
|
||||
|
||||
using var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
AddLog($"[警告] [pixiv_common] サムネイル画像取得失敗 (Status={(int)response.StatusCode}, URL={targetUrl})");
|
||||
return null;
|
||||
}
|
||||
|
||||
var contentType = response.Content.Headers.ContentType?.MediaType ?? "image/jpeg";
|
||||
var bytes = await response.Content.ReadAsByteArrayAsync();
|
||||
if (bytes == null || bytes.Length == 0) return null;
|
||||
|
||||
var base64 = Convert.ToBase64String(bytes);
|
||||
return $"data:{contentType};base64,{base64}";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AddLog($"[エラー] [pixiv_common] サムネイル Base64 取得例外 (URL={imageUrl}): {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pixiv の作品アイテムリストを非同期で SyndicationItem のリストに変換します。
|
||||
/// EmbedThumbnailBase64 が有効な場合、サムネイル画像を最大並列数で直接ダウンロードして Base64 埋め込みを行います。
|
||||
/// </summary>
|
||||
/// <param name="client">HttpClient インスタンス</param>
|
||||
/// <param name="illusts">Pixiv 作品アイテム列挙</param>
|
||||
/// <param name="maxParallelDownloads">画像ダウンロードの最大並列数(デフォルト: 6)</param>
|
||||
/// <returns>SyndicationItem リスト</returns>
|
||||
public static async Task<List<SyndicationItem>> ConvertToSyndicationItemsAsync(
|
||||
HttpClient client,
|
||||
IEnumerable<PixivIllustItem> illusts,
|
||||
int maxParallelDownloads = 6)
|
||||
{
|
||||
var illustList = illusts.ToList();
|
||||
var base64ImageMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
// Base64 埋め込みが有効な場合、画像を並列ダウンロードしてマップに保持
|
||||
if (EmbedThumbnailBase64)
|
||||
{
|
||||
using var semaphore = new SemaphoreSlim(maxParallelDownloads);
|
||||
var tasks = illustList
|
||||
.Where(i => !string.IsNullOrWhiteSpace(i.Url))
|
||||
.Select(async illust =>
|
||||
{
|
||||
await semaphore.WaitAsync();
|
||||
try
|
||||
{
|
||||
var dataUri = await FetchImageAsBase64Async(client, illust.Url);
|
||||
if (!string.IsNullOrWhiteSpace(dataUri))
|
||||
{
|
||||
lock (base64ImageMap)
|
||||
{
|
||||
base64ImageMap[illust.Id] = dataUri;
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
semaphore.Release();
|
||||
}
|
||||
});
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
AddLog($"[情報] [pixiv_common] サムネイル画像の Base64 取得完了: {base64ImageMap.Count}/{illustList.Count} 件成功");
|
||||
}
|
||||
|
||||
// アイテムの組み立て(各アイテムに Base64 またはフォールバック URL を設定)
|
||||
return ConvertToSyndicationItemsInternal(illustList, base64ImageMap);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pixiv の作品アイテムリストを SyndicationItem のリストに変換します(同期版・後方互換用)。
|
||||
/// </summary>
|
||||
/// <param name="illusts">Pixiv 作品アイテム列挙</param>
|
||||
/// <returns>SyndicationItem リスト</returns>
|
||||
public static List<SyndicationItem> ConvertToSyndicationItems(IEnumerable<PixivIllustItem> illusts)
|
||||
{
|
||||
return ConvertToSyndicationItemsInternal(illusts, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pixiv の作品アイテムリストを SyndicationItem のリストに変換する共通内部処理です。
|
||||
/// </summary>
|
||||
private static List<SyndicationItem> ConvertToSyndicationItemsInternal(
|
||||
IEnumerable<PixivIllustItem> illusts,
|
||||
Dictionary<string, string>? base64ImageMap)
|
||||
{
|
||||
var items = new List<SyndicationItem>();
|
||||
|
||||
@@ -375,11 +506,19 @@ internal static class pixiv_common
|
||||
// 本文 HTML の生成
|
||||
var body = new StringBuilder();
|
||||
|
||||
// 画像 URL の決定(プロキシ適用)
|
||||
var displayImageUrl = illust.Url;
|
||||
if (UseImageProxy && !string.IsNullOrWhiteSpace(displayImageUrl))
|
||||
// 画像 URL の決定(Base64 埋め込み優先、なければプロキシまたは元URL適用)
|
||||
string? displayImageUrl = null;
|
||||
if (base64ImageMap != null && base64ImageMap.TryGetValue(illust.Id, out var base64Uri))
|
||||
{
|
||||
displayImageUrl = displayImageUrl.Replace("i.pximg.net", ImageProxyHost);
|
||||
displayImageUrl = base64Uri;
|
||||
}
|
||||
else
|
||||
{
|
||||
displayImageUrl = illust.Url;
|
||||
if (UseImageProxy && !string.IsNullOrWhiteSpace(displayImageUrl))
|
||||
{
|
||||
displayImageUrl = displayImageUrl.Replace("i.pximg.net", ImageProxyHost);
|
||||
}
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(displayImageUrl))
|
||||
|
||||
@@ -32,8 +32,8 @@ internal class pixiv_non_r18_ai
|
||||
minCount: 30
|
||||
);
|
||||
|
||||
// SyndicationItem のリストに変換
|
||||
var items = pixiv_common.ConvertToSyndicationItems(illusts);
|
||||
// SyndicationItem のリストに変換(Base64 サムネイル埋め込み対応)
|
||||
var items = await pixiv_common.ConvertToSyndicationItemsAsync(client, illusts);
|
||||
|
||||
// フィード作成と XML 出力
|
||||
pixiv_common.CreateAndExportFeed(
|
||||
|
||||
@@ -32,8 +32,8 @@ internal class pixiv_non_r18_non_ai
|
||||
minCount: 30
|
||||
);
|
||||
|
||||
// SyndicationItem のリストに変換
|
||||
var items = pixiv_common.ConvertToSyndicationItems(illusts);
|
||||
// SyndicationItem のリストに変換(Base64 サムネイル埋め込み対応)
|
||||
var items = await pixiv_common.ConvertToSyndicationItemsAsync(client, illusts);
|
||||
|
||||
// フィード作成と XML 出力
|
||||
pixiv_common.CreateAndExportFeed(
|
||||
|
||||
@@ -32,8 +32,8 @@ internal class pixiv_r18_ai
|
||||
minCount: 30
|
||||
);
|
||||
|
||||
// SyndicationItem のリストに変換
|
||||
var items = pixiv_common.ConvertToSyndicationItems(illusts);
|
||||
// SyndicationItem のリストに変換(Base64 サムネイル埋め込み対応)
|
||||
var items = await pixiv_common.ConvertToSyndicationItemsAsync(client, illusts);
|
||||
|
||||
// フィード作成と XML 出力
|
||||
pixiv_common.CreateAndExportFeed(
|
||||
|
||||
@@ -32,8 +32,8 @@ internal class pixiv_r18_non_ai
|
||||
minCount: 30
|
||||
);
|
||||
|
||||
// SyndicationItem のリストに変換
|
||||
var items = pixiv_common.ConvertToSyndicationItems(illusts);
|
||||
// SyndicationItem のリストに変換(Base64 サムネイル埋め込み対応)
|
||||
var items = await pixiv_common.ConvertToSyndicationItemsAsync(client, illusts);
|
||||
|
||||
// フィード作成と XML 出力
|
||||
pixiv_common.CreateAndExportFeed(
|
||||
|
||||
Reference in new issue
Block a user