Files
rss-generator/rss-generator/Class/RSS/missav123.cs
T

93 lines
3.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using AngleSharp.Dom;
using System;
using System.Collections.Generic;
using System.ServiceModel.Syndication;
using System.Text;
namespace rss_generator.Class.RSS;
internal class missav123
{
public static async Task CreateRSSAsync(HttpClient client)
{
var pageMaster = "https://123av.com";
var feed = new SyndicationFeed("123av.com", "", new Uri($"{pageMaster}/ja/recent"));
var items = new List<SyndicationItem>();
try
{
// 複数ページをまとめる場合に先に取得する
var docList = new List<IDocument>();
for (int pageId = 1; pageId <= 2; pageId++)
{
var pageURL = $"{pageMaster}/ja/recent{(pageId > 1 ? $"?page={pageId}" : "")}";
var document = await CmnUtil.GetDocument(client, pageURL);
docList.Add(document);
}
var linkList = new List<string>();
foreach (var document in docList)
{
if (document == null)
{
continue;
}
var pageTitle = document.QuerySelector("title")?.TextContent;
if (pageTitle == null)
{
continue;
}
var elements = document.QuerySelectorAll("div.card");
var targetDivs = elements.Where(e => e.ClassList.Length == 1).ToList();
foreach (var div in targetDivs)
{
var titleElement = div.QuerySelector("a.card__link");
var linkElement = div.QuerySelector("a.card__link");
var imgElement = div.QuerySelector("img");
var title = titleElement?.TextContent?.Trim() ?? "タイトルなし";
var linkHref = $"{pageMaster}{linkElement?.GetAttribute("href")}";
var imageSrc = imgElement?.GetAttribute("src");
if (linkHref != string.Empty && !linkList.Contains(linkHref))
{
linkList.Add(linkHref);
}
else
{
continue;
}
var itemUri = !string.IsNullOrEmpty(linkHref) ? new Uri(linkHref) : null;
var imageUri = !string.IsNullOrEmpty(imageSrc) ? new Uri(imageSrc) : null;
var body = new StringBuilder();
body.Append($@"<div><a href=""{itemUri}"">{title}</a></div>");
body.Append($@"<div><img src=""{imageUri}"" alt=""{title}"" style=""max-width: 100%; height: auto;"" /></div>");
var item = new SyndicationItem(
title,
string.Empty,
itemUri,
CmnUtil.CreateGuid(linkHref),
DateTime.Now // 取得できた公開日時があればそれをセット
);
// item.Content = SyndicationContent.CreateHtmlContent(body.ToString());
// ※ RSS 2.0の場合は Content よりも Summary(<description>タグ)が使われることが多いため、
// 両方に設定しておくか、Summary側にHTMLを設定すると大半のリーダーで確実です。
item.Summary = new TextSyndicationContent(body.ToString(), TextSyndicationContentKind.Html);
items.Add(item);
}
}
feed.Items = items;
CmnUtil.ExportXMLFile("123av.xml", feed);
}
catch(Exception ex)
{
LogManager.AddLog($"[エラー] 123av:{ex.Message}");
}
}
}