Files
RSSAPI/CienRSSAPI/Program.cs
T
2026-04-27 19:46:46 +09:00

129 lines
5.0 KiB
C#

using AngleSharp;
using AngleSharp.Dom;
using System.ServiceModel.Syndication;
using System.Text.RegularExpressions;
using System.Xml;
var builder = WebApplication.CreateBuilder(args);
// --- 1. AngleSharpの初期設定 ---
var config = Configuration.Default.WithDefaultLoader();
var context = BrowsingContext.New(config);
var app = builder.Build();
// --- 2. APIエンドポイントの定義 ---
app.MapGet("/ci-en/{creatorId}", async (string creatorId) =>
{
// HTMLの記載に合わせてドメインを ci-en.dlsite.com に修正
var targetUrl = $"https://ci-en.dlsite.com/creator/{creatorId}/article";
try
{
// --- 3. HTMLの取得と解析 ---
var document = await context.OpenAsync(targetUrl);
var pageTitle = document.QuerySelector("title")?.TextContent ?? $"{creatorId} - Ci-en Articles";
// --- 4. RSSフィードの基本情報を作成 ---
var feed = new SyndicationFeed(
title: pageTitle,
description: "Ci-en Unofficial Scraped RSS Feed",
feedAlternateLink: new Uri(targetUrl)
);
var items = new List<SyndicationItem>();
// --- 5. 記事一覧の要素を取得(実際のクラス名に修正) ---
var articleElements = document.QuerySelectorAll(".c-postedArticle");
foreach (var element in articleElements)
{
// ==========================================
// 【機能1】固定記事の除外ロジック
// ==========================================
// 取得した要素自身が "is-fixed" クラスを持っているか判定
if (element.ClassList.Contains("is-fixed"))
{
continue; // 固定記事の場合はスキップして次の記事へ
}
// タイトル、リンク、日付の取得
var titleElement = element.QuerySelector(".e-title");
var linkElement = element.QuerySelector("a.c-cardLink");
var title = titleElement?.TextContent?.Trim();
var link = linkElement?.GetAttribute("href");
// 相対パスの場合は絶対パスに変換
if (!string.IsNullOrEmpty(link) && link.StartsWith("/"))
{
link = $"https://ci-en.dlsite.com{link}";
}
if (!string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(link))
{
// 日付の取得と変換
var dateText = element.QuerySelector(".e-date")?.TextContent?.Trim();
var pubDate = DateTimeOffset.Now;
if (!string.IsNullOrEmpty(dateText))
{
// "2026/04/03 00:04" などの文字列を日付型に変換
if (DateTimeOffset.TryParse(dateText, out var parsedDate))
{
pubDate = parsedDate;
}
}
// ==========================================
// 【機能2】サムネイル画像の取得ロジック
// ==========================================
var imgElement = element.QuerySelector(".v-image");
// 遅延読み込み対策: data-src を優先し、なければ src を取得
var imageUrl = imgElement?.GetAttribute("data-src");
if (string.IsNullOrEmpty(imageUrl))
{
imageUrl = imgElement?.GetAttribute("src");
}
// RSSリーダーで表示するためのHTMLコンテンツを作成
var contentBody = "<p>この記事の詳細はCi-enのサイト内で確認してください。</p>";
if (!string.IsNullOrEmpty(imageUrl))
{
// 本文内に画像を埋め込む
contentBody = $"<div><img src=\"{imageUrl}\" /></div>" + contentBody;
}
var item = new SyndicationItem()
{
Title = new TextSyndicationContent(title),
Id = link,
PublishDate = pubDate,
Content = SyndicationContent.CreateHtmlContent(contentBody)
};
item.Links.Add(new SyndicationLink(new Uri(link)));
items.Add(item);
}
}
feed.Items = items;
// --- 6. XMLフォーマットで出力(文字化け修正・UTF-8対応版) ---
using var memoryStream = new MemoryStream();
using (var xmlWriter = XmlWriter.Create(memoryStream, new XmlWriterSettings
{
Indent = true,
Encoding = System.Text.Encoding.UTF8
}))
{
var rssFormatter = new Rss20FeedFormatter(feed);
rssFormatter.WriteTo(xmlWriter);
}
return Results.File(memoryStream.ToArray(), contentType: "application/rss+xml; charset=utf-8");
}
catch (Exception ex)
{
return Results.Problem(detail: ex.Message, statusCode: 500, title: "Scraping Error");
}
});
app.Run();